i am getting an error message:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.lang.Object cannot be cast to com.crimetrack.business.Login
Login.java
public class Login {
private String userName;
private String password;
private boolean loggedin;
public Login(){};
/**
* @return the loggedin
*/
public boolean isLoggedin() {
return loggedin;
}
/**
* @param loggedin the loggedin to set
*/
public void setLoggedin(boolean loggedin) {
this.loggedin = loggedin;
}
/**
* @param userName
* @param password
*/
public Login(String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
@Controller
public class AuthenticationController {
private final Logger logger = Logger.getLogger(getClass());
private AuthenticationManager authenticationManager;
private Login login = new Login();
String message = "Congrulations You Have Sucessfully Login";
String errorMsg = "Login Unsucessful";
@RequestMapping(value="login.htm")
public ModelAndView onSubmit(Object command) throws ServletException {
String userName = ((Login)command).getUserName();
String password = ((Login)command).getPassword();
login.setUserName(userName);
login.setPassword(password);
logger.info("Login was set");
logger.info("the username was set to " + login.getUserName());
logger.info("the password was set to " + login.getPassword());
if (authenticationManager.Authenticate(login) == true){
return new ModelAndView("main","welcomeMessage", message);
}
//return new ModelAndView("main","welcomeMessage", message);
return new ModelAndView("login","errorMsg", "Error!!!");
}
}
Try this solution:
JSP
Controller
Explanation
The annotation
@ModelAttributedoes exactly the same asmodel.addAttribute(String name, Object value). For example,@ModelAttribute Login loginis the same asmodel.addAttribute("login", new Login());.That said, with the
onGetmethod, you passing such an object to your view. Thanks to the attributemodelAttribute="login", the tag<form:form>will look into the model’s list of attributes to find one which name islogin. If it doesn’t find, an exception is thrown.Then, that’s the magic part: with the tag
<form:input path="userName" />, Spring MVC will automatically set the propertyuserNameof the bean which is in themodelAttribute="login"attribute, i.e. in your case,login. If you had put something like<form:input path="wtf" />, it would have thrown an exception, because the beanLogindoesn’t have such a property.So, finally, on your
onSubmitmethod (thanks once again the to annotation@ModelAttribute), you can access to theloginbean, previously autobinded by Spring MVC.Note
I personally (almost) never use a
ModelAndViewinstance, but proceed as follow:The path to the JSP is specified within your web.xml file, for example:
You should read the doc to get more information (cf. 16.5 Resolving views).