Search code examples
javascriptjspdom-eventsstruts

Retaining form values for dropdown


Below is my jsp,

I have a dropdown populated with <option> tag.Here my value is getting populated from a Java bean. The dropdown values are getting dynamically populated. Framework am using is struts 1.3.

MyJSP.jsp

<% 
String testIdValue = request.getAttribute("testIdValue");
%>
<body>
    <html:select  property="testId" id="format">
    <option value="0">Select a TestId</option>
    <option value="<%=Mybean.getname()+"^"+Mybean.getdata()%>"<%=(Mybean.getname()+"^"+Mybean.getdata())?"selected=true":""%>><%=Mybean.getname()></option>
    </option>
    </html:select>
    <input type="button" value="submit"/>

</body>

struts-config.xml

<action path="/test"
type="com.test.TestAction"
name="testForm" scope="request" validate="true"
input="/MyJSP.jsp">
<forward name="success" path="/MyJSPResult.jsp"/>
<forward name="failure" path="/MyJSP.jsp"/>
</action>

TestAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form,....)
{
        TestForm testform = (TestForm) from;
        String testIdValue= request.getParameter("testId");
        request.setAttribute("testIdValue",testIdValue);
}

MyJSPResult.jsp

 <% 
    String testIdValue = request.getAttribute("testIdValue");
  %>

Here every time I click on submit, hit any validations the dropdown box value retains to the first option, that 'Select a TestId'. After submitting action I am able get the form values in TestAction. After which I set values in request object. I am not able to retrieve these values either is MyJSP.jsp or MyJSPResult.jsp.

Can anyone tell me how to retain my dropdown value or in general form values in this case?

What I want is: the selected dropwdown value to reflect after

submiting jsp->action->results page->coming back to jsp or
submiting jsp->action->hitting valiadtion->coming back to jsp.

Solution

  • Why don't you use the struts logic tags? You are using bean and scriptlet in jsp for filling your select tag. My suggestion is set your testIdValue in either action form (setter attribute) or session attribute (Best to go with action form) instead of request. Use logic iterate to fill your select tag and logic present tag to check whether your value present in that list. If so, do your html stuff like selected="selected". The code snippet follows,

    <html:select property="testId" id="format" name="your_action_form">
    <option value="0">Select a TestId</option>
    <logic:iterate property="your_list_property" name="your_action_form">
        <logic:present name="testIdValue" property="testId">
            <option value="your_value" selected="selected">your_text</option>
        </logic:present>
        <logic:notPresent name="testIdValue" property="testId">
            <option value="your_value">your_text</option>
        </logic:notPresent>
    </logic:iterate>
    </html:select>
    

    Also you can use logic equal/notEqual tag instead of present/notPresent tags. Let me know if this helps.