SSHSpring 整合 Struts
Posted Kikyo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSHSpring 整合 Struts相关的知识,希望对你有一定的参考价值。
添加 spring-struts-3.2.9.RELEASE.jar
struts-config.xml 添加
<controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"></set-property> </controller>
applicationContext.xml
<!--配置 action--> <bean name="/login" class="k.action.LoginAction" scope="prototype"/> <bean name="/employee" class="k.action.EmployeeAction" scope="prototype"/>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="employeeForm" type="k.form.EmployeeForm"> <form-property name="id" type="java.lang.Integer"></form-property> <form-property name="name" type="java.lang.String"></form-property> <form-property name="password" type="java.lang.String"></form-property> </form-bean> </form-beans> <global-forwards> <forward name="ok" path="/WEB-INF/jsp/ok.jsp"></forward> <forward name="err" path="/WEB-INF/jsp/err.jsp"></forward> </global-forwards> <action-mappings> <action name="employeeForm" path="/login" parameter="action" type="k.action.LoginAction" scope="request" attribute="employeeForm" input="index.jsp" validate="false"> <forward name="main" path="/WEB-INF/jsp/main.jsp"></forward> <forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward> </action> <action name="employeeForm" path="/employee" parameter="action" type="k.action.EmployeeAction" scope="request" attribute="employeeForm" input="index.jsp" validate="false"> <forward name="addEmployeeUI" path="/WEB-INF/jsp/addEmployeeUI.jsp"></forward> <forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward> </action> </action-mappings> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"></set-property> </controller> </struts-config>
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- 加载db.properties文件 --> <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations" value="classpath*:*.properties"/> </bean> <!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> <property name="initialSize" value="${initialSize}"></property> <property name="maxActive" value="${maxActive}"></property> <property name="maxIdle" value="${maxIdle}"></property> <property name="minIdle" value="${minIdle}"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- //加载实体类的映射文件位置及名称 --> <property name="mappingLocations" value="classpath:k/bean/*.hbm.xml"></property> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!--事务--> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--开启注解配置--> <context:annotation-config/> <!--配置 action--> <bean name="/login" class="k.action.LoginAction" scope="prototype"/> <bean name="/employee" class="k.action.EmployeeAction" scope="prototype"/> <!--配置 Service--> <bean name="testService" class="k.service.TestService"/> <bean name="employeeService" class="k.service.impl.EmployeeServiceImpl"/> <bean name="departmentService" class="k.service.impl.DepartmentServiceImpl"/> </beans>
LoginAction
public class LoginAction extends DispatchAction { @Resource private EmployeeService employeeService; public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("loginJsp"); } public ActionForward doLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { EmployeeForm employeeOld = (EmployeeForm) form; Employee employee = new Employee(Integer.parseInt(employeeOld.getId()), employeeOld.getPassword()); employee = employeeService.checkEmployee(employee); if (employee != null) { request.getSession().setAttribute("employee", employee); return mapping.findForward("main"); } return mapping.findForward("err"); } public ActionForward loginOut(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return super.execute(mapping, form, request, response); } }
以上是关于SSHSpring 整合 Struts的主要内容,如果未能解决你的问题,请参考以下文章