aspose file tools*
The moose likes Other Application Frameworks and the fly likes using SimpleFormController Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "using SimpleFormController" Watch "using SimpleFormController" New topic
Author

using SimpleFormController

Saikiran Madhavan
Greenhorn

Joined: May 15, 2006
Posts: 28
Hi,

i just want to create a simple registration page using Spring/JSP.Since its a form submission i want to use SimpleFormController for this activity.

I am having the 4 following files:-

- registerForm.jsp

- RegisterCommand.java

- RegisterFormController.java (SimpleFormController)

- RegisterFormValidator.java

actually, i am new to Spring and i have not able to understand SimpleFormController from the documentation site, can anyone help me out with this regard about what exactly all these files will hold and how the flow will go.


thanks and regards
saikiran
anand tiwari
Greenhorn

Joined: Jun 22, 2007
Posts: 22
Hi,

The files you have mentioned will hold the following contents:

1) Validator---> The validations you want to keep for your form fields.
2) Command---> your bean object
3) Controller---> Simpleformcontroller
4) jsp---> the jsp file with spring:bind tags associated with the corresponding fields.

I am giving the sample code as follows:

Validator:

public class LoginValidator implements Validator {

private static final int MINIMUM_PASSWORD_LENGTH = 4;

public boolean supports(Class beanName) {
return beanName.equals(Login.class);
}

public void validate(Object target, Errors errors) {
Login login = (Login) target;

ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name","field.required", "usrname required");



jsp:

<spring:bind path="login.name">
<input type="text" name="name"
class="LoginTextBox" value="<c ut value="${status.value}"/>" />
<c:if test="${status.error}">
<c:set var="errormsg" value="${status.errorMessage}"/>
</c:if>

</spring:bind>



Controller:

protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object obj, BindException exp)
throws Exception {
if (request.getSession().getAttribute("userName") == null) {
Login login = (Login) obj;
boolean isExist = loginservice.isValid(login.getName(), login
.getPassword());
if (isExist) {
request.getSession().setAttribute("userName", login.getName());
return new ModelAndView(getSuccessView());
} else {
return new ModelAndView(getFailedView());
}



servlet-action.xml:

<bean id="loginController" class="com.lbadmin.web.LoginController">
<property name="formView"><value>login</value></property>
<property name="successView"><value>redirect:/adminHome.do</value></property>
<property name="failedView"><value>fail</value></property>
<property name="commandClass"><value>com.lbadmin.model.Login</value></property>
<property name="commandName"><value>login</value></property>
<property name="loginservice"><ref bean="loginService"/></property>
<property name="validator"><ref bean="loginValidator"/></property>
</bean>

hope this helps.....


Andy
Saikiran Madhavan
Greenhorn

Joined: May 15, 2006
Posts: 28
hi Andy,

thank you for your reply.

Actually, i just want to register a new user, for this usecase, what will be the Command object POJO looks like.

I am getting the following parameters from the registration form:-

email
firstname
lastname
city
state
zip
address1
address2
phone

etc....

the logic you have given for the login page is a good one to look at.

thanks again...
anand tiwari
Greenhorn

Joined: Jun 22, 2007
Posts: 22
thanks a lot..

The fields that you have mentioned will all form your bean obejct. For example an object like person.java, you need to define the getter and setter methods for all the properties. Also remember one thing, if you are using the spring:bind tag in your jsp, the jsp field names should be same as the bean obejct properties.

andy
Saikiran Madhavan
Greenhorn

Joined: May 15, 2006
Posts: 28
hi all,

i want to create a jsp form using spring-form.tld. I am not sure what is wrong with this piece of code i am writting (newbee).

the error i am getting is given below:



this is my FormController file



my xxx-servlet.xml file is as below



My userRegistration.jsp is given below:

showing only part of the code here



My command object is as below:



can anyone help me out, why i am getting this error and how to recity it.

regards
saikiran
anand tiwari
Greenhorn

Joined: Jun 22, 2007
Posts: 22
Hi,

I cannot see the spring:bind tag in the code snippet you have pasted for your jsp page. Can you please use the jsp code, the one i pasted before and check it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using SimpleFormController
 
Similar Threads
how to populate dropdown box dynamically
difference between AbstractFormController & SimpleFormController
@Controller annotation query
Getting Spring framework
onSubmit() not getting called