转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广
该篇主要编写整合Struts1.3.8的配置代码。Spring与Struts的整合,主要是用Spring容器来管理struts的action。
加入Struts1.3.8的jar文件:
在classpath下,加入struts-1.3.8-all/struts-1.3.8/lib下除antlr-2.7.2.jar(该jar文件spring中已有)外的全部jar文件,如下图所示:
修改web.xml文件,修改后的代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 对Spring进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spirng解决乱码 --> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置struts --> <servlet> <servlet-name>struts</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>struts</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
在WEB-INF下创建struts配置文件struts-config.xml,其代码如下:
<!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="studentForm" type="com.zyg.ssj.web.formbean.StudentForm"></form-bean> </form-beans> <action-mappings> <action path="/student/list" validate="false"> <forward name="list" path="/WEB-INF/pages/studentlist.jsp"/> </action> <action path="/student/manage" parameter="method" validate="false" scope="request" name="studentForm"> <forward name="message" path="/WEB-INF/pages/message.jsp"></forward> </action> </action-mappings> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller> </struts-config>
<controller> <set-property property="processorClass" value="org.springframework.web.struts .DelegatingRequestProcessor"/> </controller>
<?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" 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/context http://www.springframework.org/schema/context/spring-context-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"> <context:annotation-config/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="zyg_db"></property> </bean> <bean id="txManger" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="txManger"/> <bean id="studentDao" class="com.zyg.ssj.dao.jpa.StudentDaoImpl"/> <bean id="studentService" class="com.zyg.ssj.service.impl.StudentServiceImpl"/> <!-- Spring 管理struts的action --> <bean name="/student/list" class="com.zyg.ssj.web.action.StudentAction"/> <bean name="/student/manage" class="com.zyg.ssj.web.action.StudentManageAction"/> </beans>
至此,spring整合struts的相关配置文件和web.xml文件编写完毕。下一篇编写FormBean和action。