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.