`
fuyangchang
  • 浏览: 146249 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

struts标签

阅读更多
 

bean:write

http://www.solol.org/technologic/java/j-struts/#d10e504

bean:write标签将指定的bean的属性值写到当前的JspWriter中,并且可以对输出进行格式化。

下面的代码片段示例了bean:write标签输出User-Agent:

<logic:present header="User-Agent">
<bean:header id="header" name="User-Agent"/>
<bean:write name="header"/>
</logic:present>

下面的代码片段示例了bean:write标签格式化输出当前日期,其中now是在 DataForm中定义的一个java.util.Date类型的域(值为new Date()),format.date.standard是在资源文件中的一个键(format.date.standard=yyyy-MM- dd):

<bean:define id="date" name="dataForm" property="now"/>
<br/><bean:write name="date"/>
<br/><bean:write name="date" format="MM/dd/yyyy"/>
<br/><bean:write name="date" formatKey="format.date.standard"/>

上面代码运行的结果为:

Sun Jun 04 17:04:05 CST 2006
06/04/2006
2006-06-04

bean:resource

bean: resource标签取回指定的web应用程序的资源,以InputStream或String的形式保存到page作用域中并且创建scripting 变量。采用什么形式取决于标签的input属性,如果指定input则以InputStream的形式保存,如果没有指定input则以String的形 式保存。

下面的两个代码片段示例了bean:resource标 签,其中resource.txt是要使用的资源文件。前面的代码片段中没有指定input属性,因此以String的形式处理资源文件,bean: write标签输出资源文件的内容。后面的代码片段中指定了input属性的值,因此以InputStream的形式使用资源文件,两个bean: write标签分别输出InputStream对象的实例名(如java.io.ByteArrayInputStream@16dadf9)和类名(如 class java.io.ByteArrayInputStream)。

<bean:resource id="str" name="/resource.txt"/> <!-- 其它标签通过绑定到page作用域中的属性使用该值 --> <bean:write name="str"/><br/> <!-- JSP脚本通过scripting变量使用该值 --> <% out.println(str+"<br/>"); %> <bean:resource id="is" input="true" name="/resource.txt"/> <!-- 其它标签通过绑定到page作用域中的属性使用该值 --> <bean:write name="is"/><br/> <bean:write name="is" property="class"/> <!-- JSP脚本通过scripting变量使用该值 --> <% out.println(is+"<br/>"); out.println(is.getClass()+"<br/>"); %>

html:checkbox

html:check标签生成一个checkbox。这里的value值可以是true,yes或on。如果您要提交其它的值(如某种形式的标识)应该考虑使用html:multibox标签。

注意:为了正确的处理没有选中的checkbox您必须在reset()中设置对应的属性为false。

下面的代码示例了html:checkbox标签的用法,其中CheckboxForm中声明了三个boolean类型的域,如下:

  
<!---->
private boolean one = false;
private boolean two = false;
private boolean three = false;
<html:checkbox name="checkboxForm" property="one">
One
</html:checkbox>
<html:checkbox name="checkboxForm" property="two">
Two
</html:checkbox>
<html:checkbox name="checkboxForm" property="three">
Three
</html:checkbox>

如果选中后被提交则相应的属性的值为true。

html:multibox

html:multibox标签生成多个checkbox。当您要使用大量的checkbox时使用这个标签非常方便,可以使您避免在ActionForm中声明大量的boolean类型的变量,带之以一个数组就行了。

注意:为了正确的处理没有选中的checkbox您必须在reset()中设置数组的长度为0。

下面的代码示例了html:multibox标签的一般用法,如果被提交则选中的所有checkbox的value值将被提交到multiboxForm中的selectedItems中,这是一个String[]数组。

<html:multibox name="multiboxForm" property="selectedItems" 
value="00001"/>
<html:multibox name="multiboxForm" property="selectedItems"
value="00002"/>

下面的代码示例了html:multibox标签的典型用法:

<logic:iterate id="person" name="multiboxForm" property="persons"> 
<html:multibox property="selectedItems">
<bean:write name="person" property="id"/>
</html:multibox>
<bean:write name="person" property="name"/>
</logic:iterate>

html:errors

html:errors标签和html:messages标签的功能相似,所以我们放到一起来介绍。

html:errors标签将由name属性指定的ActionMessages、ActionErrors、String和String[]直接输出到页面中。

html:messages标签将用由name属性(注意message属性值对它的影响)指定的ActionMessages、ActionErrors、String和String[]创建一个新的属性和scripting变量,使用id属性值作为名称。

html:errors标签和html:messages标签的property属性是用来为errors和messages分类的。我们可以给这两个标签指定property属性,以便只显示某一类的错误或消息。

在资源文件增加了如下的内容:

# -- standard errors -- errors.header=<ul> errors.prefix=<li> errors.suffix=</li> errors.footer=</ul> error=error with none value . error1=error1 with one value is {0} . error2=error2 with two values are {0} , {1} . error3=error3 with three values are {0} , {1} , {2} . error4=error4 with four values are {0} , {1} , {2} ,{3} .

下面的代码示例了actionErrors的构造:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors actionErrors = new ActionErrors(); actionErrors.add("property1", new ActionMessage("error")); actionErrors.add("property2", new ActionMessage("error1","value0")); actionErrors.add("property2", new ActionMessage("error2","value0","value1")); actionErrors.add("property3", new ActionMessage("error3","value0","value1","value2")); actionErrors.add("property3", new ActionMessage("error4","value0","value1","value2","value3")); actionErrors.add("property4", new ActionMessage("error1",new Object[]{"value0"})); actionErrors.add("property4", new ActionMessage("error2",new Object[]{"value0","value1"})); actionErrors.add("property4", new ActionMessage("error3",new Object[]{"value0","value1", "value2"})); actionErrors.add("property5", new ActionMessage("error4",new Object[]{"value0","value1", "value2","value3"})); actionErrors.add("notBundle", new ActionMessage("not a bundle key",false)); return actionErrors; }

errors标签代码示例:

<html:errors/> <br/> <html:errors property="property4"/>

messages标签代码示例:

<logic:messagesPresent> <ul> <html:messages id="message"> <li><bean:write name="message"/></li> </html:messages> </ul> </logic:messagesPresent> <br/> <logic:messagesPresent> <ul> <html:messages id="message" property="property4"> <li><bean:write name="message"/></li> </html:messages> </ul> </logic:messagesPresent>

图示 2. 上面的两段代码示例运行结果相同,如下所示:
运行结果





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics