1

I know this issue might have been addressed elsewhere but I'm unable to find a satisfactory solution to my problem. Btw, I'm working with spring 3.0.2

Login.jsp

<form:form id="_LoginForm" name="LoginForm" modelAttribute="user" action="login" method="POST">
    <form:input path="username" value=""/>
    <form:input path="password" value=""/>
    <input type="submit" value="Submit"/>

LoginController.java

@RequestMapping(value="login", method=RequestMethod.POST)
public String login(@ModelAttribute("user") User user, BindingResult result) {
    System.out.println("recd request");
    return null;
}

When I try to access the login.jsp page, I get the following error:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:123)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
axtavt
  • 239,438
  • 41
  • 511
  • 482
Deepak Marur
  • 537
  • 1
  • 13
  • 27

2 Answers2

1

Can you please provide your RequestMethod.GET method in the controller? Just want to make sure you are adding the modelAttribute in the GET method as well.

arun
  • 86
  • 1
  • 5
  • Are suggesting to add a sort of setup method which puts the "user" attribute into the model map and then redirect to the login page? – Deepak Marur Oct 20 '11 at 02:17
0

I added the the following method to make this work, though I feel there must be a better way to get this working without having to write a setup method everytime.

applicationContext.xml

<mvc:view-controller path="/" view-name="index" />

index.jsp

<jsp:forward page="index.action"/>

LoginController.java

@RequestMapping(value="index.action", method=RequestMethod.GET)
public String setupLogin(Map<String, Object> modelMap) {        
    modelMap.put("user", new User());
    return "Login";
}
Deepak Marur
  • 537
  • 1
  • 13
  • 27