A description of the Select option Custom Label __struts

Source: Internet
Author: User

I always thought it would be inconvenient to spell option in the tag of select. So I like to use the label to do this thing, all the information directly written in the JSP, the background does not need to add code, really comfortable, here I do not want to say how to get a custom label, read the book believe it is not difficult to understand, I'd like to say how to get the real value of select in a custom label and display this value by default, OK, let's go.

1. If you are using the Struts tab, the following is the way:

Import org.apache.struts.taglib.html.Constants;

Import Org.apache.struts.taglib.html.SelectTag;

Import Org.apache.commons.beanutils.BeanUtils;

Add the following code in the Doendtag () method,

Selecttag selecttag= (Selecttag) Pagecontext.getattribute (Constants.select_key);

And then, whatever way you use it, get a list.

Then: if (selecttag.ismatched (Beanutil.getproperty (List.get (i), "id"))

Outstr.append ("selected");

Obviously, the actual value of the label for the select of struts is matched, and the default is chosen if the match succeeds.

2. If you are using the spring label, the following is the way:

Import Org.apache.commons.beanutils.BeanUtils;
Import Org.springframework.web.servlet.support.BindStatus;

Add the following code in the Doendtag () method,

Object selecttagvalue = null;

String value = null;

Bindstatus bs = (bindstatus) pagecontext.getattribute (" Org.springframework.web.servlet.tags.form.SelectTag.listValue ");
if (bs!=null) {
Selecttagvalue = Bs.getvalue ();
if (selecttagvalue!=null) value = selecttagvalue.tostring ();
}

And then it's also a list, and the value is compared to the line,

if (value!=null)
if (Value.equals (Beanutils.getproperty (List.get (i), "id"))
Outstr.append ("selected");

3. If it is an ordinary HTML select tag, I have not yet found a way to get its true value through the label itself, but since it is a custom label, you can specify this value yourself.

For example, add a value attribute to the tag, represent the real value, and then compare it to the value.

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.