我是靠谱客的博主 自觉冬日,这篇文章主要介绍struts中html:options的使用,现在分享给大家,希望可以做个参考。

<html:options>

html:options was born to use data in an ArrayList. Do NOT use a Vector, because it is Synchronized and will cause problems if more than one person uses your app at a time.

Given that you have a list of products with their product ID, make a drop-down that returns the product ID. First you set up your data in your controller:

复制代码
1
2
3
4
5
ArrayList prod = new ArrayList(); prod.add( new org.apache.struts.util.LabelValueBean( "Widget", "20" ) ); prod.add( new org.apache.struts.util.LabelValueBean( "Sprocket", "29-2a" ) ); prod.add( new org.apache.struts.util.LabelValueBean( "Cog", "29s" ) ); prod.add( new org.apache.struts.util.LabelValueBean( "Dunsel", "943" ) );

LabelValueBean takes the first String argument as the displayed name and the second as the value returned. We’ll see more about this in a bit.

Now that you have prod set up, you pass that as a session or request bean to the JSP.

复制代码
1
request.setAttribute( Constants.PRODUCT_KEY, prod );

(the Constants.PRODUCT_KEY is set to our bean name: "products"; we use this to centralize bean name management)

To use the data in the JSP, add the following code:

复制代码
1
2
3
<html:select property="product_id" styleClass="prodselect" > <html:options collection="products" property="value" labelProperty="label" /> </html:select>

Notes: product_id is the variable the JSP will set to the product ID value of the product selected by the user. products is the name of the bean (it must match what is in Contants.PRODUCT_KEY -- we probably could've said

复制代码
1
... collection="<%= Constants.PRODUCT_KEY %>" ...

in place of hard-coding the name. The choice is up to you.

property="value" and labelProperty="label" are automatically set by org.apache.struts.util.LabelValueBean, so only use the values shown here.

The resulting HTML will look something like:

复制代码
1
2
3
4
5
6
<select name="product_id" size="1"> <option value="20">Widget</option> <option value="29-2a">Sprocket</option> <option value="29s">Cog</option> <option value="943">Dunsel</option> </select>

Which renders as

最后

以上就是自觉冬日最近收集整理的关于struts中html:options的使用的全部内容,更多相关struts中html:options内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部