代码改变世界

Struts2 入门

2017-11-16 19:47  龘靐齉爩  阅读(182)  评论(0)    收藏  举报

实现struts2和spring的整合 :

一:struts2整合到spring容器的步骤:

web.xml 
1.配置【web监听spring容器上下文对象】

<!-- web监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2.【初始化Spring容器,加载spring核心配置文件】

<!-- 初始化Spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/applicationContext.xml</param-value>
</context-param>

spring容器配置:applicationContext.xml,导入负责处理业务bean和持久化bean的配置,需要额外添加对controller层的action bean管理的支持。(将action bean交给spring容器管理)
3.【配置struts2核心过滤器,过滤所有的请求】
4.【加载struts2核心配置】
struts2核心配置:struts.xml,负责处理controller层的action bean以及前端控制功能,声明action对象的管理交给spring容器。

二:整合后applicationContext.xml和struts.xml的详细配置

1.struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 常量配置 -->
<!-- 开发模式打开 -->
<constant name="struts.devMode" value="true" />
<!-- 声明action对象的管理交给spring容器 -->
<constant name="struts.objectFactory" value="spring" />

<!-- action配置:包,命名空间,action,result等等 -->
<package name="zidingyide" extends="struts-default" namespace="/">
<!-- name表示请求的url:/rams/user_* -->
<!-- class表示请求的处理action对象 -->
<!-- method表示处理请求的方法名,{1}与name中第一次出现的通配符关联 -->
<action name="user_*" class="userAction" method="{1}">
<!-- result表示响应的结果,此处响应webapp根目录下面的index.jsp页面 -->
<result name="success">/index.jsp</result>
</action>
</package>
</struts>

    

2.applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 导入spring配置 -->
<import resource="spring-service.xml" />
<import resource="spring-dao.xml" />

<!-- 打开组件扫描controller,将action对象管理起来 -->
<context:component-scan base-package="com.entor.rams.actions">
<!-- 只对@Controller -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>

三、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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>RAMS</display-name>

<!-- 首页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- web监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 初始化Spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/applicationContext.xml</param-value>
</context-param>

<!-- 配置struts过滤器,接管所有的请求 -->
<!-- 默认加载classpath路径下面的struts.xml -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

例如:

UserAction继承ActionSupport实现ModelDrive<T>
1.ModelDrive<T>提供了对参数整体封装
2.必须提供getModel方法并实现(由这个方法接收并封装请求参数)
3.如果想在jsp页面使用el表达式直接访问参数,可以在action中将参数声明为成员,并提供这个成员的getter方法。