3
votes

In my basic spring mvc application I am getting the exception. I am using SimpleFormController. My code is as below.

In my dispatcher-servlet

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
           <!-- <prop key="login.htm">loginController</prop>  -->
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />
<!--  <bean name="loginController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="login"/>  -->

 <bean name="/login.htm" class="com.pack.controller.LoginController" >
 <property name="commandClass" value="com.pack.model.User" />
 <property name="formView" value="login"/>
 <property name="successView" value="index.htm"/>
 </bean>

In my JSP page

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>
    <body>
        <h1>Login</h1>
        <form:form name="loginForm" method="post">
        ${msg}
        <form:input id="username" path="username"/>
        <form:password id="password" path="password"/>
        <input type="submit" value="login" name="login" />
        </form:form>
    </body>
</html>

In my controller class

public class LoginController extends SimpleFormController{

    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        User user =  (User)command;
        System.out.println("Username:" + user.getUsername() + " Password:" + user.getPassword());
        if(user.getUsername().equalsIgnoreCase("sa") && user.getPassword().equalsIgnoreCase("12")){
            return new ModelAndView(new RedirectView(getSuccessView()));
        }
        else
        {
            ModelAndView modelAndView = new ModelAndView("login");
            modelAndView.addObject("msg", "Invalid login");
            return  modelAndView;
        }
    }    
}

Once the user enter correct username and password will redirect to the index page. This is working properly. But the user enter wrong login credentials it throws this error.

exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

Any suggestion to solve this issue?

4

4 Answers

4
votes

You can reference the spring form tag documentation here: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/spring-form.tld.html#spring-form.tld.form

The culprit is that you are not setting the 'modelAttribute' attribute, which gets defaulted to 'command'; hence, your error message. You should set 'modelAttribute' to the name of the attribute in your model which represents a User.

Also, I agree with micha that you should use the newer annotation based controllers.

Below you will find an example of how to properly use modelAttribute. Notice that when you a request gets routed to users/new, the Controller places an empty user (new User()) into the model under the 'user' key. The JSP then specifies 'user' as the modelAttribute (I'm fairly sure this is case sensitive).

@Controller
@RequestMapping(value = "/users")
public class UserController {

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String newForm(Model model) {
        model.addAttribute("user", new User());

        return "users/new";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String create(User user) {
        this.userRepository.save(user);

        return "redirect:/users/" + user.getId();
    }

}

Inside "users/new.jsp":

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
<html>

<body>
<c:url value="/users" var="users"/>

<sf:form action="${users}" method="POST" modelAttribute="user">
    <table>
        <tr>
            <th><label for="full_name">Full Name:</label></th>
            <td><sf:input path="name" size="15" id="full_name" /></td>
        </tr>
    </table>
    <input type="submit" value="Create User" />
</sf:form>

1
votes

Maybe not a real answer to your problem but you should use (newer) annotation based controllers if possible. You are using SimpleFormController which is deprecated in the current spring version.

Check this or this for an alternative.

1
votes

I got a solution for this problem,If we use HTML controls rather than spring controls, this issue will solve.Here in JSP file,

 <%--   <form:input id="username" path="username"/>
        <form:password id="password" path="password"/> --%>
        <input type="text" name="username"/> 
        <input type="password" name="password"/>

This will solve the issue.Thanks for all responses.

0
votes

This is not a valid solution, rather a work-around, as the binding to your command object would not be available.

In a similar error situation, I found out that the commandName in my *-servlet.xml was differrent. When I made it identical to the actual commandBean, the error had disappeared.