user2544272
user2544272

Reputation: 35

Neither BindingResult Nor Plain Target Object For Bean Name ‘Xxx’ Available As Request Attribute

I am getting this error, while trying to configure the spring controllers. I am sure that I have the name correctly matching and it still gives me this error. Here is my controller class. Also the input and output page are same. please help me

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

@Controller
@SessionAttributes
public class UserController {

    protected Log logger = LogFactory.getLog(getClass());

    @Autowired
    UserService userService;

    @RequestMapping(value="/userList")
    public ModelAndView getUserList(){

        List<User> userList = userService.getUserList();        

        return new ModelAndView("userList", "userList", userList);      
    }

    @RequestMapping(value="/findUser")
    public ModelAndView findUser(@ModelAttribute("user") User user,
                                 BindingResult result){ 

        List<User> userResultsList = null;

        //model.addAttribute("user", new User());

        userResultsList = userService.findUser(user.getUsername(), user.getFirstname(),
                user.getLastname());            

        return new ModelAndView("userList", "user", userResultsList);       
    }


}

And my JSP is here

<body>
    <form:form modelAttribute="user" method="post" action="/findUser.html">
        <table>
            <tr>
                <td><form:label path="username">User ID:</form:label></td>
                <td><form:input path="username" /></td>
            </tr>
            <tr>
                <td><form:label path="firstname">First Name</form:label></td>
                <td><form:input path="firstname" /></td>
            </tr>
            <tr>
                <td><form:label path="lastname">Last Name:</form:label></td>
                <td><form:input path="lastname" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="findUser" /></td>
            </tr>
        </table>
    </form:form>    
        <table>
            <tr>
                <td>User Id</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Email Address</td>
                <td>Last Update Date</td>
                <td>Last Update By</td>
            </tr>
        <c:forEach var="list" items="${userList}">
            <tr>
                <td>${list.userId}</td>
                <td>${list.username}</td>
                <td>${list.firstname}</td>
                <td>${list.lastname}</td>
                <td>${list.email}</td>
                <td>${list.lastUpdatedBy}</td>
                <td>${list.lastUpdatedDate}</td>    
                <td><a href="listVendors.html?userId=${list.userId}">assignVendor</a></td>          
            </tr>
        </c:forEach>
    </table>

Upvotes: 0

Views: 1418

Answers (2)

Debojit Saikia
Debojit Saikia

Reputation: 10632

In your JSP you are using two model attributes, named "user" and "userList", which are basically properties of the MODEL object.

When you first come to the page, Spring will try to find the property values with names "user" and "userList" from the Model object (the M in MVC). So you need to set the property values for "user" and "userList" before showing the page to the user (typically from within the method that takes the user to the JSP page), and in your case you can do that by changing the "getUSerList" method as follows:

        @RequestMapping(value="/userList")
        public ModelAndView getUserList(){
             ModelAndView model = new ModelAndView("userList");
             User user=new User();
             List<User> userList = userService.getUserList();        

             model.addObject("user",user);
             model.addObject("userList",userList);
             return model;      
        }

Now this method will take you to the "userList" JSP, with your MODEL populated with "user" and "userList" attribute values.

Upvotes: 0

yname
yname

Reputation: 2245

Try to add following line to your public ModelAndView getUserList() method:

modelAndView.addObject("user", new User());

Upvotes: 0

Related Questions