Use of HTML: Options in struts

Source: Internet
Author: User
<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:

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.

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:

  

Notes:Product_idIs the variable the JSP will set to the product ID value of the product selected by the user.ProductsIs the name of the bean (it must match what is in contants. product_key -- we probably coders 've said

... collection="<%= Constants.PRODUCT_KEY %>" ...

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

Property = "value"AndLabelproperty = "label"Are automatically set by org. Apache. Struts. util. labelvaluebean, so only use the values shown here.

The resulting HTML will look something like:

<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
WidgetSprocketCOGDunsel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.