一.在Spring环境下配置Struts1.x
导入Struts1.x的jar文件:
修改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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/javaee/jsp " version="2.5"> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 使用spring解决struts1.3乱码问题 --> <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> <!-- 使用spring解决hibernate因session关闭导致的延迟加载例外问题 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</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> </web-app>
以上:
1.对Spring容器进行了配置。
2.对Spring容器进行了实例化。
3.利用Spring的org.springframework.web.context.ContextLoaderListener
解决Struts1.x的乱码问题。
4.org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
解决hibernate因session关闭导致的延迟加载例外问题
5.配置struts1.x
WEB-INF下创建struts-config.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="addPersonForm" type="com.zyy.service.form.AddPersonForm"> </form-bean> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> </global-forwards> <action-mappings> <action path="/persons" type="com.zyy.service.action.PersonAction" > <forward name="list" path="/WEB-INF/jsp/personlist.jsp"></forward> </action> <action path="/person/add" type="com.zyy.service.action.AddPerson" parameter="method" validate="false" scope="request" name="addPersonForm"> </action> </action-mappings> <!--在struts配置文件中添加进spring的请求控制器 该请法语控制器会先根据action的path属性值到spring 容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求 --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller> <message-resources parameter="resource.MessageResources"/> </struts-config>
以上:
在Struts配置中添加了Spring 的请求控制器:该请法语控制器会先根据action的path属性值
到spring容器中寻找跟该属性值同名的bean。如果寻找到即使用该bean处理用户请求
Spring的beans.xml文件不需要修改。
由于Struts配置文件的需要,在com.zyy.service.form路径下,建立一个
AddPersonForm:
package com.zyy.service.form; import org.apache.struts.action.ActionForm; /** * Created by CaMnter on 2014/8/26. */ public class AddPersonForm extends ActionForm { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
com.zyy.service.action路径下,建立AddPerson 和 PersonAction:
PersonAction
package com.zyy.service.action; import com.zyy.service.PersonService; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by CaMnter on 2014/8/24. */ /** * struts 的Action 交给Spring管理 * bean的名字和action 中 path 必须一致 */ @Service("/persons") public class PersonAction extends Action { @Autowired private PersonService personService; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { /* WebApplicationContext webApplicationContext = WebApplicationContextUtils .getWebApplicationContext(this.getServlet().getServletContext()); this.personService = (PersonService) webApplicationContext.getBean("personServiceBean"); */ request.setAttribute("persons", this.personService.getPersons()); return mapping.findForward("list"); } }
这个Action要交给Spring容器管理的话,那么使用注解和自动扫描对这个action的bean
命名 必须和 Struts配置文件中action 的path一致对应(<action path="/persons"
type="com.zyy.service.action.PersonAction" >),所以这些定义的注解是
Service("/persons")。
AddPerson
package com.zyy.service.action; import com.zyy.service.PersonService; import com.zyy.service.form.AddPersonForm; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by CaMnter on 2014/8/26. */ public class AddPerson extends DispatchAction { @Autowired private PersonService personService; public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { AddPersonForm addPersonForm = (AddPersonForm) form; System.out.println(addPersonForm.getName()); //this.personService.save(new Person(addPersonForm.getName())); request.setAttribute("message", "添加成功"); return null; } }
然后在WEN-INF下,创建JSP文件夹 再创建 message.jsp 、 personlist.jsp
和 WEB根目录下的 struts_submit.jsp:
personlist.jsp
<%-- Created by IntelliJ IDEA. User: CaMnter Date: 2014/8/23 Time: 0:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title></title> </head> <body> <c:forEach items="${persons}" var="person"> <h4>id=${person.id} name=${person.name}<h4> </c:forEach> </body> </html>
<%-- Created by IntelliJ IDEA. User: CaMnter Date: 2014/8/23 Time: 0:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <h4>${message}</h4> </body> </html>
struts_submit.jsp
<%-- Created by IntelliJ IDEA. User: CaMnter Date: 2014/8/26 Time: 12:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %> <html> <head> <title></title> </head> <body> <html:form action="/person/add" method="post"> 名称:<html:text property="name"></html:text> <input type="hidden" name="method" value="add"> <input type="submit" value="提交"> </html:form> </body> </html>
二.测试
输入http://127.0.0.1:8080/SSH/persons.do:
在WEB跟目录下找到: