struts2

来源:互联网 发布:手机伴侣软件 编辑:程序博客网 时间:2024/05/29 17:26
struts1.x








struts2:
// 设计原理来建筑学
Apache Struts 2 Architecture in a Nutshell




需要搭建struts2平台
导入包:
struts2-core-2.3.1.1.jar//框架核心类库
xwork-core-2.3.1.1.jar//XWork类库 ,Struts要其上构建
ognl-3.0.3.jar//对象图导航语言,struts2框架通过其读定对象的属性
freemarker-2.3.18.jar//strus2的UI标签的模板使用FreeMarker编写
commons-fileupload-1.2.2.jar//文件上传组件,2.1.6版本后必须加入此文件
commons-logging-1.1.1.jar//ASF出品的日志包,struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录
javassist-3.11.0.GA.jar//O.GA表达式
commons-lang-2.5.jar
commons-io-2.0.1.jar
注意:不要全部导入进去,因为有第三方的插件包,但没有提供第三方实现jar文件

编写Struts2的配置文件
<?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>
<package name="struts" namespace="/test" extends="struts-default">
<action name="helloworld" class="action.ActionStruts" method="execute" >
<result name="success">/WEB-INF/page/helloworld.jsp</result>
    </action>
    </package>
</struts>

在web.xml文件中配置struts2MVC框架的启动文件.其通过Filter来启动

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>


    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




action的默认值:
class="ActionSupport"
method="execute"
<result name="success"></result>


result:type常用的类型:dispatcher(默认)/redirect/redirectAction/plainText


O.GA表达式:






MVC层
业务层
访问层








web框架:


StrutsPrepareAndExecuteFilter类:核心类


API:
org.apache.struts2.interceptor.ServletRequestAware
org.apache.struts2.interceptor.servletResponseAware

com.opensymphony.xwork2.ActionContext
ActionSupport
org.apache.struts2.ServletActionContext
com.opensymphony.xwork2.config.entities.ActionConfig;


Action

com.opensymphony.xwork2.ActionSupport
validate()
ActionContext.getContext().getSession().put(key, value)

Interceptor
init()
intercept(ActionInvocation invocation) 
destroy()

ActionInvocation



struts2的标签库
struts2-core-2.3.1.1.jar---->META_INF--->
struts-tags.tld文件
使用页面引用
<%@ taglib uri="/struts-tags" prefix="s"%>
<short-name>s</short-name>
  <uri>/struts-tags</uri>
  使用:
  <s:fielderror></s:fielderror> 
  .....
*
嵌套的的拦载器参数高于一切
Nested Interceptor param overriding 






Action名称的搜索顺序




Action中result的转发类型:
dispatcher(默认)(转发),redirect(重定向),redirectAction,plainTect




拦载器栈


  <interceptor-stack name="basicStack"、>
<interceptor-stack name="validationWorkflowStack"/>
<interceptor-stack name="fileUploadStack"/>
<interceptor-stack name="modelDrivenStack"/>
<interceptor-stack name="chainStack"/>
<interceptor-stack name="i18nStack"/>
<interceptor-stack name="paramsPrepareParamsStack"/>
<interceptor-stack name="defaultStack"/>
<interceptor-stack name="completeStack"/>
<interceptor-stack name="executeAndWaitStack">

<default-interceptor-ref name="defaultStack"/>//默认的拦载器


18个基本的拦载器
 
 <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="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
           //清除会话拦载器
            <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="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"/>
            //servletConfig拦载器(还未了解,需要重新查阅资料)
            <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
           //计时器拦载器
            <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="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
            //多层次设置拦载器
            <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />


</interceptors>
 
18个Interceptor-ref:
顺序加载
<interceptor-ref name="exception"/>//异常拦载器
        <interceptor-ref name="alias"/>//别名拦载器
        <interceptor-ref name="servletConfig"/>
        <interceptor-ref name="i18n"/>//国际化
        <interceptor-ref name="prepare"/>//准备拦载器
        <interceptor-ref name="chain"/>//链拦载器
        <interceptor-ref name="scopedModelDriven"/>审视模型驱动拦载器
        <interceptor-ref name="modelDriven"/>//模型驱动拦载器
        <interceptor-ref name="fileUpload"/>//文件上传拦载器
        <interceptor-ref name="checkbox"/>//检验
        <interceptor-ref name="multiselect"/>//上传文件大小
        <interceptor-ref name="staticParams"/>静态参数
        <interceptor-ref name="actionMappingParams"/>//功能映射参数拦载器
        <interceptor-ref name="params">//参数拦载器
          <param name="excludeParams">dojo\..*,^struts\..*</param>
        </interceptor-ref>
        <interceptor-ref name="conversionError"/>//转换错误
        <interceptor-ref name="validation">//确认/验证拦载器
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="workflow">//工作流拦载器
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="debugging"/>//调试拦载器






参数文件
struts2-core-2.3.1.1.jar包:org.apache.struts2下的参数文件
default.properties
struts-messages.properties
struts-messages_da.properties
struts-messages_de.properties
struts-messages_pl.properties
struts-messages_pt.properties

default.properties下的参数
//国际化的编码:默认为UTF-8
struts.i18n.encoding=UTF-8
//与spring的集成参数
struts.objectFactory = spring
struts.objectFactory.spring.autoWire = name
struts.objectFactory.spring.useClassCache = true
struts.objectFactory.spring.autoWire.alwaysRespect = false
//限制对象的类型
struts.objectTypeDeterminer = tiger
struts.objectTypeDeterminer = notiger
//设置文件的大小,目录
struts.multipart.parser=cos
struts.multipart.parser=pell
struts.multipart.parser=jakarta
struts.multipart.saveDir=
struts.multipart.maxSize=2097152
//定制特性
struts.custom.properties=application,org/apache/struts2/extension/custom
//导航类
struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper
//功能扩展
struts.action.extension=action,,
//是否提供静态的服务
struts.serve.static=true
//浏览器是否提供静态的缓存
struts.serve.static.browserCache=true
//是否请求能够使用动态方法的
struts.enable.DynamicMethodInvocation = true

struts.enable.SlashesInActionNames = false
//设置标签语法
struts.tag.altSyntax=true
//设置是否使用开发模式
struts.devMode = false
//国际化的重载
struts.i18n.reload=false
//UI主题
struts.ui.theme=xhtml
//UI的模板目录和后缀
struts.ui.templateDir=template
struts.ui.templateSuffix=ftl
//重新加载XML的配置
struts.configuration.xml.reload=false
//效率配置
struts.velocity.configfile = velocity.properties
struts.velocity.contexts =
struts.velocity.toolboxlocation=
//URI配置
struts.url.http.port = 80
struts.url.https.port = 443
struts.url.includeParams = none
//用记的国际化资源
struts.custom.i18n.resources=testmessages,testmessages2
//转发参数工作群
struts.dispatcher.parametersWorkaround = false

struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager
struts.freemarker.templatesCache=false
struts.freemarker.beanwrapperCache=false
struts.freemarker.wrapper.altMap=true
struts.freemarker.mru.max.strong.size=100

struts.xslt.nocache=false

struts.mapper.alwaysSelectFullNamespace=false
//OGNL标签的配置
struts.ognl.allowStaticMethodAccess=false
//EL标签配置
struts.el.throwExceptionOnFailure=false
//OGNL标签的配置
struts.ognl.logMissingProperties=false
struts.ognl.enableExpressionCache=true

//结果集类型
<result-types>
//链
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
           ***//转发
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            //自定义标签输出
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            //
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
           //重定向
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            //重定向到某一个Action
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
           //流输出
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
           
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
           //原码打印,但输出时需要解码
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
      </result-types>





OGNL表达式:(Object Graphic Navigation Language)
对象图导航语言

格式:用#号来标识
         
ValueStack
存放值的地方,存于request中
request
application
session
attr
parameters



struts2标签:
struts-tags.tld文件
freamarker:ftl文件
格式:<s:params....>
在使用的页面中申明引用
<%@ taglib prefix="s" uri="/struts-tags"%>


<s:a value="" cssClass="" accesskey="" encode="" href="" method="" onclick=""/>
   <s:action var="" name="" executeResult="" namespace="" id="" ignoreContextParams=""></s:action>
   <s:bean name="" id="" var=""></s:bean>
   <s:url action="" encode="" includeContext="" method=""></s:url>
   <s:checkbox id="" cssStyle="" theme="" onclick="" label="" onmousedown=""></s:checkbox>
   <s:append id="" var=""></s:append>
<s:date name="" id="" format="" timezone="" nice="" var="" />
<s:div accesskey="" id="" cssStyle="" key="" value="" name="" onselect=""></s:div>    
<s:if test=""></s:if>
<s:else></s:else>
<s:elseif test=""></s:elseif>
<s:fielderror id="" theme="" title="" onmouseover=""></s:fielderror>
<s:file id="" cssClass="" disabled="" onselect=""></s:file>
<s:form action="" focusElement="" theme="" namespace=""></s:form>
<s:head id="" tooltip=""/>
<s:hidden value=""></s:hidden>
<s:i18n name=""></s:i18n>
<s:include value=""></s:include>
<s:iterator id="" begin="" status="" value="" step="" end="" var=""></s:iterator>
<s:label for="" id=""></s:label>
<s:optiontransferselect doubleList="" list="" doubleName=""></s:optiontransferselect>
<s:param name="" value=""></s:param>
<s:password id="" value="" maxlength="" maxLength=""></s:password>
<s:property default="" value="" escape="" escapeCsv="" escapeHtml="" escapeJavaScript="" escapeXml=""/>
<s:radio list="" id="" onselect="" theme=""></s:radio>
<s:sort comparator=""></s:sort>
<s:select list="" size="" emptyOption="" listKey="" listValue=""></s:select>
<s:submit action="" ondblclick=""></s:submit>
<s:set id="" name="" scope="" value="" var=""></s:set>
<s:subset count="" decider="" id="" source="" start="" var=""></s:subset>
<s:reset accesskey="" action="" id="" align="" type=""></s:reset>
<s:text name="" id="" searchValueStack="" var=""></s:text>
<s:textfield cssStyle="" onchange=""></s:textfield>
<s:textarea cols="" rows=""></s:textarea>
<s:token></s:token>
<s:updownselect list=""></s:updownselect>
<s:url action="" encode="" escapeAmp="" includeContext="" windowState=""></s:url>
<s:debug theme="" tooltip="" required="" labelposition="" id="" accesskey="" cssClass="" disabled="" template="" value=""></s:debug>






















原创粉丝点击