接上篇:Spring整合Hibernate:http://blog.csdn.net/u010800530/article/details/38470321
首先,如果要整合Struts2,则核心拦截器是必不可少的了,然后还要配置ContextLoaderListener监听器。
一、这里是web.xml的配置内容,接下来会详细介绍它的所有配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<!-- 指定初始化struts2的配置文件 -->
<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、首先,我们看到了<context-param></context-param>标签:
格式定义:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>contextConfigLocationValue</param-value>
</context-param>
如果不配置这个标签,默认情况下,会加载WEB-INF/applicationContext.xml这个文件。但是我们可以通过配置<context-param>标签中的<param-value>标签的内容来改变配置文件的默认属性。
如上所示,我们就是修改了<param-value>属性,让他加载classpath:beans.xml这个配置文件
(PS:关于这个标签的具体详解参照我的另一篇博文:web.xml配置<context-param>详解)
2、然后我们通过配置ContextLoaderListener监听器,使IOC容器在启动的时候,自动加载applicationContext配置,
因为它实现了ServletContextListener这个接口,容器启动时候会自动执行它实现的方法。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、最后是配置struts2的核心拦截器,并初始化struts2配置文件
二、随后我们要配置web.xml中提到的struts-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<action-mappings>
<action path="/person/list" validate="false">
<forward name="list" path="/WEB-INF/page/personlist.jsp" />
</action>
</action-mappings>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
</struts-config>
1、在struts-config.xml中,有一个anction,它的路径是“/person/list”。如果想要把这个action交给Spring管理,我们可以使用依赖注入在action中注入业务层的bean。
但是,必须要确保action的path属性值和Spring里边的bean的名称(name)相同。
比如,我们在上一个Spring整合Hibernate的博文中有一个beans.xml,里边有一个bean,他的代码如下:
<span style="font-size:14px;"><bean name="/person/list" class="cn.itcast.web.action.PersonAction" /></span>
2、仅仅有了这些还不够,必须要定义<controller>标签:
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
采用DelegatingRequestProcessor将处理转发给Spring容器中的bean,DelegatingRequestProcessor继承自RequestProcessor。
这段代码的作用就是,告诉Struts用DelegatingRequestProcessor来代替原来的RequestProcessor。完成设置这个设置后,Struts会将拦截到的用户请求转发到Spring context下的bean,根据bean的name属性来匹配
三、在beans.xml中接收到了struts传过来的action之后,匹配到相应的bean。
然后我们就需要实现这个bean的实体类了:
PersonAction.java
package cn.itcast.web.action;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import cn.itcast.service.PersonService;
public class PersonAction extends Action{
@Resource PersonService personService;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/*这里加注释的内容是未把Action交给SpringIOC管理的时候。
* WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
this.getServlet().getServletContext());
PersonService personService = (PersonService)ctx.getBean("personService");
*/
request.setAttribute("persons", personService.getPersons());
return mapping.findForward("list"); //直接跳转到在struts-config里边name="list"的页面。
}
}
四、接下来我们就要实现view页面了,就是WEB-INF/page/personList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'personlist.jsp' starting page</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">
</head>
<body>
<c:forEach items="${persons}" var="person">
ID = ${person.id },name = ${person.name }<br>
</c:forEach>
</body>
</html>
这里用到的是jstl标签直接输出出来了。
尤其要注意的是,SSH三层框架的项目搭建好了,只需要输入地址:http://localhost:8080/SSH2/person/list.do就可以进行访问了。
但是,因为我们的jsp页面是在WEB-INF下面的,是属于安全性的,所以不能够通过浏览器地址栏输入得出结果,只能够通过控制台
那里的Web Browser输出结果。