0

In my JSP I have a dropdownlist and a submit button when clicking the submit button I lose the value already selected in my list. I am using the jstl because i need to construct other table corresponding the value selected in my list.For that I must call a submit button but the problem; it reset the value selected

I want to know if there is a way to save the value selected in my list even I click a submit button. I work with JSP and eclipse environment.
Thank you for your help.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
kawtousse
  • 3,971
  • 12
  • 38
  • 57
  • Already answered in your previous question: http://stackoverflow.com/questions/2710344/how-to-submit-a-form-without-losing-values-already-selected-at-the-same-form – BalusC Apr 27 '10 at 11:32

1 Answers1

1

You need to preset the inputs with the request parameter values. You can access parameter values in EL by ${param.name}. In case of dropdowns rendered by HTML <select> element, 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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • the problem I found is exactly in the attribut items of the tag forEach because I use a class java to build my options dynamically so I have not the options from the begining to put them in the items I tried the following: but it doesn't work. Thanks – kawtousse Apr 27 '10 at 11:40
  • so I found myself writing java code in the items attribute. and this provoques many problems when running. – kawtousse Apr 27 '10 at 11:42
  • I've posted many answers on your questions before how to use a servlet to preprocess a request for a JSP. Reread them. http://stackoverflow.com/questions/2682069/from-servlet-to-jsp, http://stackoverflow.com/questions/2577918/how-to-send-httpservletresponse-in-the-printwriter-having-an-html-structure-to-a, etc. You don't seem to actually learn from them. Try to work on that as well. I suggest to forget about this all and concentrate you on actually **learning and understanding** how the stuff works. This is a good starting point: http://courses.coreservlets.com/Course-Materials/csajsp2.html – BalusC Apr 27 '10 at 12:09