java 在 spring 视图中使用 commandName:IllegalStateException:既不是 BindingResult 也不是普通目标对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7751965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:16:05  来源:igfitidea点击:

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

javaspringjsp

提问by Junaidaj

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?

我正在使用 spring 并阅读了有关在 view(jsp) 中使用 commandName 的信息。我在使用它时遇到了一些麻烦谁能帮助我如何有效地使用它?

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

在我的 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) 

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

最后我得到的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>

Can anyone please help on this?

任何人都可以帮忙吗?

Thanks in advance

提前致谢

回答by Ralph

You need to add an empty userto the model.

您需要为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";
}

in addition you have to write:

另外你必须写:

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

(modelAttributeinstead of commandName)

(modelAttribute而不是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
}