How to return values to jsp when validation fails

969 Views Asked by At

I have this form that allows user to modify a bean saved in DB:

<html:form action="/confMod">
<table id="inserimento">
<tr>
  <td>Titolo</td>
  <td><html:text property="titolo" value="${libro.titolo }"/>
</tr>
<tr>
  <td>Pagine</td>
  <td><html:text property="pagine" value="${libro.pagine }"/>
</tr>
<tr>
<td>ISBN</td>
<td><html:text property="isbn" value="${libro.isbn }"/>
  </tr>
</table>
<html:hidden property="idLibro" value="${idLibro }"/>
<html:hidden property="opCod" value="modifica" />
<html:submit value=" Inserisci " />

When user submit, the validate() methods checks input. If input isn't correct, Struts return to the jsp that contain form, but values are empty. I want to maintain old values in form... How can I do that? I don't want to access to BD in validate() method nor save it in session.

1

There are 1 best solutions below

0
On

By using the 'value' attribute of the struts html tags you are explicitly setting the form fields' values to whatever the libro bean contains. I am guessing that you populate this bean in the Action that sets up the page initially but not in the Action that processes the page when it is submitted, so in the latter case it will be empty.

Instead, in your first Action you should populate the properties of your Form, for example with the values held in the 'libro' bean. You can then use the html tags like this:

<html:text property="titolo"/>

In the Action class to which the form is submitted your Form will contain the values entered by the user. If you then return to the same JSP page, e.g. because of a validation error, then the same Form object will be in the request, i.e. it will still contain the values entered by the user.