将spring struts项目整合到一个项目中
2012-08-17 14:08
435 查看
在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; } }
相关文章推荐
- 遇到一个问题,struts 与spring 整合
- mybatis整合struts,spring的一个细节问题
- 关于struts与spring整合的一个小案例入门
- MyEclipse创建一个struts+spring+ibatis整合的工程
- 一个 struts2+ hibernate3+spring 整合的示例
- 一个简单Spring+Hibernate+Struts2.0+Ajax整合获取客户端IP、URL和请求时间
- struts-spring-mybatis整合项目搭建
- 总结做一个Hibernate 或Spring或Struts项目需要哪些基本的包
- 在项目中整合Struts与Spring
- Struts+Spring+Hibernate项目整合AJAX+JSON
- struts-spring-mybatis整合项目搭建
- Spring+Mybatis+Velocity整合(1):建立一个Maven项目
- 一个简单的SpringBoot整合Mybatis项目
- struts-2.3+spring-4.0+hibernate-4.0整合项目
- 整合Struts2+Spring2.5的时候遇到一个问题
- Struts2.3+Spring2.5.6+JPA(Hibernate3)项目移植入WebLogic12容器,并整合入MyEclipse10 图文详解
- 《Lucene、webservice、(svn_ant_maven)、DWR、Freemarker、POI视频教程》更新了Spring、JAXWs的整合并且完成了一个小项目
- 结合Struts+Spring+jdbc做的一个小项目
- 对于web项目单独使用struts,不和Spring整合的情况。
- Maven项目 Struts和Spring整合