您的位置:首页 > 编程语言 > Java开发

struts2标签

2015-12-07 22:25 323 查看
一、通用标签

1、property

  

Name

Required

Default

Evaluated

Type

Description

defaultfalsefalseStringThe default value to be used if value attribute is null
escapefalsetruefalseBooleanDeprecated. Use 'escapeHtml'. Whether to escape HTML
escapeCsvfalsefalsefalseBooleanWhether to escape CSV (useful to escape a value for a column)
escapeHtmlfalsetruefalseBooleanWhether to escape HTML
escapeJavaScriptfalsefalsefalseBooleanWhether to escape Javascript
escapeXmlfalsefalsefalseBooleanWhether to escape XML
valuefalse<top of stack>falseObjectValue to be displayed
  <s:property value="username" />输出变量名为username的变量,value中的值会被当成ognl表达式输出

  <s:property value="'username'" />输出username,加了单引号不会当成ognl表达式而是当成字符串输出

  <s:property value="admin" default="xiaoming" /> 输出变量名为admin的值,如果没有则默认为xiaoming

  <s:property value="'<br/>'" escape="false" />

2、set

Name

Required

Default

Evaluated

Type

Description

idfalsefalseStringDeprecated. Use 'var' instead
namefalsefalseStringDeprecated. Use 'var' instead
scopefalseactionfalseStringThe scope in which to assign the variable. Can be application, session, request, page, or action.
valuefalsefalseStringThe value that is assigned to the variable named name
varfalsefalseStringName used to reference the value pushed into the Value Stack
  <s:set var="username" value="xiaomi" />默认为request和ActionContext

  从request取值<s:property value="#request.username" />(debug标签可能在username还未放到request中时就已经形成了,所以有时候会看不到)

  从ActionContext取值<s:property value="#username" />

  

  设定范围<s:set var="username" value="xiaomi" scope="session" />

  从设定范围取值<s:property value="#session.username" />

3、bean

Name

Required

Default

Evaluated

Type

Description

idfalsefalseStringDeprecated. Use 'var' instead
nametruefalseStringThe class name of the bean to be instantiated (must respect JavaBean specification)
varfalsefalseStringName used to reference the value pushed into the Value Stack

  调用无参构造函数构造一个User类示例:xiaomi<s:bean name="cn.orlion.model.User var="xiaomi"></s:bean>

  调用无参构造函数构造一个User类示例:dami,初始化name为dami

  <s:bean name="cn.orlion.model.User var="dami">

    <s:param name="name" value="'dami'"></s:param> 这里value中的值必须要用单引号引起来,不然会被当成ognl表达式!!!

  </s:bean>

4、include

Name

Required

Default

Evaluated

Type

Description

valuetruefalseStringThe jsp/servlet output to include
  <s:include value="/index.jsp" />

  <s:include value="/index.jsp">

    <s:param name="param1">value1</s:param> (value会被当成字符串处理)

    <s:param name="param2">value2</s:param>

  </s:include>

  包含动态文件:

  <s:set name="url" value="'/index.jsp'" />

  <s:include value="%{#url}" />

5、param

Name

Required

Default

Evaluated

Type

Description

namefalsefalseStringName of Parameter to set
suppressEmptyParametersfalsefalsefalseBooleanWhether to suppress empty parameters
valuefalseThe value of evaluating provided name against stackfalseStringValue expression for Parameter to set
可以:<s:param name="param1" value="value1" />这里value1会被当成ognl表达式

也可以:<s:param name="param1" />value1</s:param>这里value1会被当成字符串

6、debug

不多作介绍

二、控制标签

1、if elseif else

  <s:if test="%{false}">

    <p>1</p>

  </s:if>

  <s:elseif test="%{true}">

    <p>2</p>

  </s:elseif>

  <s:else>

    <p>3</p>

  </s:else>

2、iterator

Name

Required

Default

Evaluated

Type

Description

beginfalse0falseIntegerif specified the iteration will start on that index
endfalseSize of the 'values' List or array, or 0 if 'step' is negativefalseIntegerif specified the iteration will end on that index(inclusive)
idfalsefalseStringDeprecated. Use 'var' instead
statusfalsefalsefalseBooleanIf specified, an instanceof IteratorStatus will be pushed into stack upon each iteration
stepfalse1falseIntegerif specified the iteration index will be increased by this value on each iteration. It can be a negative value, in which case 'begin' must be greater than 'end'
valuefalsefalseStringthe iteratable source to iterate over, else an the object itself will be put into a newly created List
varfalsefalseStringName used to reference the value pushed into the Value Stack
遍历集合:(输出123)

  <s:iterator value="{1,2,3}">

    <s:property/>

  </s:iterator>

自定义变量:(输出ABC)从集合中迭代取出赋值给val

  <s:iterator value="{'a', 'b' , 'c'}" var="val">

    <s:property value="#val.toUpperCase()"/>

  </s:iterator>

使用status:

  <s:iterator value="{'a' , 'b' , 'c'}" status="status">

    <s:property/>

    遍历过的元素的总数<s:property value="#status.count" />

    当前元素的索引<s:property value="#status.index" />

    当前是偶数<s:property value="#status.even" />

    当前是奇数<s:property value="#status.odd" />

    是否是第一个元素<s:property value="#status.first" />

    是否是最后一个元素<s:property value="#status.last" />

  </s:iterator>

遍历map

  <s:iterator value="#{1:'a' , 2:'b' , 3:'c'}">

    <s:property value="key" /> | <s:property value="value" />

  </s:iterator>

  <s:iterator value="#{1:'a' , 2:'b' , 3:'c'}" var="val">

    <s:property value="#val.key" /> | <s:property value="#val.value" />

  </s:iterator>

3、subset

Name

Required

Default

Evaluated

Type

Description

countfalsefalseIntegerIndicate the number of entries to be in the resulting subset iterator
deciderfalsefalseorg.apache.struts2.util.SubsetIteratorFilter.DeciderExtension to plug-in a decider to determine if that particular entry is to be included in the resulting subset iterator
idfalsefalseStringDeprecated. Use 'var' instead
sourcefalsefalseStringIndicate the source of which the resulting subset iterator is to be derived base on
startfalsefalseIntegerIndicate the starting index (eg. first entry is 0) of entries in the source to be available as the first entry in the resulting subset iterator
varfalsefalseStringThe name to store the resultant iterator into page context, if such name is supplied
三、UI标签

1、theme

主题,默认为xhtml,可以设置为simple/css_html/ajax

struts.xml中<constant name="theme" value="simple" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: