• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Spring form input binding problem

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am new to Spring. I tried to execute a simple user login program. I got the following error

Neither BindingResult nor plain target object for bean name 'user' available as request attribute

My code is as follows

login.jsp

<%@ include file="taglibs.jsp" %>
<%@ include file="header.jsp" %>

<html>
<body bgcolor="lightblue" text="yellow">
<%--<form:form commandName="user" action="/loginUser.htm" method="post">
<form:errors path="user"/>
<form:input path="username"/><br>
<script type="text/javascript">document.getElementById("username").focus();</script>
<form assword path="password"/><br>
<input type="submit" value="Submit"/>
</form:form>
--%>
<form:form action="/loginUser.htm" method="post" commandName="user">
<spring:bind path="username">
<input type="text" name='<c ut value="${status.expression}"/>' value='<c ut value="${status.value}"/>'><br>
</spring:bind>
<spring:bind path="password">
<input type="text" name='<c ut value="${status.expression}"/>' value='<c ut value="${status.value}"/>'><br>
</spring:bind>
<spring:hasBindErrors name="user">
<font color="red"><c ut value="${status.errorMessage}"/></font>
</spring:hasBindErrors>
<%--<spring:hasBindErrors name="user.password">
<font color="red"><c ut value="${status.errorMessage}"/></font>
</spring:hasBindErrors>
--%>
<input type="submit" value="Login"/>
</form:form>
</body>
</html>



LoginController.java


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package controllers;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import services.LoginService;
import users.User;
import validators.LoginValidator;

/**
*
* @author ramesh.k
*/

// This class is a Controller that handles all Spring MVC web requests
public class LoginController extends SimpleFormController{

public LoginController(){
//This method set the type of class for the Command Object
setCommandName("user");
setCommandClass(User.class);
setSessionForm(true);
setBindOnNewForm(true);
setFormView("login");
setSuccessView("success");
System.out.println("commandName,formView,successView,sessionForm properties were set");
}

//declaration of login service bean
private LoginService loginService;

//declaration of login validator
private LoginValidator validator;

/*public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response){
LoginService service=new LoginService();
ModelAndView authentication_data=service.authenticate(request.getParameter("username"),request.getParameter("password"));

setLoginService(service);
return authentication_data;
}

*/
/* @Override
protected Object formBackingObject(HttpServletRequest request) throws ServletRequestBindingException {
System.out.println("Username is "+RequestUtils.getStringParameter(request,"username"));
User user=new User();
user.setUsername(RequestUtils.getStringParameter(request,"username"));
user.setPassword(RequestUtils.getStringParameter(request,"password"));
return user;
}*/


/* @Override
protected ModelAndView showForm(HttpServletRequest request,HttpServletResponse response,BindException errors){
System.out.println("Form is shown");
String viewName=null;
if(errors.hasErrors()){
viewName=getFormView();
System.out.println("Form input has error");
}else{
viewName=getSuccessView();
System.out.println("Form input is correct");
}
return new ModelAndView(viewName);
}*/

// A method is used to do display of a form and a form submit.
@Override
protected ModelAndView onSubmit(Object command) throws Exception{
System.out.println("hello ");
loginService=new LoginService();
User user=(User)command;
setLoginService(loginService);
System.out.println("welcome "+user.getUsername());
/* ModelAndView view_data=null;
if(view_data.getViewName().equals("login")){
showForm(request,response,errors);
}else{

}
*/
return getLoginService().authenticate(user);

}

//getters and setters of loginService property
public LoginService getLoginService() {
return loginService;
}

public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}


LoginService.java
// object of this class is a property of a LoginController class

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package services;

import org.springframework.web.servlet.ModelAndView;
import users.User;

/**
*
* @author ramesh.k
*/

//This class is responsible for doing business logic for the login operation
public class LoginService {

public LoginService(){

}

//This method authenticates the User retrieved by the values entered in the form
public ModelAndView authenticate(User user){
ModelAndView view_data=null;
if(user.getUsername().equals("rameshk")&&user.getPassword().equals("ohmsakthi")){
System.out.println("Inside the method authenticate()");
view_data=new ModelAndView("success");
}else{
view_data=new ModelAndView("login");
}
return view_data;
}
}

When I submit the form, The above mentioned error comes. please help me to solve this.

Thanks and regards,

Ramesh K
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!