What is the best non-framework (JSP/Servlet only) way to create a form such that it can:
- Prepopulate with default values / data loaded from a database
- Redisplay submitted values which fail validation
It seems fairly straight forward to do one or the other, but creating a form that supports both simultaneously is tricky.
For example using
${param.scheduledDate}
works great for re-displaying a date on validation failure, but can’t easily be set programmatically on page load. Conversely,
${myBean.scheduledDate}
works great for displaying values loaded from a database, but can’t re-display data on validation failure since the bean is using an object of type Date, not string.
A few things come to mind, but non really seem all that good:
- use an intermediate bean with just strings
- use a servlet filter to set parameters on page load
Do it in the view side. Use the conditional operator
?:in EL:Note that I assume that unlike GET the POST request doesn’t prepopulate the
${bean}before validation has succeeded.This verboseness is by the way one of the reasons why MVC frameworks exist. JSF for example handles this fully transparently by the
EditableValueHolderinterface which is implemented by among othersUIInputcomponents.