简体   繁体   中英

Java -Struts 1.2 combobox question

I am using java (Struts 1.2). My first page is a login page. After a successful login, the second page is displayed The second page contains a form in which one of the fields is a combo box. I want this combo box should be filled so that the user can select the option.

I have tried many methods, like using html:option collections, and html:optionsCollections etc. but it is showing errors like bean not found.

Can any body give me a sample working code.

Thanks

<%
List< LabelValueBean> hourList = new ArrayList<LabelValueBean>();
pageContext.setAttribute("hourList", hourList);
%>

<html:select property="endhour">
    <html:options collection="hourList" property="value" labelProperty="label" />
<html:select>

The property on html:select is the one in your form which will be filled when the user selects something from the combobox.

--- sorry couldn't format it to show the code clearly

The use of html tag required to be in html:form and that both must have an associated bean, you configure it in struts.xml.

<form-beans>
    <form-bean name="LoginForm"
        type="struts.forms.LoginForm" />
</form-beans>

<action  name="LoginForm" path="/logon" type="common.Logon"
        parameter="cmd">
        <forward name="error" path="/html/login/login.jsp?login_error=1" />
        <forward name="success" path="/login.do" />            
</action>

What I can tell you is this:

  • Once you successfully login (through your LoginAction in this example), let your action forward to another action which will populate your list.
  • Populate your list and save it in the request (like so, request.setAttribute("contents", list) . Now forward your page to a JSP file.
  • On the JSP file, you will have to read your contents (as stored in request.setAttribute method) and do something of this effect:

Example:

<html:select property="selectedValue">
    <html:options name="contents" />
</html:select>

More information on Struts HTML TagLib.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM