Struts2配置精要之Result Types(Struts2.3.4)



struts2.3.4的Predefined Result Types,比struts2.2.3要多出一种:  
Chain                 Used for Action Chaining 
Dispatcher         Used for web resource integration, including JSP integration 
FreeMarker         Used for FreeMarker integration 
HttpHeader         Used to control special HTTP behaviors 
Redirect         Used to redirect to another URL (web resource) 
RedirectAction         Used to redirect to another action mapping 
Stream                  Used to stream an InputStream back to the browser(usually for file downloads) 
Velocity         Used for Velocity integration 
XSL                 Used for XML/XSLT integration 
PlainText         Used to display the raw content of a particular page(i.e jsp, HTML) 
Tiles                 Used to provide Tiles integration 

文档中对这些Result Types是这样定义的:  
1.chain  
这个结果调用其他action,完成它自己定义的拦截器堆栈和结果。只能请求action,如果请求视图资源会报错。需要注意的就是与redirect的区别,请求转发是还在当前请求,而redirect会响应一次浏览器然后浏览器再根据响应请求重定向的资源,注意看url的变化就明白了! 
Xml代码   收藏代码
  1. <package name="public" extends="struts-default">  
  2.     <!-- Chain creatAccount to login, using the default parameter -->  
  3.     <action name="createAccount" class="...">  
  4.         <result type="chain">login</result>  
  5.     </action>  
  6.   
  7.     <action name="login" class="...">  
  8.         <!-- Chain to another namespace -->  
  9.         <result type="chain">  
  10.             <param name="actionName">dashboard</param>  
  11.             <param name="namespace">/secure</param>  
  12.         </result>  
  13.     </action>  
  14. </package>  
  15.   
  16. <package name="secure" extends="struts-default" namespace="/secure">  
  17.     <action name="dashboard" class="...">  
  18.         <result>dashboard.jsp</result>  
  19.     </action>  
  20. </package>  


2.redirectAction  
重定向至Action,完成与它自己的拦截器堆栈和结果。相对于redirect来说,redirectAction只能请求action,如果请求视图资源会报错,然后还有个小区别就是redirectAction会为url添加.action后缀而redirect不会,但是两者都可以通过url传参 
Xml代码   收藏代码
  1. <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">  
  2.    <!-- Pass parameters (reportType, width and height) -->  
  3.    <!--  
  4.    The redirectAction url generated will be :  
  5.    /genReport/generateReport.action?reportType=pie&width=100&height=100#summary  
  6.    -->  
  7.    <action name="gatherReportInfo" class="...">  
  8.       <result name="showReportResult" type="redirectAction">  
  9.          <param name="actionName">generateReport</param>  
  10.          <param name="namespace">/genReport</param>  
  11.          <param name="reportType">pie</param>  
  12.          <param name="width">100</param>  
  13.          <param name="height">100</param>  
  14.          <param name="empty"></param>  
  15.          <param name="suppressEmptyParameters">true</param>  
  16.          <param name="anchor">summary</param>  
  17.       </result>  
  18.    </action>  
  19. </package>  


3.redirect  
让客户端请求另外的网络资源,可以为action,也可以为视图资源。 
文档上是这么解释的: 
调用{ @link HttpServletResponse # sendRedirect(String)sendRedirect }方法到指定的地址。 响应是告诉重定向浏览器到指定的地址(一个新的请求从客户端)。这样做的结果意味着action(action instance, action errors, field errors等)只是执行失败,不再可用。这是因为action是一个单线程模式(single-thread model)。唯一的传参方法是通过会话或用OGNL表达式,url参数(url ?名称=值)。 
Xml代码   收藏代码
  1. <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">  
  2.    <-- Pass parameters (reportType, width and height) -->  
  3.    <!--  
  4.    The redirect-action url generated will be :  
  5.    /genReport/generateReport.jsp?reportType=pie&width=100&height=100#summary  
  6.    -->  
  7.    <action name="gatherReportInfo" class="...">  
  8.       <result name="showReportResult" type="redirect">  
  9.          <param name="location">generateReport.jsp</param>  
  10.          <param name="namespace">/genReport</param>  
  11.          <param name="reportType">pie</param>  
  12.          <param name="width">100</param>  
  13.          <param name="height">100</param>  
  14.          <param name="anchor">summary</param>  
  15.       </result>  
  16.    </action>  
  17. </package>  


4.dispatcher(缺省值,如果没有配置类型默认就是dispatcher)  
包括或转发到一个视图(通常是一个jsp)。在后台Struts2将使用一个RequestDispatcher,目标servlet/JSP接收相同的request/response对象作为原始的servlet或JSP。 因此,可以使用request.setAttribute()传递数据- - - Struts的action是可用的。如果请求action会找不到资源。 
Xml代码   收藏代码
  1. <result name="success" type="dispatcher">  
  2.   <param name="location">foo.jsp</param>  
  3. </result>  


5.httpheader  
可以通过设置HTTP headers和status的值来发送错误信息给客户端。 
他的参数有这些: 
status - the http servlet response status code that should be set on a response. 
parse - true by default. If set to false, the headers param will not be parsed for Ognl expressions. 
headers - header values. 
error - the http servlet response error code that should be set on a response. 
errorMessage - error message to be set on response if 'error' is set. 
Xml代码   收藏代码
  1. <result name="success" type="httpheader">  
  2.   <param name="status">204</param>  
  3.   <param name="headers.a">a custom header value</param>  
  4.   <param name="headers.b">another custom header value</param>  
  5. </result>  
  6.   
  7. <result name="proxyRequired" type="httpheader">  
  8.   <param name="error">305</param>  
  9.   <param name="errorMessage">this action must be accessed through a prozy</param>  
  10. </result>  


6.stream  
这个返回类型主要用作下载文件或者在浏览器上显示PDF等文档 
他的参数有这些: 
contentType - the stream mime-type as sent to the web browser (default = text/plain). 
contentLength - the stream length in bytes (the browser displays a progress bar). 
contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf". 
inputName - the name of the InputStream property from the chained action (default = inputStream). 
bufferSize - the size of the buffer to copy from input to output (default = 1024). 
allowCaching if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true) 
contentCharSet if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header 
Xml代码   收藏代码
  1. <result name="success" type="stream">  
  2.   <param name="contentType">image/jpeg</param>  
  3.   <param name="inputName">imageStream</param>  
  4.   <param name="contentDisposition">attachment;filename="document.pdf"</param>  
  5.   <param name="bufferSize">1024</param>  
  6. </result>  

此处给一个显示PDF文档示例: 
web.xml:  
Xml代码   收藏代码
  1. <mime-mapping>      
  2.      <extension>pdf</extension>      
  3.      <mime-type>application/pdf</mime-type>      
  4. </mime-mapping>      

struts.xml:  
Xml代码   收藏代码
  1. <action name="test" class="com.iss.action.TestAction">    
  2.     <result name="success" type="stream">    
  3.        <param name="contentType">application/pdf</param>    
  4.        <param name="inputName">inputStream</param>    
  5.        <param name="contentDisposition">filename="a.pdf"</param>    
  6.     </result>    
  7. </action>   


7.plainText  
响应以plain形式返回给客户端,相当于response.setContentType("text/plain; charset="+charSet); 
Xml代码   收藏代码
  1. <action name="displayJspRawContent" >  
  2.   <result type="plainText">/myJspFile.jsp</result>  
  3. </action>  
  4.   
  5. <action name="displayJspRawContent" >  
  6.   <result type="plainText">  
  7.      <param name="location">/myJspFile.jsp</param>  
  8.      <param name="charSet">UTF-8</param>  
  9.   </result>  
  10. </action>  



8.velocity  
使用Servlet容器的JspFactory,这个结果模拟一个JSP执行环境,然后显示一个Velocity模板,将直接传输到Servlet输出。 
Xml代码   收藏代码
  1. <result name="success" type="velocity">  
  2.   <param name="location">foo.vm</param>  
  3. </result>  



9.freemarker  
呈现一个视图使用Freemarker模板引擎。。 
Xml代码   收藏代码
  1. <result name="success" type="freemarker">foo.ftl</result>  


10.xslt  
调用一个xslt文件并解析执行。 
Xml代码   收藏代码
  1. <result name="success" type="xslt">  
  2.   <param name="location">foo.xslt</param>  
  3.   <param name="matchingPattern">^/result/[^/*]$</param>  
  4.   <param name="excludingPattern">.*(hugeCollection).*</param>  
  5. </result>  


11.tiles  
Tiles是一个模板框架被设计来轻松地允许创建web应用程序的页面具有一致的外观和感觉。它可以用于页面和组件化装饰。 
特性:支持在Freemarker,JSP,Velocity使用Tiles 
Xml代码   收藏代码
  1. .....  
  2. <result-types>  
  3.   <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>  
  4. </result-types>  
  5. .....  
  6. <action name="sample" class="org.apache.struts2.tiles.example.SampleAction" >  
  7.   <result name="success" type="tiles">tilesWorks</result>  
  8. </action>  


Xml代码   收藏代码
  1. <listener>  
  2.   <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>  
  3. </listener>  


Xml代码   收藏代码
  1. <dependency>  
  2.   <groupId>org.apache.struts</groupId>  
  3.   <artifactId>struts2-tiles-plugin</artifactId>  
  4.   <version>${version.tiles}</version>  
  5.   <scope>compile</scope>  
  6. </dependency>  


Xml代码   收藏代码
  1. <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3.   
  4. <%-- Show usage; Used in Header --%>  
  5. <tiles:importAttribute name="title" scope="request"/>  
  6. <html>  
  7.     <head><title><tiles:getAsString name="title"/></title></head>  
  8. <body>  
  9.     <tiles:insertAttribute name="header"/>  
  10.   
  11.     <p id="body">  
  12.         <tiles:insertAttribute name="body"/>  
  13.     </p>  
  14.   
  15.     <p>Notice that this is a layout made in JSP</p>  
  16. </body>  
  17. </html>  




这是目前为止收集的struts.xml中比较常用的常量值,包括现在的最新版本struts2.3.4常用的也就这些了。

 

下面是零配置中的常用的常量配置:

 

<constant name="struts.convention.action.disableScanning" value="false"/> 
是否不扫描类。一定要设为false,否则convention插件不起作用,零配置也没有意义。


<constant name="struts.convention.classes.reload" value="true" />

相当于开发模式,重新加载class文件。


<constant name="struts.convention.result.path" value="/WEB-INF/pages/"/> 
指定结果页面路径。 convention插件会自动在此路径中寻找文件。放到WEB-INF的目的的保护文件资源,只能通过程序内部跳转才能访问,我们的权限拦截器或其他权限处理只要加到action上就可以了。 默认值是/WEB-INF/content/


<constant name="struts.convention.action.name.separator" value="_"/>

具体action的名字将根据Action类名来进行转化,首先取出Action类名,其次根据驼峰命名法的类名将大写子母小写,默认用“-”进行分割并拼接,这样就形成一个action的name。
比如url为/hello-world,HelloWorldAction返回的结果是success,Result Type为Dispatcher即默认就跳转到/WEB-INF/content/hello-world.jsp
如果url是/test1/test2/hello ,HelloAction返回的结果是error,result type就跳转到/WEB-INF/content/test/test2/hello-error.html


<constant name="struts.convention.default.parent.package" value="default"/> 
设置默认的父包,一般我们都设置一个default包继承自struts-default。大部分类再继承default。如果有特殊的类需要特殊的包,只能在action中再指定父包了。 

<constant name="struts.convention.package.locators" value="action"/>

配置搜索包的关键词


<constant name="struts.convention.package.locators.disable" value="false"/>

设置禁止从action的根package搜索Action,默认值false


<constant name="struts.convention.package.locators.basePackage" value=""/> 
确定搜索包的路径。只要是结尾为action的包都要搜索。basePackage按照默认不用配置,如果配置,只会找以此配置开头的包。locators及locators.basePackage都是一组以逗号分割的字符串。 

<constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate."/>
排除哪些包不搜索。按默认配置即可。逗号分割字符串。 

<constant name="struts.convention.action.includeJars" value="" /> 
包括哪些jar包中的action。逗号分割字符串。 

<constant name="struts.convention.relative.result.types" value="dispatcher,freemarker,velocity"/> 
默认返回的结果类型搜索。按顺序先找相关的dispatcher的jsp文件是否存在。然后再找freemarker,再找velocity。 

<constant name="struts.convention.result.flatLayout" value="true"/> 
如果此值设为true,如果一个action的namespace为/login,名称为HelloWorldAction。result返回值是 success,默认会找到/WEB-INF/pages/login/hello-world.jsp(如果有hello-world- success.jsp就找这个文件,连接符“-”是在<constant name="struts.convention.action.name.separator" value="-"/>中配置的)。如果有一个action的result返回值是“error”,就会找/WEB-INF/pages /login/hello_world_error.jsp。 

如果此值设为false,如果一个action的命名空间为/login,名称为HelloWorldAction。result返回值是 success,默认会找到/WEB- INF/pages/login/hello_world/index.jsp(如果有success.jsp就找这个文件)。如果有一个action的 result返回值是“error”,就会找/WEB-INF/pages /login/hello_world/error.jsp。 

<constant name="struts.convention.action.mapAllMatches" value="false"/>

设置即使没有@action注解时仍然创建action映射,默认值false


<constant name="struts.convention.action.checkImplementsAction" value="true"/>

设置是否把实现了Action接口的类映射成action,默认值是true


<constant name="struts.convention.redirect.to.slash" value="true"/>

设置是否重定向到(/),例如当/login不存在时,重定向到/login/,默认值true


<constant name="struts.convention.action.suffix" value="Action"/>

搜索Action类名后缀,默认就是Action,不建议修改


<constant name="struts.convention.action.name.lowercase" value="true"/> 
设置是否在映射action的时候,所有字母都小写,默认值true



<constant name="..." value="..."/>

 

namevalue
struts.configuration该属性指定加载Struts 2配置文件的配置文件管理器。该属性的默认值是org.apache.Struts2.config.DefaultConfiguration,这是Struts 2默认的配置文件管理器。如果需要实现自己的配置管理器,开发者则可以实现一个实现Configuration接口的类,该类可以自己加载Struts 2配置文件。
struts.locale指定Web应用的默认Locale。如:zh_CN
struts.i18n.encoding指定Web应用的默认编码集。该属性对于处理中文请求参数非常有用,对于获取中文请求参数值,应该将该属性值设置为GBK或者GB2312。当设置该参数为GBK时,相当于调用HttpServletRequest的setCharacterEncoding方法。
struts.objectFactory指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring
struts.objectFactory.spring.autoWire指定Spring框架的自动装配模式,该属性的默认值是name,即默认根据Bean的name属性自动装配。
struts.objectFactory.spring.useClassCache该属性指定整合Spring框架时,是否缓存Bean实例,该属性只允许使用true和false两个属性值,它的默认值是true。通常不建议修改该属性值。
struts.objectFactory.spring.autoWire.alwaysRespect确保遵守自动装配策略。默认值为false
struts.objectTypeDeterminer该属性指定Struts 2的类型检测机制,通常支持tiger和notiger两个属性值。
struts.multipart.parser该属性指定处理multipart/form-data的MIME类型(文件上传)请求的框架,该属性支持cos、pell和jakarta等属性值,即分别对应使用cos的文件上传框架、pell上传及common-fileupload文件上传框架。该属性的默认值为jakarta。注意:如果需要使用cos或者pell的文件上传方式,则应该将对应的JAR文件复制到Web应用中。例如,使用cos上传方式,则需要自己下载cos框架的JAR文件,并将该文件放在WEB-INF/lib路径下。
struts.multipart.saveDir该属性指定上传文件的临时保存路径,该属性的默认值是javax.servlet.context.tempdir
struts.multipart.maxSize"

该属性指定Struts 2文件上传中整个请求内容允许的最大字节数。

默认值是2097152字节

struts.custom.properties该属性指定Struts 2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖struts.properties文件中指定的属性,如果需要加载多个文件则用英文逗号隔开。
struts.mapper.class指定将HTTP请求映射到指定Action的映射器,Struts 2提供了默认的映射器:org.apache.struts2.dispatcher.mapper.DefaultActionMapper。默认映射器根据请求的前缀与Action的name属性完成映射。
struts.action.extension该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts 2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。
struts.serve.static该属性设置是否通过JAR文件提供静态内容服务,该属性只支持true和false属性值,该属性的默认属性值是true。
struts.serve.static.browserCache该属性设置浏览器是否缓存静态内容。当应用处于开发阶段时,我们希望每次请求都获得服务器的最新响应,则可设置该属性为false,其默认值是true
struts.enable.DynamicMethodInvocation该属性设置Struts2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。一般不建议修改
struts.enable.SlashesInActionNames该属性设置Struts2是否允许在Action名中使用斜线,该属性的默认值是false。如果开发者希望允许在Action名中使用斜线,则可设置该属性为true。
struts.tag.altSyntax该属性指定是否允许在Struts2标签中使用表达式语法,因为通常都需要在标签中使用表达式语法,故此属性应该设置为true,该属性的默认值是true。
struts.devMode该属性设置Struts2应用是否使用开发模式。应用在开发阶段,将该属性设置为true,当进入产品发布阶段后,则该属性设置为false。其默认值是false
struts.i18n.reload该属性设置是否每次HTTP请求到达时,系统都重新加载资源文件。该属性默认值是false。在开发阶段将该属性设置为true会更有利于开发,但在产品发布阶段应将该属性设置为false。
提示 开发阶段将该属性设置了true,将可以在每次请求时都重新加载国际化资源文件,从而可以让开发者看到实时开发效果;产品发布阶段应该将该属性设置为 false,是为了提供响应性能,每次请求都需要重新加载资源文件会大大降低应用的性能。
struts.ui.theme该属性指定视图标签默认的视图主题,该属性的默认值是xhtml。
struts.ui.templateDir该属性指定视图主题所需要模板文件的位置,该属性的默认值是template,即默认加载template路径下的模板文件。
struts.ui.templateSuffix该属性指定模板文件的后缀,该属性的默认属性值是ftl。该属性还允许使用ftl、vm或jsp,分别对应FreeMarker、Velocity和JSP模板。
struts.configuration.xml.reload该属性设置当struts.xml文件改变后,系统是否自动重新加载该文件。该属性的默认值是false
struts.velocity.configfile该属性指定Velocity框架所需的velocity.properties文件的位置。该属性的默认值为velocity.properties
struts.velocity.contexts该属性指定Velocity框架的Context位置,如果该框架有多个Context,则多个Context之间以英文逗号(,)隔开。
struts.velocity.toolboxlocation该属性指定Velocity框架的toolbox的位置
struts.url.http.port该属性指定Web应用所在的监听端口。该属性通常没有太大的用处,只是当Struts 2需要生成URL时(例如Url标签),该属性才提供Web应用的默认端口。
struts.url.https.port该属性类似于struts.url.http.port属性的作用,区别是该属性指定的是Web应用的加密服务端口。
struts.url.includeParams该属性指定Struts 2生成URL时是否包含请求参数。该属性接受none、get和all三个属性值,分别对应于不包含、仅包含GET类型请求参数和包含全部请求参数。
struts.custom.i18n.resources该属性指定Struts 2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开。
struts.dispatcher.parametersWorkaround对于某些Java EE服务器,不支持HttpServlet Request调用getParameterMap()方法,此时可以设置该属性值为true来解决该问题。该属性的默认值是false。对于 WebLogic、Orion和OC4J服务器,通常应该设置该属性为true
struts.freemarker.manager.classname该属性指定Struts2使用的FreeMarker管理器。该属性的默认值是org.apache.struts2.views.freemarker.FreemarkerManager,这是Struts2内建的FreeMarker管理器
struts.freemarker.beanwrapperCache默认值false,是否支持缓存的BeanWrapper的模型
struts.freemarker.wrapper.altMap该属性只支持true和false两个属性值,默认值是true。通常无需修改该属性值
struts.freemarker.mru.max.strong.size默认值100,也没有必要修改
struts.xslt.nocache该属性指定XSLT Result是否使用样式表缓存。当应用处于开发阶段时,该属性通常被设置为true;当应用处于产品使用阶段时,该属性通常被设置为false
struts.configuration.files该属性指定Struts2框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开。该属性的默认值为struts-default.xml,struts-plugin.xml,struts.xml,这就是为什么Struts2框架默认加载struts.xml文件了。
struts.ognl.allowStaticMethodAccess是否允许OGNL表达式访问静态方法,默认是false
struts.el.throwExceptionOnFailure当EL表达式找不到该属性或当表达式求值的失败时,支持缓存的模型在BeanWrapper是否会抛出一个RuntimeException的异常,默认false
struts.ognl.logMissingProperties当OGNL表达式没有找到属性,是否记录日志作为警告(非常冗长)默认false
struts.ognl.enableExpressionCache默认true,如果应用程序会用到大量的不同的表达式,缓存OGNL解析表达式,但会导致内存泄漏
struts.mapper.alwaysSelectFullNamespace设置映射器是否总是选择完整的命名空间,默认值是false

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值