2011-10-13 49 views
3

我正在使用spring并阅读有关在命令行中使用view(jsp)的信息。我遇到一些麻烦,任何人都可以帮助我如何有效地使用它?在spring视图中使用commandName:IllegalStateException:既不BindingResult也不是普通目标对象

我在我的jsp页面中使用时出现此错误。

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) 

我的代码如下: - 当我点击提交按钮,应该带我到窗体页

<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> 

我的控制器类

@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"; 
} 

最后我得到的jsp页面

<%@ 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" /> 
      <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> 

任何人都可以请帮忙吗?

在此先感谢

+0

可能重复http://stackoverflow.com/questions/8781558/neither-bindingresult-nor-plain-target-对象为bean名称可用请求) – Raedwald

回答

1

你需要一个空user添加到模型。

@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"; 
} 

除了你必须写:(单位:modelAttribute代替commandName

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


反正你的控制器是有点少见。尝试此:

@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 
} 
[既不BindingResult也不对可作为请求属性bean名称纯目标对象(的
+0

嗨拉尔夫,我试图添加空的用户对象,但仍然是相同的错误 – Junaidaj

+1

'modelAttribute =“user”''替换'commandName =“user”' - 我有扩展了答案 – Ralph

相关问题