开发者

Working with commandName in spring view: IllegalStateException: Neither BindingResult nor plain target object

开发者 https://www.devze.com 2023-04-12 22:24 出处:网络
I was working with spring and read about the use of commandName in view(jsp). I have a little trouble working with it can anyone help me how to effectively use it?

I was working with spring and read about the use of commandName in view(jsp). I have a little trouble working with it can anyone help me how to effectively use it?

i get this error when i use in my jsp page.

SEVERE: Servlet.service() for servlet [dispatcher] in context with path     

[/hibernateAnnotaionWithSpring] threw exception [An exception occurred processing JSP  

page /WEB-INF/jsp/userForm.jsp at line 17

14:     <table>
15:         <tr>
16:             <td>User Name :</td>
17:             <td><form:input path="name" /></td>
18:         </tr>
19:         <tr>
20:             <td>Password :</td>

Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for   

bean name 'user' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) 

my code is as follows:- when i click on submit button it should take me to userForm page

<form action="login.htm">
         <table class="hundredPercent headerBackground" cellspacing="0">
         <tr>
            <td class="textLabel sevenPer">
                Email
            </td>
            <td class="textField sevenPer">
                  <input type="text" value="" name="username">
            </td>
            <td class="textLabel sevenPer">
                Password
            </td>
            <td class="textField sevenPer">
                  <input type="password" value="" name="password">
            </td>
            <td class="textField">
                   <input type="submit" value="Enter" name="submit" class="buttonSubmit">
            </td>
            <td>

            </td>
            <td class="textLabel">
                <a href="list.htm" >Register</a>
            </td>
         </tr>
    </table>
    </form>

my controller class

@RequestMapping("/login.htm")
public String login(HttpServletRequest request,
        HttpServletResponse response,ModelMap model) throws Exception {
        String userName= request.getParameter("username");
        String password= request.getParameter("password");
        System.out.println("name===="+userName);
        return "userForm";
}

finally my resultant jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
</head>
<body>

<form:form action="add.htm" commandName="user" method="POST">
<table>
    <tr>
        <td>User Name :</td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td>Password :</td>
        <td><form:password path="password" /></td>
    </tr>
    <tr>
        <td>Gender :</td>
        <td><form:radiobutton path="gender" value="M" label="M" />       

<form:radiobutton
            path="gender" value="F" label="F" /></td>
    </tr>
    <tr>
        <td>Country :</td>
        <td><form:select path="country">
            <form:option value="0" label="Select" />
            <form:option value="India" label="India"开发者_如何学Python />
            <form:option value="USA" label="USA" />
            <form:option value="UK" label="UK" />
        </form:select></td>
    </tr>
    <tr>
        <td>About you :</td>
        <td><form:textarea path="aboutYou" /></td>
    </tr>
    <tr>
        <td>Community :</td>
        <td><form:checkbox path="community" value="Spring"
            label="Spring" /> <form:checkbox path="community" 
  value="Hibernate"
            label="Hibernate" /> <form:checkbox path="community"       
  value="Struts"
            label="Struts" /></td>
    </tr>
    <tr>
        <td></td>
        <td><form:checkbox path="mailingList"
            label="Would you like to join our mailinglist?" /></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="Register"></td>
    </tr>
</table>
</form:form>
</body>
</html>

Can anyone please help on this?

Thanks in advance


You need to add an empty user to the model.

@RequestMapping("/login.htm")
public String login(HttpServletRequest request,
        HttpServletResponse response, ModelMap model) throws Exception {
        String userName= request.getParameter("username");
        String password= request.getParameter("password");
        System.out.println("name===="+userName);

        model.addAttribute("user", new User(...)):
        return "userForm";
}

in addition you have to write:

<form:form action="add.htm" modelAttribute="user" method="POST">

(modelAttribute instead of commandName)


Anyway your controller is a bit uncommon. Try This:

@RequestMapping(value="/login.htm" method = RequestMethod.GET)
public String loginForm(ModelMap model) throws Exception {
        model.addAttribute("user", new User("", "")):
        return "userForm";
}

@RequestMapping(value="/login.htm" method = RequestMethod.POST)
public String login(User user) throws Exception {
        String userName= user.getUsername();
        String password= user.getPassword();
        ...

}

/** The command class */
public class User{
   private String username;
   private String password:

   Getter and Setter
}
0

精彩评论

暂无评论...
验证码 换一张
取 消