第一步:导入对应jar文件
第二步:
1、在web容器中实例化spring容器
<!-- 指定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容器被存放到ServletContext当中。
第三步:
在web.xml中配置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>
书写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> <action-mappings> <!-- <action path="/person/list" type="cn.itcast.web.action.PersonAction" validate="false"> <forward name="list" path="/WEB-INF/page/personlist.jsp"/> </action> --> <action path="/person/list" validate="false"> <forward name="list" path="/WEB-INF/page/personlist.jsp"/> </action> </action-mappings> <!-- 加入spring请求控制器 --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller> </struts-config>
如果action没有交给spring管理时,我们通过下面语句获取spring容器实例
WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext( this.getServlet().getServletContext());
例子代码:
@Override public ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response) throws Exception { WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext( this.getServlet().getServletContext()); PersonService personService=(PersonService) ctx.getBean("personService"); request.setAttribute("persons", personService.getPersons()); System.out.println("12"); return mapping.findForward("list"); }
我们也可以将action教给spring管理:
要求:
1、确保action的path属性值与bean的名称相同。
<action path="/person/list" validate="false"> ... @Controller("/person/list") public class PersonAction extends Action { @Resource(name="personService") private PersonService personService; ...
2、在struts配置文件中添加进spring的请求控制器
该请求控制器会先根据action的path属性值到spring容器中寻找跟该属性同名的bean。如果寻找到即使用该bean处理用户请求。
<!-- 加入spring请求控制器 --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller>
较完整代码:
package cn.itcast.web.action; import javax.annotation.Resource; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; 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.stereotype.Controller; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import cn.itcast.service.PersonService; @Controller("/person/list") public class PersonAction extends Action { @Resource(name="personService") private PersonService personService; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response) throws Exception { request.setAttribute("persons", personService.getPersons()); System.out.println("12"); return mapping.findForward("list"); } /* @Override public ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response) throws Exception { WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext( this.getServlet().getServletContext()); PersonService personService=(PersonService) ctx.getBean("personService"); request.setAttribute("persons", personService.getPersons()); System.out.println("12"); return mapping.findForward("list"); } */ }
<?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> <action-mappings> <!-- <action path="/person/list" type="cn.itcast.web.action.PersonAction" validate="false"> <forward name="list" path="/WEB-INF/page/personlist.jsp"/> </action> --> <action path="/person/list" validate="false"> <forward name="list" path="/WEB-INF/page/personlist.jsp"/> </action> </action-mappings> <!-- 加入spring请求控制器 --> <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller> </struts-config>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 指定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> <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>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>人员列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <c:forEach items="persons" var="person"> ID=${person.id },name=${person.name} </c:forEach> </body> </html>
其实我的代码没有执行成功!我不太会struts1,所以。。。仅供参考。