How To
Restore Form Data in Java
I am working with a registration form. Currently
when I press submit button and if any input is wrong , I am redirected
to same page with all the previously filled fields blank. I want that my
previously filled value persist.
How to achieve this is Java? I am using Java,
.page file and xsl for rendering things. I can't use PHP for this.
Either I can do this on my xsl file or on Java script.
If this is just a single jsp page form (i.e the form itself
doesn't span multiple pages) you don't need to use the session.
Maybe this MVC example will help? This is just a basic
example. There are many different methods to do the same thing.
(M) = Model
(V) = View
(C) = Controller
First Part
----------
1) User directs browser to RegistrationFormController
(C) url
2) LoginFormController is a servlet that creates an empty
RegistrationForm bean and puts it on the request object, named "regDetails".
The bean has methods such as setFirstName(String name) and getFirstName().
3) LoginFormController forwards (dispatches) the request
to registrationForm.jsp (V)
4) registrationForm.jsp uses JSTL to put values from
bean (empty at the moment) in to the form values.
e.g.
<input type="text" value="<c:out value="${regDetails.firstName}"/>
"/>
<input type="text" value="<c:out value="${regDetails.firstName}"/>
"/>
Second Part
--------------
1) User fills in form and clicks submit button. This
posts the data to the RegistrationController (C)
2) RegistrationController takes the submitted values
from the request object and creates a RegistrationForm bean using these
values.
3) RegistrationController then validates the bean values
to make sure they are of the correct data types, all mandatory fields have
values etc.
4) If no problems, calls some business logic (M) to do
whatever you need to do with the data (save it to a database maybe?) -
Note - this could also provide validation errors for example if the username
already exists in the database etc.
5) If problems, forward to success.jsp to display a success
message.
6) If problems, put bean back in response object. Add
error message to response object. Forward to registrationForm.jsp again.
That should at least get you started...
Do you have a Java Problem?
Ask It in The Java
Forum
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|