ssh(spring+struts2+hibernate)整合

--    	配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>struts2_deptsystem</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
     <!--  struts2核心 --> 
	<filter>
		<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


--    配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- - Application context definition for JPetStore's business layer. - Contains 
	bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml 
	(see web.xml's "contextConfigLocation"). -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<!-- 配置数据源   -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url"
			value="jdbc:oracle:thin:@192.168.0.110:1521:ites" />
		<property name="username" value="scott" />
		<property name="password" value="tiger" />
	</bean>



	<!-- 配置hibernate的sessionFactory对象 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
                <!--  数据库方言 -- >
				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	

	<!-- 配置Action scope="prototype 必须要有,否则会出现页面‘卡死’状态。 Spring默认的是单例模式,每次只能生成一个对象,所以propertype表示生成多个对象。 -->
	<bean id="deptAction" class="kcit.rj2.deptsystem.action.DeptAction" scope="prototype">
		<property name="deptService" ref="deptService" />
	</bean>
	
	<bean id="empAction" class="kcit.rj2.deptsystem.action.EmpAction" scope="prototype">
		<property name="empService" ref="empService" />
		<property name="deptService" ref="deptService"/>
	</bean>
	
	<!-- 配置service -->
	<bean id="deptService" class= "kcit.rj2.deptsystem.service.impl.DeptServiceImpl">
		<property name="deptDao" ref="deptDao"/>
	</bean>
	
	<bean id="empService" class= "kcit.rj2.deptsystem.service.impl.EmpServiceImpl">
		<property name="empDao" ref="empDao"/>
	</bean>
	
	<!-- 配置Dao -->
	<bean id="deptDao" class="kcit.rj2.deptsystem.dao.impl.DeptDaoImpl">
		<property name="sessionFactory" ref = "sessionFactory"/>
	</bean>
	
	<bean id="empDao" class="kcit.rj2.deptsystem.dao.impl.EmpDaoImpl">
		<property name="sessionFactory" ref = "sessionFactory"/>
	</bean>
</beans>


--    配置dept.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="kcit.rj2.deptsystem.bean">

	<class name="DeptBean" table="DEPT_TEMP_HWL">
		<id name="deptId" type="java.lang.Integer">
			<column name="DEPTNO" />
			<generator class="native" />
		</id>
		<property name="deptName" type="java.lang.String">
			<column name="DNAME" length="14" />
		</property>
		<property name="addr" type="java.lang.String">
			<column name="LOC" length="13" />
		</property>
	</class>
</hibernate-mapping>


--    配置emp.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="kcit.rj2.deptsystem.bean">

	<class name="EmpBean" table="EMP_TEMP_HWL">
		<id name="empId" type="java.lang.Integer">
			<column name="EMPNO" />
			<generator class="native" />
		</id>
		<property name="empName" type="java.lang.String">
			<column name="ENAME"  />
		</property>
		<property name="empJob" type="java.lang.String">
			<column name="JOB" />
		</property>
		<property name="empMgr" type="java.lang.Integer">
			<column name="MGR"  />
		</property>
		<property name="empHiredate" type="java.util.Date">
			<column name="HIREDATE" />
		</property>
		<property name="empSal" type="java.lang.Integer">
			<column name="SAL"  />
		</property>
		<property name="empComm" type="java.lang.Integer">
			<column name="COMM"  />
		</property>
		<many-to-one fetch = "join" name="dept" lazy="false" class="DeptBean" column="DEPTNO"></many-to-one>
	</class>
</hibernate-mapping>


--    hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<mapping resource="kcit/rj2/deptsystem/bean/dept.hbm.xml" />
		<mapping resource="kcit/rj2/deptsystem/bean/emp.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 deptList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>deptList</title>
</head>
<body style="background:url(${pageContext.request.contextPath}/framework/image/530906.jpg) no-repeat fixed; ">
<table border='0'>
	<tr>
		<td><a href="/struts2_deptsystem/deptview/deptAdd.jsp">添加</a>
	</tr>
	<tr>
		<td>部门ID</td>
		<td>部门名称</td>
		<td>部门地址</td>
		<td>操作</td>
	</tr>
	<s:iterator value="list">
		<tr>
			<td><s:property value="deptId" /></td>
			<td><s:property value="deptName" /></td>
			<td><s:property value="addr" /></td>
			<td><a href="dept_toUpdate.action?deptId=<s:property value="deptId" />">修改</a>&nbsp;
			<a href="dept_delete.action?deptId=<s:property value="deptId" />">删除</a></td>
		</tr>
	</s:iterator>

</table>
</body>
</html>

 deptAdd.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>deptAdd</title>
</head>
<body
	style="background:url(${pageContext.request.contextPath}/framework/image/476362.jpg)">
<s:form action="dept_insert.action" method="post">
	<table>
		<tr>
			<!-- 显示校验信息 -->
			<td><span style="color: #ff0000;"><s:fielderror /></span></td>
		</tr>
		<tr>
			<td><s:textfield key="deptId"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="deptName"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="addr"></s:textfield></td>
		</tr>
		<tr>
			<td><s:submit value="提交" /></td>
		</tr>
	</table>
</s:form>
</body>
</html>

 deptUpdate.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>deptUpdate</title>
</head>
<body
	style="background:url(${pageContext.request.contextPath}/framework/image/476362.jpg)">
<s:form action="dept_alter.action" method="post">
	<table>
		<tr>
			<!-- 显示校验信息 -->
			<td><span style="color: #ff0000;"><s:fielderror /></span></td>
		</tr>
		<tr>
			<td><s:textfield key="deptId" name="dept.deptId"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="deptName" name="dept.deptName"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="addr" name="dept.addr"></s:textfield></td>
		</tr>
		<tr>
			<td><s:submit value="提交" /></td>
		</tr>
	</table>
</s:form>
</body>
</html>

 empList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>deptList</title>
</head>
<body style="background:url(${pageContext.request.contextPath}/framework/image/530906.jpg) no-repeat fixed; ">
<table border='0'>
	<tr>
		<td><a href="emp_toAdd.action">添加</a>
	</tr>
	<tr>
		<td>员工ID</td>
		<td>员工名称</td>
		<td>员工工作</td>
		<td>经理ID</td>
		<td>入职日期</td>
		<td>底薪</td>
		<td>奖金</td>
		<td>部门ID</td>
		<td>操作</td>
	</tr>
	<s:iterator value="list">
		<tr>
			<td><s:property value="empId" /></td>
			<td><s:property value="empName" /></td>
			<td><s:property value="empJob" /></td>
			<td><s:property value="empMgr" /></td>
			<td><s:property value="empHiredate" /></td>
			<td><s:property value="empSal" /></td>
			<td><s:property value="empComm" /></td>
			<td><s:property value="dept.deptId" /></td>
			<td><a href="emp_toUpdate.action?empId=<s:property value="empId" />">修改</a>&nbsp;
			<a href="emp_delete.action?empId=<s:property value="empId" />">删除</a></td>
		</tr>
	</s:iterator>

</table>
</body>
</html>

 empAdd.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>empAdd</title>
</head>
<body style="background:url(${pageContext.request.contextPath}/framework/image/476362.jpg)">
<s:form action="emp_insert.action" method="post">
	<table>
		<tr><!-- 显示校验信息 -->
			<td><span style="color: #ff0000;"><s:fielderror /></span></td>
		</tr>
		<tr>
			<td><s:textfield key="empId" ></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empName"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empJob"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empMgr"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empSal"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empComm"></s:textfield></td>
		</tr>
		<tr>
			<td><select name="dept">
				<s:iterator value="deptList" >
					<option value="<s:property value="deptId" />"><s:property
						value="deptName" />
				</s:iterator>
			</select></td>
		</tr>
		<tr>
			<td><s:submit value="提交" /></td>
		</tr>
	</table>
</s:form>
</body>
</html>

 empUpdate.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>empUpdate</title>
</head>
<body style="background:url(${pageContext.request.contextPath}/framework/image/476362.jpg)">
<s:form action="emp_alter.action" method="post">
	<table>
		<tr><!-- 显示校验信息 -->
			<td><span style="color: #ff0000;"><s:fielderror /></span></td>
		</tr>
		<tr>
			<td><s:textfield key="empId" name="emp.empId"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empName"  name="emp.empName"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empJob" name="emp.empJob"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empMgr" name="emp.empMgr"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empSal" name="emp.empSal"></s:textfield></td>
		</tr>
		<tr>
			<td><s:textfield key="empComm" name="emp.empComm"></s:textfield></td>
		</tr>
		<tr>
			<td><select name="dept">
				<s:iterator value="deptList" >
					<option value="<s:property value="deptId" />"><s:property
						value="deptName" />
				</s:iterator>
			</select></td>
		</tr>
		<tr>
			<td><s:submit value="提交" /></td>
		</tr>
	</table>
</s:form>
</body>
</html>

相应Action类 

DeptAction.java

package kcit.rj2.deptsystem.action;

import java.util.ArrayList;
import java.util.List;

import kcit.rj2.deptsystem.bean.DeptBean;
import kcit.rj2.deptsystem.service.IDeptService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DeptAction extends ActionSupport implements ModelDriven<DeptBean> {

	private static final long serialVersionUID = 1L;
	private DeptBean dept = new DeptBean();

	public DeptBean getDept() {
		return dept;
	}

	public void setDept(DeptBean dept) {
		this.dept = dept;
	}

	private List<DeptBean> list = new ArrayList<DeptBean>();

	public List<DeptBean> getList() {
		return list;
	}

	public void setList(List<DeptBean> list) {
		this.list = list;
	}

	private IDeptService<DeptBean> deptService;

	public IDeptService<DeptBean> getDeptService() {
		return deptService;
	}

	public void setDeptService(IDeptService<DeptBean> deptService) {
		this.deptService = deptService;
	}

	@Override
	public DeptBean getModel() {
		return dept;
	}

	public String insert() {
		deptService.add(dept);
		return query();
	}

	public String query() {
		list = deptService.list();
		return "deptList";
	}

	public String toUpdate() {
		dept  = deptService.queryByCondition(dept);
		return "deptUpdate";
	}

	public String alter() {
		deptService.update(dept);
		return query();
	}

	public String delete() {
		deptService.del(dept);
		return query();
	}
	
	public void validateAlter() {
		if(dept.getDeptId() == null){
			addFieldError("deptId","内容不能为空");
		}else if("".equals(dept.getDeptName().trim()) || dept.getDeptName() == null){
			addFieldError("deptName", "内容不能为空");
		}else if("".equals(dept.getAddr()) || dept.getAddr() == null){
			addFieldError("addr", "内容不能为空");
		}
	}
	
	public void validateInsert() {
		if(dept.getDeptId() == null){
			addFieldError("deptId","内容不能为空");
		}else if("".equals(dept.getDeptName().trim()) || dept.getDeptName() == null){
			addFieldError("deptName", "内容不能为空");
		}else if("".equals(dept.getAddr()) || dept.getAddr() == null){
			addFieldError("addr", "内容不能为空");
		}
	}
}

 EmpAction.java

package kcit.rj2.deptsystem.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import kcit.rj2.deptsystem.bean.DeptBean;
import kcit.rj2.deptsystem.bean.EmpBean;
import kcit.rj2.deptsystem.service.IDeptService;
import kcit.rj2.deptsystem.service.IEmpService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class EmpAction extends ActionSupport implements ModelDriven<EmpBean>{

	private static final long serialVersionUID = 1L;
	private EmpBean emp = new EmpBean();
	private List<EmpBean> list = new ArrayList<EmpBean>();
	private List<DeptBean> deptList = new ArrayList<DeptBean>();
	private IEmpService<EmpBean> empService;
	private IDeptService<DeptBean> deptService;
	
	public EmpBean getEmp() {
		return emp;
	}

	public void setEmp(EmpBean emp) {
		this.emp = emp;
	}

	public List<EmpBean> getList() {
		return list;
	}

	public void setList(List<EmpBean> list) {
		this.list = list;
	}

	public List<DeptBean> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<DeptBean> deptList) {
		this.deptList = deptList;
	}

	public IEmpService<EmpBean> getEmpService() {
		return empService;
	}

	public void setEmpService(IEmpService<EmpBean> empService) {
		this.empService = empService;
	}

	public IDeptService<DeptBean> getDeptService() {
		return deptService;
	}

	public void setDeptService(IDeptService<DeptBean> deptService) {
		this.deptService = deptService;
	}

	@Override
	public EmpBean getModel() {
		return emp;
	}
	
	public String query(){
		list = empService.list();
		return  "empList";
	}
	
	public String toAdd(){
		deptList = deptService.list();
		return "empAdd";
	}
	
	public String insert(){
		emp.setEmpHiredate(new Date());
		empService.add(emp);
		return query();
	}
	
	public String toUpdate(){
		deptList = deptService.list();
		emp = empService.queryByCondition(emp);
		return "empUpdate";
	}
	
	public String alter(){
		empService.update(emp);
		return query();
	}
	
	public String delete(){
		empService.del(emp);
		return query();
	}
	
	public void validateAlter() {
		if(emp.getEmpId() == null){
			addFieldError("empId","内容不能为空");
		}else if(emp.getEmpComm() == null){
			addFieldError("empComm","内容不能为空");
		}else if(emp.getEmpHiredate() == null){
			addFieldError("empHiredate","内容不能为空");
		}else if("".equals(emp.getEmpJob().trim()) || emp.getEmpJob() == null){
			addFieldError("empJob","内容不能为空");
		}else if(emp.getEmpMgr() == null){
			addFieldError("empMgr","内容不能为空");
		}else if(emp.getEmpName() == null || "".equals(emp.getEmpName().trim())){
			addFieldError("empName","内容不能为空");
		}else if(emp.getEmpSal() == null){
			addFieldError("empSal","内容不能为空");
		}
	}
	
	public void validateInsert() {
		if(emp.getEmpId() == null){
			addFieldError("empId","内容不能为空");
		}else if(emp.getEmpComm() == null){
			addFieldError("empComm","内容不能为空");
		}else if(emp.getEmpHiredate() == null){
			addFieldError("empHiredate","内容不能为空");
		}else if("".equals(emp.getEmpJob().trim()) || emp.getEmpJob() == null){
			addFieldError("empJob","内容不能为空");
		}else if(emp.getEmpMgr() == null){
			addFieldError("empMgr","内容不能为空");
		}else if(emp.getEmpName() == null || "".equals(emp.getEmpName().trim())){
			addFieldError("empName","内容不能为空");
		}else if(emp.getEmpSal() == null){
			addFieldError("empSal","内容不能为空");
		}
	}
}

 相应的bean类

DeptBean.java

扫描二维码关注公众号,回复: 369112 查看本文章
package kcit.rj2.deptsystem.bean;

public class DeptBean {
	private Integer deptId;
	private String deptName;
	private String addr;

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	@Override
	public String toString() {
		return "DeptBean [deptId=" + deptId + ", deptName=" + deptName
				+ ", addr=" + addr + "]";
	}

}

 EmpBean.java

package kcit.rj2.deptsystem.bean;

import java.util.Date;

public class EmpBean {
	private Integer empId;
	private String empName;
	private String empJob;
	private Integer empMgr;
	private Date empHiredate;
	private Integer empSal;
	private Integer empComm;
	private DeptBean dept = new DeptBean();

	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getEmpJob() {
		return empJob;
	}

	public void setEmpJob(String empJob) {
		this.empJob = empJob;
	}

	public Integer getEmpMgr() {
		return empMgr;
	}

	public void setEmpMgr(Integer empMgr) {
		this.empMgr = empMgr;
	}

	public Date getEmpHiredate() {
		return empHiredate;
	}

	public void setEmpHiredate(Date empHiredate) {
		this.empHiredate = empHiredate;
	}

	public Integer getEmpSal() {
		return empSal;
	}

	public void setEmpSal(Integer empSal) {
		this.empSal = empSal;
	}

	public Integer getEmpComm() {
		return empComm;
	}

	public void setEmpComm(Integer empComm) {
		this.empComm = empComm;
	}

	public DeptBean getDept() {
		return dept;
	}

	public void setDept(DeptBean dept) {
		this.dept = dept;
	}

	@Override
	public String toString() {
		return "EmpBean [empId=" + empId + ", empName=" + empName + ", empJob="
				+ empJob + ", empMgr=" + empMgr + ", empHiredate="
				+ empHiredate + ", empSal=" + empSal + ", empComm=" + empComm
				+ ", dept=" + dept + "]";
	}

}

关联struts.xml

配置dept.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="dept" namespace="/" extends="struts-default">
        <action name="dept_*" method="{1}" class="deptAction">
             <result name="deptList">/deptview/DeptList.jsp</result>
             <result name="deptUpdate">/deptview/deptUpdate.jsp</result>
             <result name="input">/deptview/deptAdd.jsp</result>
        </action>
    </package>
</struts>

 配置emp.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="emp" namespace="/" extends="struts-default">
        <action name="emp_*" method="{1}" class="empAction">
            <result name="empUpdate">/empview/empUpdate.jsp</result>
            <result name="empList">/empview/empList.jsp</result>
            <result name="empAdd">/empview/empAdd.jsp</result>
			<!--    -->
            <result name="input">/empview/empAdd.jsp</result>
        </action>
    </package>
</struts>

 配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.custom.i18n.resources" value="message"></constant>
	<package name="login" namespace="/" extends="struts-default">
		<interceptors>
			<!—自定义拦截器 -->
			<interceptor name="myInterceptor" class="kcit.rj2.deptsystem.interceptors.LoginInterceptor"/>
			<interceptor-stack name="myInterceptorStack">
				<interceptor-ref name="myInterceptor"></interceptor-ref>
				<!— 必须引入struts2默认的拦截器,否则无法得到表单提交的数据。 -->
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<action name="login_*" method="{1}" class="loginAction">
			<interceptor-ref name="myInterceptorStack"></interceptor-ref>
			<result name="framework">/framework/framework.jsp</result>
			<result name="login">/framework/login.jsp</result>
		</action>
	</package>
	<include file="dept.xml" />
	<include file="emp.xml" />
</struts>

 LoginInterceptor.java

package kcit.rj2.deptsystem.interceptors;

import javax.servlet.http.HttpServletRequest;

import kcit.rj2.deptsystem.tools.GetIPTools;
import kcit.rj2.deptsystem.tools.GetMACTools;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor{

	private static final long serialVersionUID = 1L;
	private String ip ;
	private String mac;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		HttpServletRequest req = ServletActionContext.getRequest();
		ip = GetIPTools.getIpAddr(req);
		mac = GetMACTools.getWindows7MACAddress();
		if(ip.equals("127.0.0.1")&& mac.equals("94de800762a4")){
			arg0.invoke();
			return "framework";				
		}
		return "login";
	}
}

 

 

 

 

 

 

 

猜你喜欢

转载自1160514291.iteye.com/blog/2096256