在struts-config.xml中配置清单:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="addEmpForm" type="cn.veryedu.form.EmpForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/addEmp" type="cn.veryedu.action.EmpManageAction"
name="addEmpForm" scope="request" parameter="addEmp">
<forward name="queryEmpInfo" path="/queryEmp.do?method=queryEmp"></forward>
</action>
<action path="/queryEmp" type="cn.veryedu.action.EmpManageAction" parameter="queryEmp">
<forward name="queryPage" path="/empManager.jsp"></forward>
</action>
<action path="/initPage" type="cn.veryedu.action.EmpManageAction" parameter="initQueryPage">
<forward name="initPage" path="/queryEmpInfo.jsp"/>
</action>
<action path="/empInfo" type="cn.veryedu.action.EmpManageAction" parameter="queryByCriteria">
<forward name="queryPage" path="/empInfo.jsp"></forward>
</action>
</action-mappings>
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
<message-resources parameter="ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/spring-config/applicationContext.xml"/>
</plug-in>
</struts-config>
在beans.xml中配置清单:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="empManagerAction" class="cn.veryedu.action.EmpManageAction" abstract="true">
<property name="empDAO">
<ref local="empDAO"/>
</property>
<property name="deptDAO">
<ref local="deptDAO"/>
</property>
</bean>
<bean name="/addEmp" parent="empManagerAction">
</bean>
<bean name="/queryEmp" parent="empManagerAction">
</bean>
<bean name="/initPage" parent="empManagerAction">
</bean>
<bean name="/empInfo" parent="empManagerAction">
</bean>
<bean id="empDAO" class="cn.veryedu.dao.EmpDAO">
</bean>
<bean id="deptDAO" class="cn.veryedu.dao.DeptDAO">
</bean>
</beans>
代码使用中示例:
package cn.veryedu.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import cn.veryedu.dao.DAO;
import cn.veryedu.dao.DeptDAO;
import cn.veryedu.dao.EmpDAO;
import cn.veryedu.dao.IEmpDAO;
import cn.veryedu.entity.Dept;
import cn.veryedu.entity.Emp;
import cn.veryedu.form.EmpForm;
public class EmpManageAction extends MappingDispatchAction {
private IEmpDAO empDAO = null;
private DAO deptDAO = null;
public void setEmpDAO(IEmpDAO empDAO) {
this.empDAO = empDAO;
}
public void setDeptDAO(DAO deptDAO) {
this.deptDAO = deptDAO;
}
public ActionForward addEmp(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
EmpForm empForm = (EmpForm) form;
Emp emp = new Emp();
BeanUtils.copyProperties(emp, empForm);
Dept dept = new Dept();
dept.setDeptno(empForm.getDeptno());
emp.setDept(dept);
empDAO.add(emp);
return mapping.findForward("queryEmpInfo");
}
public ActionForward queryEmp(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
List result = empDAO.findByAll();
request.setAttribute("empList", result);
System.out.println("Action执行那个结束......");
return mapping.findForward("queryPage");
}
public ActionForward initQueryPage(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
List jobList = empDAO.findJob();
List deptList = deptDAO.findByAll();
request.setAttribute("jobList", jobList);
request.setAttribute("deptList", deptList);
return mapping.findForward("initPage");
}
public ActionForward queryByCriteria(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String job = request.getParameter("job");
String salLow = request.getParameter("sallow");
String salHeight = request.getParameter("salheight");
String deptno = request.getParameter("deptno");
DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
addJob(dc, job);
addSal(dc, salLow, salHeight);
addDeptno(dc, deptno);
List result = empDAO.findByCriteria(dc);
request.setAttribute("empList", result);
return mapping.findForward("queryPage");
}
private void addJob(DetachedCriteria dc, String job) {
if(job != null && !job.equals("")) {
dc.add(Restrictions.eq("job", job));
}
}
private void addSal(DetachedCriteria dc, String sallow, String salheight) {
if(sallow != null && !sallow.equals("") && salheight != null && !salheight.equals("")){
dc.add(Restrictions.ge("sal", Double.parseDouble(sallow)));
dc.add(Restrictions.le("sal", Double.parseDouble(salheight)));
}
}
private void addDeptno(DetachedCriteria dc, String deptno) {
if(deptno != null && !deptno.equals("")) {
dc = dc.createCriteria("dept");
dc.add(Restrictions.eq("deptno", Long.parseLong(deptno)));
}
}
public ActionForward queryById(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return null;
}
public ActionForward deleteById(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return null;
}
}