有部分功能未实现
项目结构如下图
com.frank.doamin
Department.java
1 package com.frank.domain; 2 3 import java.util.Set; 4 5 public class Department { 6 private Integer id; 7 private String name; 8 private Set<Employee> emps; 9 10 public Department(){ 11 12 } 13 public Department(String name){ 14 this.name = name; 15 } 16 public Set<Employee> getEmps() { 17 return emps; 18 } 19 public void setEmps(Set<Employee> emps) { 20 this.emps = emps; 21 } 22 public Integer getId() { 23 return id; 24 } 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 public String getName() { 29 return name; 30 } 31 public void setName(String name) { 32 this.name = name; 33 } 34 35 }
Employee.java
1 package com.frank.domain; 2 3 import java.util.Date; 4 5 public class Employee { 6 private Integer id; 7 private String passwd; 8 private Integer grade; 9 private Department department; 10 public Employee(){ 11 12 } 13 public Employee(String passwd, Integer grade, 14 String name, 15 String email, Date hiredate, Float salary) { 16 this.passwd = passwd; 17 this.grade = grade; 18 19 this.name = name; 20 this.email = email; 21 this.hiredate = hiredate; 22 this.salary = salary; 23 } 24 public Department getDepartment() { 25 return department; 26 } 27 public void setDepartment(Department department) { 28 this.department = department; 29 } 30 public String getPasswd() { 31 return passwd; 32 } 33 public void setPasswd(String passwd) { 34 this.passwd = passwd; 35 } 36 public Integer getGrade() { 37 return grade; 38 } 39 public void setGrade(Integer grade) { 40 this.grade = grade; 41 } 42 private String name; 43 private String email; 44 private java.util.Date hiredate; 45 private Float salary; 46 public Integer getId() { 47 return id; 48 } 49 public void setId(Integer id) { 50 this.id = id; 51 } 52 public String getName() { 53 return name; 54 } 55 public void setName(String name) { 56 this.name = name; 57 } 58 public String getEmail() { 59 return email; 60 } 61 public void setEmail(String email) { 62 this.email = email; 63 } 64 public java.util.Date getHiredate() { 65 return hiredate; 66 } 67 public void setHiredate(java.util.Date hiredate) { 68 this.hiredate = hiredate; 69 } 70 public Float getSalary() { 71 return salary; 72 } 73 public void setSalary(Float salary) { 74 this.salary = salary; 75 } 76 77 78 }
Department.hbm.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping package="com.frank.domain"> 5 <class name="Department" table="department" lazy="true"> 6 <!-- 主键 --> 7 <id name="id" type="java.lang.Integer"> 8 <generator class="native" /> 9 </id> 10 <property name="name" type="java.lang.String"> 11 <column name="name" length="32" /> 12 </property> 13 <set name="emps" lazy="true"> 14 <key column="department_id" /> 15 <one-to-many class="com.frank.domain.Employee" /> 16 </set> 17 </class> 18 </hibernate-mapping>
Employee.hbm.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <hibernate-mapping package="com.frank.domain"> 5 <class name="Employee" table="employee" lazy="true"> 6 <!-- 主键 --> 7 <id name="id" type="java.lang.Integer"> 8 <generator class="native"/> 9 </id> 10 <property name="passwd" type="java.lang.String"> 11 <column name="passwd" length="32"></column> 12 </property> 13 <property name="grade" type="java.lang.Integer"> 14 <column name="grade" length="3"></column> 15 </property> 16 <property name="email" type="java.lang.String"> 17 <column name="email" length="64"/> 18 </property> 19 <property name="hiredate" type="java.util.Date"> 20 <column name="hiredate"/> 21 </property> 22 <property name="name" type="java.lang.String"> 23 <column name="name" length="64"/> 24 </property> 25 <property name="salary" type="java.lang.Float"> 26 <column name="salary"/> 27 </property> 28 <many-to-one name="department" column="department_id"></many-to-one> 29 </class> 30 </hibernate-mapping>
com.frank.service.base
BaseService.java
1 package com.frank.service.base; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.SessionFactory; 8 import org.springframework.transaction.annotation.Transactional; 9 10 11 @Transactional 12 public abstract class BaseService implements BaseServiceInter { 13 private SessionFactory sessionfactory; 14 15 public void setSessionfactory(SessionFactory sessionfactory) { 16 System.out.println("###SetsessionFactory#########"); 17 this.sessionfactory = sessionfactory; 18 } 19 20 public SessionFactory getSessionfactory() { 21 return sessionfactory; 22 } 23 24 public BaseService() { 25 System.out.println("Base被实例化"); 26 System.out.println(sessionfactory); 27 } 28 29 /* 30 * (non-Javadoc) 31 * @see com.frank.service.base.BaseServiceInter#findById(java.lang.Class, java.io.Serializable) 32 * 根据id好查找记录 33 */ 34 @Override 35 public Object findById(@SuppressWarnings("rawtypes") Class clazz, Serializable id) { 36 // TODO Auto-generated method stub 37 System.out.println("********findById*********"); 38 System.out.println(sessionfactory); 39 return sessionfactory.getCurrentSession().load(clazz, id); 40 } 41 /** 42 * 删除记录 43 */ 44 @Override 45 public void deleteObj(Object obj) { 46 // TODO Auto-generated method stub 47 sessionfactory.getCurrentSession().delete(obj); 48 } 49 50 /** 51 * 保存到数据库 52 */ 53 @Override 54 public void addTo( Object obj) { 55 // TODO Auto-generated method stub 56 sessionfactory.getCurrentSession().save(obj); 57 58 } 59 public Object uniqueQuery(String hql, Object[] parameters) { 60 // TODO Auto-generated method stub 61 Query query=this.sessionfactory.getCurrentSession().createQuery(hql); 62 //给?赋值 63 if(parameters!=null && parameters.length>0){ 64 for(int i=0;i<parameters.length;i++){ 65 query.setParameter(i, parameters[i]); 66 } 67 } 68 69 return query.uniqueResult(); 70 } 71 //统一查询方法(hql) 72 public List executeQuery(String hql, Object[] parameters) { 73 // TODO Auto-generated method stub 74 75 Query query=this.sessionfactory.getCurrentSession().createQuery(hql); 76 77 //注入?值 78 if(parameters!=null && parameters.length>0){ 79 for(int i=0;i<parameters.length;i++){ 80 query.setParameter(i, parameters[i]); 81 82 } 83 } 84 85 return query.list(); 86 } 87 88 }
BaseServiceInter.java
1 package com.frank.service.base; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 public interface BaseServiceInter { 7 public List executeQuery(String hql, Object[] parameters); 8 9 public Object findById(Class clazz, Serializable id); 10 11 public void deleteObj(Object obj); 12 13 public Object uniqueQuery(String hql, Object[] parameters); 14 15 public void addTo(Object obj); 16 17 }
com.frank.service.imp
DepartmentService.java
1 package com.frank.service.imp; 2 3 import java.util.List; 4 5 6 import com.frank.domain.Department; 7 import com.frank.service.base.BaseService; 8 import com.frank.service.inter.DepartmentServiceInter; 9 10 public class DepartmentService extends BaseService implements DepartmentServiceInter { 11 12 13 14 public List<Department> showDepartment() { 15 // TODO Auto-generated method stub 16 System.out.println("#########showDepartment##########"); 17 String hql="from Department"; 18 @SuppressWarnings("unchecked") 19 List<Department> deps=(List<Department>) this.executeQuery(hql, null); 20 21 return deps; 22 } 23 24 25 }
EmployeeService.java
1 package com.frank.service.imp; 2 3 4 import java.util.List; 5 6 import org.hibernate.SessionFactory; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import com.frank.domain.Employee; 10 import com.frank.service.base.BaseService; 11 import com.frank.service.inter.EmployeeServiceInter; 12 13 @Transactional 14 public class EmployeeService extends BaseService implements EmployeeServiceInter { 15 16 17 public List<Employee> showEmployee() { 18 // TODO Auto-generated method stub 19 System.out.println("#########showEmployee##########"); 20 String hql="from Employee"; 21 22 @SuppressWarnings("unchecked") 23 List<Employee> emps=(List<Employee>) this.executeQuery(hql, null); 24 25 return emps; 26 } 27 28 29 30 @Override 31 public void updateEmployee(Employee e) { 32 // TODO Auto-generated method stub 33 34 } 35 36 37 38 @SuppressWarnings("unchecked") 39 @Override 40 public Employee chackEmployee(Employee e) { 41 // TODO Auto-generated method stub 42 String hql="from Employee where id=? and passwd=?"; 43 Object []parameters={e.getId(),e.getPasswd()}; 44 Employee emp= (Employee) this.uniqueQuery(hql, parameters); 45 System.out.println("#########chackEmployee##########"); 46 if(emp!=null){ 47 return emp; 48 } 49 else 50 return null; 51 } 52 53 54 }
com.frank.service.inter
DepartmentServiceInter.java
1 package com.frank.service.inter; 2 3 import java.util.List; 4 5 import com.frank.domain.Department; 6 import com.frank.service.base.BaseServiceInter; 7 8 public interface DepartmentServiceInter extends BaseServiceInter { 9 10 public List<Department> showDepartment() ; 11 }
EmployeeServiceInter.java
1 package com.frank.service.inter; 2 3 import java.util.List; 4 5 import com.frank.domain.Employee; 6 import com.frank.service.base.BaseServiceInter; 7 8 public interface EmployeeServiceInter extends BaseServiceInter { 9 10 //显示所有雇员 11 public List<Employee> showEmployee(); 12 //更新 13 public void updateEmployee(Employee e); 14 15 //验证 16 public Employee chackEmployee(Employee e); 17 18 }
com.frank.Test
该包为在编码过程中写的测试类,不再贴代码。。。
com.frank.web.action
AddAndGoEmployee.java
1 package com.frank.web.action; 2 3 4 import java.util.List; 5 6 import org.apache.struts2.ServletActionContext; 7 8 import com.frank.domain.Department; 9 import com.frank.domain.Employee; 10 import com.frank.service.inter.DepartmentServiceInter; 11 import com.frank.service.inter.EmployeeServiceInter; 12 import com.opensymphony.xwork2.ActionSupport; 13 14 public class AddAndGoEmployee extends ActionSupport { 15 16 /** 17 * 18 */ 19 private static final long serialVersionUID = 1L; 20 private EmployeeServiceInter employeeService; 21 private DepartmentServiceInter departmentService; 22 23 private String name; 24 private String passwd; 25 private String email; 26 private String salary; 27 private Integer department_id; 28 private String grade; 29 private Integer employee_id; 30 31 32 33 34 public Integer getEmployee_id() { 35 return employee_id; 36 } 37 public void setEmployee_id(Integer employee_id) { 38 this.employee_id = employee_id; 39 } 40 public String getName() { 41 return name; 42 } 43 public void setName(String name) { 44 this.name = name; 45 } 46 public String getPasswd() { 47 return passwd; 48 } 49 public void setPasswd(String passwd) { 50 this.passwd = passwd; 51 } 52 public String getEmail() { 53 return email; 54 } 55 public void setEmail(String email) { 56 this.email = email; 57 } 58 public String getSalary() { 59 return salary; 60 } 61 public void setSalary(String salary) { 62 this.salary = salary; 63 } 64 public Integer getDepartment_id() { 65 return department_id; 66 } 67 public void setDepartment_id(Integer department_id) { 68 this.department_id = department_id; 69 } 70 public String getGrade() { 71 return grade; 72 } 73 public void setGrade(String grade) { 74 this.grade = grade; 75 } 76 public void setDepartmentService(DepartmentServiceInter departmentService) { 77 this.departmentService = departmentService; 78 } 79 public void setEmployeeService(EmployeeServiceInter employeeService) { 80 this.employeeService = employeeService; 81 } 82 //删除雇员 83 public String delete() throws Exception { 84 // TODO Auto-generated method stub 85 System.out.println("#########deleteEmployee###########"); 86 System.out.println(this.employee_id); 87 // employeeService 88 return SUCCESS; 89 } 90 //调用service,添加到数据库 91 public String addTo() throws Exception { 92 // TODO Auto-generated method stub 93 Employee e=new Employee(passwd,Integer.parseInt(grade),name 94 ,email,new java.util.Date(),Float.parseFloat(salary)); 95 Department dep=(Department) departmentService.findById(Department.class, department_id); 96 e.setDepartment(dep ); 97 98 employeeService.addTo(e); 99 System.out.println("#########addTo##########"); 100 // System.out.println(email+" "+name+" "+grade+" "+passwd+" "+salary); 101 // System.out.println(department_id); 102 return SUCCESS; 103 } 104 //跳转到输入信息界面addEmployee.jsp 105 public String goAdd() throws Exception { 106 // TODO Auto-generated method stub 107 System.out.println("#######goAddEmployee#########"); 108 //获取所有部门 109 List<Department> deps= departmentService.showDepartment(); 110 ServletActionContext.getRequest().setAttribute("deps", deps); 111 return INPUT; 112 } 113 114 }
LoginAndLogout.java
1 package com.frank.web.action; 2 3 4 5 import com.frank.domain.Employee; 6 import com.frank.service.inter.EmployeeServiceInter; 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionSupport; 9 10 public class LoginAndLogout extends ActionSupport{ 11 private String id; 12 private String passwd; 13 private EmployeeServiceInter employeeService; 14 15 public void setEmployeeService(EmployeeServiceInter employeeService) { 16 this.employeeService = employeeService; 17 } 18 public String getId() { 19 return id; 20 } 21 public void setId(String id) { 22 this.id = id; 23 } 24 public String getPasswd() { 25 return passwd; 26 } 27 public void setPasswd(String passwd) { 28 this.passwd = passwd; 29 } 30 /** 31 * 32 */ 33 private static final long serialVersionUID = 1L; 34 35 public String Login() throws Exception { 36 // TODO Auto-generated method stub 37 // //获取spring容器 38 // ServletContext sc = ServletActionContext.getServletContext(); 39 // WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(sc); 40 // //得到service类 41 // EmployeeServiceInter employeeService=(EmployeeServiceInter) ctx.getBean("employeeService"); 42 43 Employee e=new Employee(); 44 e.setId(Integer.parseInt(this.getId())); 45 e.setPasswd(this.getPasswd()); 46 e=employeeService.chackEmployee(e); 47 System.out.println("##########Login############"); 48 System.out.println(this.getPasswd()); 49 if(e!=null){ 50 ActionContext.getContext().getSession().put("loginuser", e); 51 return SUCCESS; 52 } 53 else 54 return LOGIN; 55 } 56 public String Logout() throws Exception { 57 // TODO Auto-generated method stub 58 System.out.println("#########Logout#############"); 59 //清空session 60 ActionContext.getContext().getSession().clear(); 61 return LOGIN; 62 } 63 64 65 }
ShowEmployee.java
1 package com.frank.web.action; 2 3 import java.util.List; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.frank.domain.Employee; 8 import com.frank.service.inter.EmployeeServiceInter; 9 import com.opensymphony.xwork2.ActionSupport; 10 11 public class ShowEmployee extends ActionSupport { 12 13 private EmployeeServiceInter employeeService; 14 15 public void setEmployeeService(EmployeeServiceInter employeeService) { 16 this.employeeService = employeeService; 17 } 18 19 /** 20 * 21 */ 22 private static final long serialVersionUID = 1L; 23 24 /** 25 * 调用service获得所有employee,放入request,跳转到showEmployee.jsp 26 */ 27 @Override 28 public String execute() throws Exception { 29 // TODO Auto-generated method stub 30 System.out.println("#########showEmployee##########"); 31 List<Employee> emps= employeeService.showEmployee(); 32 ServletActionContext.getRequest().setAttribute("emps", emps); 33 for(Employee e:emps) 34 { 35 System.out.println(e.getName()); 36 37 } 38 return SUCCESS; 39 } 40 41 }
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" 5 xmlns:tx="http://www.springframework.org/schema/tx"> 6 <!-- 测试类 <bean id="testService" class="com.frank.Test.TestService"> <property 7 name="name" value="frank" /> </bean> --> 8 9 <!-- 配置数据源 --> 10 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 11 <property name="url" value="jdbc:mysql://127.0.0.1:3306/spdb1"> 12 </property> 13 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 14 <property name="username" value="root"></property> 15 <property name="password" value="frank1994"></property> 16 </bean> 17 <!-- 配置会话工厂 --> 18 <bean id="sessionFactory" 19 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 20 <property name="dataSource"> 21 <ref bean="dataSource" /> 22 </property> 23 <property name="mappingResources"> 24 <list> 25 <value>com/frank/domain/Employee.hbm.xml</value> 26 <value>com/frank/domain/Department.hbm.xml</value> 27 </list> 28 </property> 29 <property name="hibernateProperties"> 30 <props> 31 <prop key="hibernate.show_sql" >true</prop> 32 <prop key="hibernate.format_sql">true</prop> 33 <prop key="hibernate.hbm2ddl.auto"> 34 update 35 </prop> 36 <prop key="hibernate.dialect"> 37 org.hibernate.dialect.MySQLDialect 38 </prop> 39 </props> 40 </property> 41 </bean> 42 43 <!-- 配置service类 --> 44 <bean id="employeeService" class="com.frank.service.imp.EmployeeService" parent="baseService"> 45 <property name="sessionfactory" ref="sessionFactory"></property> 46 </bean> 47 <bean id="departmentService" class="com.frank.service.imp.DepartmentService" parent="baseService"> 48 49 </bean> 50 <bean id="baseService" class="com.frank.service.base.BaseService" abstract="true"> 51 <property name="sessionfactory" ref="sessionFactory"></property> 52 </bean> 53 <!-- 配置action --> 54 <bean id="loginAndLogout" scope="prototype" class="com.frank.web.action.LoginAndLogout"> 55 <property name="employeeService" ref="employeeService"></property> 56 </bean> 57 <bean id="showEmployee" scope="prototype" class="com.frank.web.action.ShowEmployee"> 58 <property name="employeeService" ref="employeeService"></property> 59 </bean> 60 <bean id="addEmployee" scope="prototype" class="com.frank.web.action.AddAndGoEmployee"> 61 <property name="employeeService" ref="employeeService"></property> 62 <property name="departmentService" ref="departmentService"></property> 63 </bean> 64 <!-- 配置事务管理器 --> 65 <bean id="transactionManager" 66 class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 67 <property name="sessionFactory" ref="sessionFactory" /> 68 </bean> 69 <!-- 开启事务管理器 --> 70 <tx:annotation-driven transaction-manager="transactionManager" /> 71 </beans>
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <constant name="struts.objectFactory" value="spring"/> 5 <!-- 处理登录登出 --> 6 <package name="lee" extends="struts-default" namespace="/"> 7 <action name="*Action" class="loginAndLogout" method="{1}"> 8 <result name="success">WEB-INF/content/MainFrame.jsp</result> 9 <result name="login">index.jsp</result> 10 </action> 11 <!-- 准备数据,跳转到showEmployee.jsp --> 12 <action name="showEmployee" class="showEmployee"> 13 <result name="success">WEB-INF/content/showEmployee.jsp</result> 14 </action> 15 <!-- 跳转到addEmployee.jsp/添加雇员/删除雇员/修改雇员 --> 16 <action name="*Employee" class="addEmployee" method="{1}"> 17 <result name="input">WEB-INF/content/addEmployee.jsp</result> 18 <result name="success">/WEB-INF/content/MainFrame.jsp</result> 19 </action> 20 </package> 21 </struts>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 <display-name>sshTest_0.0</display-name> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 <welcome-file>default.html</welcome-file> 12 <welcome-file>default.htm</welcome-file> 13 <welcome-file>default.jsp</welcome-file> 14 </welcome-file-list> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 <context-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:applicationContext.xml</param-value> 21 </context-param> 22 <filter> 23 <filter-name>struts2</filter-name> 24 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 25 </filter> 26 <filter-mapping> 27 <filter-name>struts2</filter-name> 28 <url-pattern>/*</url-pattern> 29 </filter-mapping> 30 <!-- 指定spring的配置文件 --> 31 <context-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>classpath:applicationContext.xml</param-value> 34 </context-param> 35 <!-- 对spring容器进行实例化 --> 36 <listener> 37 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 38 </listener> 39 40 </web-app>
index.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() 5 + "://" 6 + request.getServerName() 7 + ":" 8 + request.getServerPort() 9 + path + "/"; 10 %> 11 12 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 13 <html> 14 <head> 15 <base href="<%=basePath%>"> 16 17 <title>My JSP ‘MainFrame.jsp‘ starting page</title> 18 19 <meta http-equiv="pragma" content="no-cache"> 20 <meta http-equiv="cache-control" content="no-cache"> 21 <meta http-equiv="expires" content="0"> 22 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 23 <meta http-equiv="description" content="This is my page"> 24 <!-- 25 <link rel="stylesheet" type="text/css" href="styles.css"> 26 --> 27 28 </head> 29 </head> 30 31 <body> 32 33 <h1>管理员登录</h1> 34 <form action="LoginAction" method="post"> 35 <table> 36 <tr> 37 <td>id:</td> 38 <td><input type="text" name="id"></td> 39 </tr> 40 <tr> 41 <td>passwd:</td> 42 <td><input type="password" name="passwd"></td> 43 </tr> 44 <tr> 45 <td><input type="submit" value="登录"></td> 46 <td><input type="reset" value="重置"></td> 47 </tr> 48 </table> 49 </form> 50 </body> 51 </html>
addEmployee.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme() 7 + "://" 8 + request.getServerName() 9 + ":" 10 + request.getServerPort() 11 + path + "/"; 12 %> 13 14 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 15 <html> 16 <head> 17 <base href="<%=basePath%>"> 18 19 <title>My JSP ‘addEmployee.jsp‘ starting page</title> 20 21 <meta http-equiv="pragma" content="no-cache"> 22 <meta http-equiv="cache-control" content="no-cache"> 23 <meta http-equiv="expires" content="0"> 24 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 25 <meta http-equiv="description" content="This is my page"> 26 <!-- 27 <link rel="stylesheet" type="text/css" href="styles.css"> 28 --> 29 30 </head> 31 32 <body> 33 <h1>添加用户</h1> 34 <br> 35 <form action="addToEmployee" method="post"> 36 <table> 37 <tr> 38 <td>name:</td> 39 <td><input type="text" name="name"></td> 40 </tr> 41 <tr> 42 <td>passwd:</td> 43 <td><input type="password" name="passwd"></td> 44 </tr> 45 <tr> 46 <td>email:</td> 47 <td><input type="text" name="email"></td> 48 </tr> 49 <tr> 50 <td>salary:</td> 51 <td><input type="text" name="salary"></td> 52 </tr> 53 <tr> 54 <td>department:</td> 55 <td><select name="department_id"><c:forEach items="${deps }" var="dep"> 56 <option value="${dep.id }">${dep.name }</option> 57 </c:forEach> 58 </select></td> 59 </tr> 60 <tr> 61 <td>grade:</td> 62 <td><input type="text" name="grade"></td> 63 </tr> 64 <tr> 65 <td><input type="submit" value="提交"></td> 66 <td><input type="reset" value="重填"></td> 67 </tr> 68 </table> 69 </form> 70 </body> 71 </html>
MainFrame.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() 5 + "://" 6 + request.getServerName() 7 + ":" 8 + request.getServerPort() 9 + path + "/"; 10 %> 11 12 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 13 <html> 14 <head> 15 <base href="<%=basePath%>"> 16 17 <title>My JSP ‘MainFrame.jsp‘ starting page</title> 18 19 <meta http-equiv="pragma" content="no-cache"> 20 <meta http-equiv="cache-control" content="no-cache"> 21 <meta http-equiv="expires" content="0"> 22 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 23 <meta http-equiv="description" content="This is my page"> 24 <!-- 25 <link rel="stylesheet" type="text/css" href="styles.css"> 26 --> 27 28 </head> 29 30 <body> 31 <h1>欢迎你${loginuser.name }</h1><br> 32 33 <a href="goAddEmployee">添加雇员</a><br> 34 <a href="showEmployee">显示雇员</a><br> 35 <a href="#">查询雇员</a><br> 36 <a href="#">退出系统</a><br> 37 </body> 38 39 </html>
showEmployee.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() 6 + "://" 7 + request.getServerName() 8 + ":" 9 + request.getServerPort() 10 + path + "/"; 11 %> 12 13 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 14 <html> 15 <head> 16 <base href="<%=basePath%>"> 17 18 <title>My JSP ‘showEmployee.jsp‘ starting page</title> 19 20 <meta http-equiv="pragma" content="no-cache"> 21 <meta http-equiv="cache-control" content="no-cache"> 22 <meta http-equiv="expires" content="0"> 23 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 24 <meta http-equiv="description" content="This is my page"> 25 <!-- 26 <link rel="stylesheet" type="text/css" href="styles.css"> 27 --> 28 29 </head> 30 31 <body> 32 欢迎你${loginuser.name }! 33 <!-- <h2><a href="#">返回上一级</a></h2> --> 34 <h2> 35 <a href="LogoutAction">退出系统</a> 36 </h2> 37 <table> 38 <tr> 39 <td>雇员姓名</td> 40 <td>雇员id</td> 41 <td>雇员邮箱</td> 42 <td>雇员级别</td> 43 <td>入职日期</td> 44 <td>工资</td> 45 <td>所属部门</td> 46 </tr> 47 <c:forEach items="${emps }" var="emp"> 48 <tr> 49 <td>${emp.name }</td> 50 <td>${emp.id }</td> 51 <td>${emp.email }</td> 52 <td>${emp.grade }</td> 53 <td>${emp.hiredate }</td> 54 <td>${emp.salary }</td> 55 56 </tr> 57 </c:forEach> 58 </table> 59 </body> 60 </html>
时间: 2024-10-20 08:36:45