Struts2 标签使用小结(转帖整理)

本文介绍了Struts框架与FreeMarker模板引擎的集成方法,包括如何在不同作用域中访问属性值、使用内置变量及标签支持等内容。还提供了实用技巧,如输出session中的值、获取国际化文件中的内容等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://kino.blog.chinajavaworld.com/entry/4315/1/ 

struts官方帮助地址 http://struts.apache.org/2.x/docs/tag-developers-guide.html
Application Scope Attribute
Assuming there's an attribute with name myApplicationAttribute in the Application scope.

<#if Application.myApplicationAttribute?exists>
${Application.myApplicationAttribute}
</#if>
or

<@s.property value="%{#application.myApplicationAttribute}" />
Session Scope Attribute
Assuming there's an attribute with name mySessionAttribute in the Session scope.

<s:set name="countrycode" value="%{#application.COUNTRYCODE}" />


<#if Session.mySessionAttribute?exists>
${Session.mySessionAttribute}
</#if>
or

<@s.property value="%{#session.mySessionAttribute}" />
Request Scope Attribute
Assuming there's an attribute with name 'myRequestAttribute' in the Request scope.

<#if Request.myRequestAttribute?exists>
${Request.myRequestAttribute}
</#if>
or

<@s.property value="%{#request.myRequestAttribute}" />
Request Parameter
Assuming there's a request parameter myParameter (eg. http://host/myApp/myAction.action?myParameter=one).

<#if Parameters.myParameter?exists>
${Parameters.myParameter}
</#if>
or

<@s.property value="%{#parameters.myParameter}" />
Context parameter
Assuming there's a parameter with the name myContextParam in framework context.

${stack.findValue('#myContextParam')}
or

<@s.property value="%{#myContextParam}" />
Template Loading
The framework looks for FreeMarker templates in two locations (in this order):

Web application
Class path
This ordering makes it ideal for providing templates inside a fully-built jar, but allowing for overrides of those templates to be defined in your web application. In fact, this is how you can override the default UI tags and Form Tags included with the framework.

In addition, you can specify a location (directory on your file system) through the templatePath or TemplatePath context variable (in the {{web.xml)}. If a variable is specified, the content of the directory it points to will be searched first.

This variable is currently NOT relative to the root of your application.

Variable Resolution
When using FreeMarker with the framework, variables are looked up in several different places, in this order:

Built-in variables
Value stack
Action context
Request scope
Session scope
Application scope
Note that the action context is looked up after the value stack. This means that you can reference the variable without the typical preceding has marker (#) like you would have to when using the JSP s:property tag. This is a nice convenience, though be careful because there is a small chance it could trip you up.

<@s.url id="url" value="http://www.yahoo.com"/>
Click here!
The built-in variables that Struts-FreeMarker integration provides are:

Name Description
stack The value stack itself, useful for calls like ${stack.findString('ognl expr')}
action The action most recently executed
response The HttpServletResponse
res Same as response
request The HttpServletRequest
req Same as request
session The HttpSession
application The ServletContext
base The request's context path

Tag Support
FreeMarker includes complete tag support. See the FreeMarker Tags documentation for information on how to use the generic Struts Tags provided by Struts. In addition to this, you can use any JSP tag, like so:

<#assign mytag=JspTaglibs["/WEB-INF/mytag.tld"]>
<@mytag.tagx attribute1="some ${value}"/>
Where mytag.tld is the JSP Tag Library Definition file for your tag library. Note: in order to use this support in FreeMarker, you must enable the JSPSupportServlet in web.xml:

Adding JspSupportSerlvet to web.xml
<servlet>
<servlet-name>JspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Tips and Tricks
There are some advanced features that may be useful when building Struts applications with FreeMarker.

转自:http://bearjava20071220091900.javaeye.com/blog/153349

  1. 1、输出session中的值   
  2. a. <s:property value="#session['key']"/>  
  3. b. ${sessionScope.key}   
  4.   
  5. 2、获取session中的值后判断   
  6. <s:if test="#session['key']==null">  
  7.   
  8. 3、输出Action中的属性值   
  9. <s:property value="property"/>  
  10.   
  11. 4、输出国际化文件中的值   
  12. a. <s:text name="key"/>  
  13. b. ${getText("key")}   
  14.   
  15. 5、输出Action中的消息   
  16. <s:actionmessage />  
  17.   
  18. 6、输出Action中的错误   
  19. <s:actionerror/>  
  20.   
  21. 7、迭代输出集合   
  22. <s:iterator id="book" value="books" status="index">  
  23.   
  24.      <!---输出当前元素的属性-->  
  25.      <s:property value="property"/>  
  26.   
  27.      <!---输出当前迭代元素的索引-->  
  28.      <s:property value="#index.index"/>  
  29.   
  30.      <!---输出当前迭代了几个元素-->  
  31.      <s:property value="#index.count"/>    
  32.   
  33.       <!---返回当前迭代元素的索引是否为奇数-->  
  34.      <s:property value="#index.odd"/>    
  35.   
  36.      <!---返回当前迭代元素的索引是否为偶数-->  
  37.      <s:property value="#index.event"/>  
  38.   
  39.      <!---返回当前元素是否为第一个-->  
  40.      <s:property value="#index.first"/>  
  41.   
  42.      <!---返回当前元素是否为最后一个-->  
  43.      <s:property value="#index.last"/>  
  44.   
  45. </s:iterator>  
  46.   
  47. 8、定义页面变量   
  48. <!---将分页Bean的属性放入Stack Context-->  
  49. <s:set name="count" value="%{pager.totalPages}"/>  
  50.   
  51. <!---利用<SPAN class=hilite1>Struts2</SPAN>标签访问-->  
  52. <s:property value="#count"/>  
  53.   
  54. <!---利用OGNL表达式访问-->  
  55. ${pageScope.count }   
  56.   
  57. <!---利用Java代码访问-->  
  58. <%      
  59.      Object obj = pageContext.getAttribute("count");       
  60.      int mycount = Integer.parseInt(obj.toString());   
  61.      for(int i =0;i<mycount;i++){   
  62.            out.print(i+1);   
  63.      }   
  64. %>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值