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

Struts标签使用示例

2008-04-13 01:35 357 查看
html:multibox标签使用说明:

<logic:iterate id="list" name="memberList">
<html:multibox name="list" property="mid">
<bean:write name="list" property="mid"/>
</html:multibox>
<bean:write name="list" property="mname"/>
</logic:iterate>

1.html:select标签

<html:select property="mid">

<html:options collection="memberList" property="mid" labelProperty="mname" />

</html:select>

或者
<html:select name="beanName" property="aPropertyForSelect" size="1">
<html:optionsCollection name="beanName" property="aCollection" label="name" />
</html:select> 其中aCollection是一个集合类型的属性,内含一个JavaBean(该Bean的实现必须符合一定的规则)。

用法2:(列表单选)

<html:select property="aPropertyForSelect" size="3">
<html:optionsCollection name="beanName" property="aCollection" label="name" value="value"/>
</html:select> 其中aCollection是一个集合类型的属性,内含一个JavaBean(该Bean的实现必须符合一定的规则)。

用法3:(列表多选)

<html:select multiple="true" property="aArray" size="3">
<html:optionsCollection name="beanName" property="aCollection" label="name" value="value"/>
</html:select> 其中aCollection是一个集合类型的属性,内含一个JavaBean(该Bean的实现必须符合一定的规则)。aArray是一个数组用来接受所有选中的选项。

3.bean:message标签
<bean:message key="keyFromProperties"/>其中keyFromProperties是资源文件中的一个键值。这个资源文件是在struts-config.xml文件中由设置的。

Struts查找属性文件的方式:
A、.properties扩展名是嵌在代码中的,所以资源文件必须使用这个扩展名
B、Struts并不是单纯去找application.properties文件,而是首先找到application,然后加上下划线"_",然后再加上localeKey(如zh,en),然后再加上.properties($filename_$locale.properties)。 如果这里没有找到和locale对应的资源文件,Struts就使用application.properties,如果这个文件也没找到,Struts会报告错误。
C、确定了文件名之后,Struts使用了ClassLoader类的getResourceAsStream方法得到了一个InputStream
D、然后Struts使用了java.util.Properties类的load方法,将资源文件中的所有资源读出放到了一个HashMap里面
E、然后Struts就可以根据key值取出不同的message了
注释:资源文件的编码为ISO8859。

4.html:cancel标签
<html:cancel>valueForDisplay</html:cancel>其中valueForDisplay是html中button的显示名。该标签用来取消当前的Form。使用<html:cancel />时在Action中典型的处理方法:
if (isCancelled(request)) {
return mapping.findForward("somewherewhencanceled");
}
表示如果cancel则跳转的相应page。

5.logic:iterate标签
<logic:iterate id="id" name="beanName" property="aCollection">
//do something here
<logic:iterate>
该标签用来迭代一个集合。使用indexId可以得到当前的index,如下:
<logic:iterate id="id" indexId="i" name="beanName" property="aCollection">
<bean:write name="i"/> //这里打印出了index的值
</logic:iterate>

6.html:checkbox标签
<html:checkbox name="beanName" property="aPropertyName"/>

<html:checkbox name="beanName" property="aPropertyName">DisplayContent</html:checkbox>
其中aPropertyName一般为boolean类型的变量。get方法可以是getXXX,也可以是isXXX。

7.html:multibox标签
<logic:iterate id="pairValue" name="beanName" property="collection">
<html:multibox property="values">
<bean:write name="pairValue" property="value"/>
</html:multibox>
<bean:write name="pairValue" property="name"/>
</logic:iterate>
其中collection是集合类型。values是一个数组,用来接受提交的value值。

8.html:text标签
用法1:
<html:text property="aValue"/>
用法2:
<html:text name="beanName" property="aValue"/>

9.html:textarea标签
用法1:
<html:textarea property="aValue" />
用法2:
<html:textarea name="beanName" property="aValue" />

10.html:radio标签
用法1:
<html:radio property="aProperty" value="aValue"/>
用法2:
<html:radio name="beanName" property="aProperty" value="aValue"/>
其中aProperty的名字相同的被分为一组。
用法3:
<logic:iterate id="pairValue" name="beanName" property="collection">
<html:radio property="aProperty" idName="pairValue" value="value"/>
</logic:iterate>
其中aProperty用来接受提交的值。

11.html:form标签
<html:form action="aAction"></html:form>

12.bean:header标签
<bean:header id="agent" name="User-Agent"/>该标签用来操作request的header,从header中读出相关信息。

13.logic:match标签
用法1:
<logic:match header="User-Agent" value="Mozilla">Mozilla Browser</logic:match>
从header中取出User-Agent的值,然后与value的值匹配,匹配则显示body,否则不显示body。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: