Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying form values after checking  RSS feed

 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a servlet that checks to see if the required form fields are filled and if they are valid or not. If not, then it dispatches to the JSP form page again along with an error message.

In this form page, I want to display the values user typed in so he doesn't need to retype everything.

For fields like input fields, I know this can be easily done by having something like

<input name="email" type="text" id="email" value="${param.email}" size="45" maxlength="45" />

But what about SELECT that has dozens of possible values, such as a SELECT for month or date? Having something like:

<option value="0" <c:if test="${param.month == 0}">selected</c:if>>January</option>

for each of the 12 or 31 options seem clumsy.

I wonder what's the best way to do this....

Thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can be "clumsy", nothing wrong with that. Or you can eliminate hard-coding the options on the page and send them from the controller as a Map (or other collection) and iterate over them to create the options. The logic to determine the selected option can be in this loop. Or you could write a snippet of Javascript to set the value as the page is loading. Or you could write a custom action (tag) to generate the options.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. you can make the "clumsy" method a bit less clumsy by using the ternary operator rather than a <c:if>.:



(Assumes JSP 2.0)
[ April 07, 2007: Message edited by: Bear Bibeault ]
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the tips, Bear Bibeault.

I think I'll use the ternary method for now.

In another form I need which requires getting data from the database first, I make it go to the controller first to get a result Collection, and then the controller dispatches to the JSP form with the collection as a request attribute for making the SELECT options.

I have heard that Struts and JSF make form validation and repopulating the form much easier, but I am still very new to JSP/Servlets so I want to do them the old fashioned way first.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Wilson:
I have heard that Struts and JSF make form validation and repopulating the form much easier, but I am still very new to JSP/Servlets so I want to do them the old fashioned way first.


That's very wise. Once you are familiar with the technology you'll be in a better position to decide if you need to adopt such frameworks. Personally, I find that they make things harder not easier.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!