【Struts2】基础总结

来源:互联网 发布:知乎 小知识 编辑:程序博客网 时间:2024/04/29 22:52

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。

用图说明:

这里写图片描述

这条线表示一个请求,是时间发生的先后顺序。从左边一个请求到右边的一个响应。
Struts2代码使用Action ,同时Action返回结果名称,找到对应的对象执行这个结果Result(转发对应某个页面或重定向对应某个地址)。
除了这个之外, 还会在前和后在执行一些拦截器。这些拦截器做的事包括一些公共的操作。以保证action只需去做一些核心的响应的操作。
这里写图片描述

这是因为,一个请求在Struts2框架中的处理大概分为以下几个步骤

1、客户端初始化一个指向Servlet容器(例如Tomcat)的请求
2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin)
3、接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action

同时,这里的拦截器也有很多。我们通过看它的struts2-core-2.2.6.jar下面的struts-default.xml 就能看到。

<interceptors>             <interceptor name="alias"class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>             <interceptor name="autowiring"class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>             <interceptor name="chain"class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>             <interceptor name="conversionError"class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>             <interceptor name="clearSession"class="org.apache.struts2.interceptor.ClearSessionInterceptor"/>             <interceptor name="createSession"class="org.apache.struts2.interceptor.CreateSessionInterceptor"/>             <interceptor name="debugging"class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor"/>             <interceptor name="externalRef"class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>             <interceptor name="execAndWait"class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>             <interceptor name="exception"class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>             <interceptor name="fileUpload"class="org.apache.struts2.interceptor.FileUploadInterceptor"/>             <interceptor name="i18n"class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>             <interceptor name="logger"class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>             <interceptor name="modelDriven"class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>             <interceptor name="scopedModelDriven"class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>             <interceptor name="params"class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>             <interceptor name="actionMappingParams"class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>             <interceptor name="prepare"class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>             <interceptor name="staticParams"class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>             <interceptor name="scope"class="org.apache.struts2.interceptor.ScopeInterceptor"/>             <interceptor name="servletConfig"class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>             <interceptor name="sessionAutowiring"class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>             <interceptor name="timer"class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>             <interceptor name="token"class="org.apache.struts2.interceptor.TokenInterceptor"/>             <interceptor name="tokenSession"class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>             <interceptor name="validation"class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>             <interceptor name="workflow"class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>             <interceptor name="store"class="org.apache.struts2.interceptor.MessageStoreInterceptor"/>             <interceptor name="checkbox"class="org.apache.struts2.interceptor.CheckboxInterceptor"/>             <interceptor name="profiling"class="org.apache.struts2.interceptor.ProfilingActivationInterceptor"/>             <interceptor name="roles"class="org.apache.struts2.interceptor.RolesInterceptor"/>             <interceptor name="jsonValidation"class="org.apache.struts2.interceptor.validation.JSONValidationInterceptor"/>             <interceptornameinterceptorname="annotationWorkflow"class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/> 

再搭建Struts框架的时候,需要这样几个步骤。
①引入struts相关的Jar。
这里写图片描述
②进行Struts2.xml文件的配置。
例如:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <!-- 配置为开发模式 -->    <constant name="struts.devMode" value="true" />    <!-- 把扩展名配置为action -->    <constant name="struts.action.extension" value="action"/>    <!-- 把主题配置为simple -->    <constant name="struts.ui.theme" value="simple"/>    <package name="default" namespace="/" extends="struts-default">  <!-- 配置测试用的Action (还未与Spring整合) class属性写类的全名-->  <!-- 当Struts2 与Spring整合后,class属性可以写bean的名称 --> <action name="test" class="testAction">  <result name="success">/test.jsp</result> </action> <!--岗位管理  --> <action name="role_*" class="roleAction" method="{1}"> <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result> <result name="toList" type="redirectAction">role_list</result> </action> </package></struts>

配置文件中“岗位管理”也就是当我们访问能匹配上 role_*的时候,指向roleAction类。不同的方法,根据不同的返回结果再去处理是转发到特定页面,还是需要重定向。例如上文中配置的<result name="toList" type="redirectAction">role_list</result>

0 0