kawtousse
kawtousse

Reputation: 4141

how to submit a form without losing values already selected at the same form

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.

Upvotes: 0

Views: 2644

Answers (1)

BalusC
BalusC

Reputation: 1109132

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>

Upvotes: 1

Related Questions