views:

208

answers:

1

Hi everyone,

I am using jstl with dropdown lists.

When i click submit button i success the specification but values int dropdownlists are reinitialized.

So I want to submit form without loosing the values already selected in the form because I need to stay always at the same level in the form.To be more clear, user choose a value from ddl and click edit button to show other options and fill them at the same form without loosing what he has selected.

I have tried to deal like that...

<form action="myjsp.jsp" method="post">
<input type="Submit" value="Edit">

...but it doesn't work.

Thank you for your help.

+1  A: 

You need to preset the inputs with the request parameter values. You can access parameter values in EL by ${param.name}. Basically:

<input type="text" name="foo" value="${param.foo}">

Note that this is XSS sensitive. You always need to sanitize the user inputs. You can use the JSTL functions taglib for this.

<input type="text" name="foo" value="${fn:escapeXml(param.foo)}">

In case of dropdowns rendered by HTML <select> element, it's a bit trickier. You need to set the selected attribute of the HTML <option> element in question. You can make use of the ternary operator in EL to print the selected attribute whenever the option value matches the request parameter value.

Basic example:

<select name="foo">
   <c:forEach items="${options}" var="option">
       <option ${param.foo == option ? 'selected' : ''}>${option}</option>
   </c:forEach>
</select>
BalusC
I want to understand more this example so to apply it but I did not display my options
kawtousse
So what we put exactly in each parameterI am noviceThank you
kawtousse
Just substitute "foo" with the actual parameter name. The parameter name is specified in the `name` attribute of the input field.
BalusC