`

struts2自带项目showcase的tags学习笔记

 
阅读更多
struts2自带项目showcase的tags学习笔记

第一、首先我们来看non-ui的标签。
有几个需要注意的地方:
1.actionTag
<s:action name="includePage" namespace="/tags/non-ui/actionTag" executeResult="true" />

这里的action并不是存在于url或是form中,而是直接一个单独的标签,这里的executeResult为true表示显示结果集。即显示的结果为:
引用
This is INCLUDED by the action tag
而如果executeResult为false代表不显示结果集。
2.我们来看下date标签
a.首先是用法
<tr>
  		<td><strong>Now Date</strong></td>
  		<td>dd.MM.yyyy hh:mm:ss</td>
  		<td><s:date name ="#mydate.now" format ="dd.MM.yyyy hh:mm:ss"/></td>
  	</tr>

就会得到
引用
Now Date dd.MM.yyyy hh:mm:ss 24.07.2012 07:47:58

我们来看参考文档:
引用
The date tag will allow you to format a Date in a quick and easy way,You can specify a custom format (eg. "dd/MM/yyyy hh:mm"), you can generate easy readable notations (like "in 2 hours, 14 minutes"), or you can just fall back on a predefined format with key 'struts.date.format' in your properties file.

而date参数的name 用法为
引用
The date value to format

b.我们来看下
name ="#mydate.now"

查看OGNL文档,我们得知
引用
OGNL has a simple variable scheme, which lets you store intermediate results and use them again, or just name things to make an expression easier to understand. All variables in OGNL are global to the entire expression.You refer to a variable using a number sign in front of its name, like this:
这个也就是变量引用的一种用法。
c.
<td><strong>Before Date</strong></td>
<td>nice</td>
<td><s:date name ="#mydate.before" nice ="true"/></td>
这里的nice并不是输出时间,而是以文字的方式表示时间。
像这里就显示
引用
Before Date nice one year ago


3、s:generator的使用
a
引用
可以这样理解:generator将一个字符串转化成一个集合。在该标签的标签体内,整个临时生成的集合将位于ValueStack的顶端,但一旦该标签结束,该集合将被移出ValueStack。

引用
count :可选属性,该属性指定生成集合中元素的总数。
separator :必填属性,指定用于解析字符串的分隔符。
val:必填属性,指定被解析的字符串。
converter:可选属性,指定一个转换器,该转换器负责将集合汇总的每个字符串转换成对象
id:可选属性,如果指定了该属性,则将生成的集合放在pageContext属性中

那我们需要结合s:iterator 来使用:
<s:generator val ="%{value}" separator ="%{separator}" count ="%{count}">
	<s:iterator value ="%{top}">
		<s:property />
	</s:iterator>
</s:generator>
这里s:iterator 就是迭代值栈的栈顶。s:property 的Value没有 就代表使用默认值,即取出对象。
b.
引用
<s:url value ="%{'/tags/non-ui'}" var ="url"/><s:a href ="%{url}">Back to non-ui</s:a>
这里就是用到了
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
welcome-file-list 就是用来指定在一个目录中默认的名字,所以这里实际就是跳转到/tags/non-ui/index.jsp。

4、<s:append>的使用:
引用
来做iterator标签的辅助,将不同iterator中的内容合在一个iterator中。

<s:generator var="iterator1" separator="," val="%{iteratorValue1}" />
    <s:generator var="iterator2" separator="," val="%{iteratorValue2}" />
    
    <s:append id="appendedIterator">
        <s:param value="%{#attr.iterator1}" />
        <s:param value="%{#attr.iterator2}" />
    </s:append>
  这里要注意 <s: generator> 的var的功能
引用
The name to store the resultant iterator into page context, if such name is supplied
也就是把iterator1存放在pageContext的对象当中,pageContext是jsp页面的上下文,它用来管理输出页面的属性,包括application,session,request和其自身上的属性。
pageContext是PageContext类的实例,它提供了如下两个方法来访问page、request、session、application范围的变量。 
    getAttribute(String name):取得page范围内的name属性。 
    getAttribute(String name,int scope):取得指定范围内的name属性,其中scope可以是如下4个值: 
    PageContext.PAGE_SCOPE:对应于page范围。 
    PageContext.REQUEST_SCOPE:对应于request范围。 
    PageContext.SESSION_SCOPE:对应于session范围。 
    PageContext.APPLICATION_SCOPE:对应于application范围。 
具体可参考http://adminsun.iteye.com/blog/537429
5、<s:subset>
<s:generator var="iterator" val="%{iteratorValue}" separator="," />

    <s:subset count="%{count}" start="%{start}" source="%{#attr.iterator}" >
        <s:iterator>
            <s:property /><br/>
        </s:iterator>
    </s:subset>

subset标签属性如下:
count :Integer: Iterator中被递归的一部分的item的数量 即:你要迭代的集合的大小
Decider org.apache.struts2.util.SubsetIteratorFilter.Decider
用来判断iterator中的item是否包含在最终的subset内部
Source String Iterator的对象 即你要迭代的对象
Start Integer 开始位置 从哪开始迭代。

第二、首先我们来看ui的标签。
1 :<s:select>
<s:select name ="favouriteColor" 
		label ="Favourite Color" 
		tooltip ="please select your favourite color" 
		list ="{'blue','yellow','red','green'}"
		emptyOption ="true"
		headerKey ="None"
		headerValue ="None" />

首先看list ="{'blue','yellow','red','green'}" 注意其中的单引号。其次 这里
引用
headerKey,Key for first item in list. Must not be empty! '-1' and '' is correct, '' is bad.

引用
headerValue: Value expression for first item in list

注意这里对于headKey和headerValue的理解 就是默认显示在这个list选项中第一个值。
headerKey就是设置当用户选择了header选项时,提交的的value,如果使用该属性,不能为该属性设置空值
而headerValue就是显示在页面中header选项内容
另外emptyOption 就是允许选项中存在空置
2、<s:doubleselect>
<s:doubleselect
		name = "region"
		label ="Region"
		list ="{'江苏','广东'}"
		value ="'江苏'"
		doubleName = "state"
		doubleValue = "'南京'"
		doubleList = " top== '江苏'?{'南京','无锡'}:{'广州','深圳'}"
		headerKey="-1"
        headerValue="---------- Please Select ----------"
        emptyOption="true" />

a这里要注意value 表示默认的值,而doubleValue 和doubleList、doubleName表示第二栏的默认值,选项、以及名字.
b.还要注意一个关键的地方,就是value ="'江苏'",因为value是String值,而单单一个“”是无法表示String的,必须要使用'',并且这里不能使用""江苏""这种形式,这会产生歧义。
3、<s:optiontransferselect>
<s:optiontransferselect
		name = "leftSideFootballTeam"
		label = "Football Team"
		list ="{'Chelsea','Arsenal','ManUnited'}"
		headerKey = "headerKey"
		headerValue = "---------- Please Select ----------"
		emptyOption = "true"
		doubleName = "rightSideFootballTeam"
		doubleList ="{'Liverpool','ManCity','Leeds'}"
		doubleHeaderKey = "doubleHeaderKey"
		doubleHeaderValue = "---------- Please Select ----------"
		doubleEmptyOption = "true"
		leftTitle="Left Title"
                rightTitle="Right Title"
		/>

同样这里的double可以理解为第二栏的属性,注意没有doubleLabel这个属性,而multiple表示是否多选
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics