gpt4 book ai didi

java - 在 spring view : IllegalStateException: Neither BindingResult nor plain target object 中使用 commandName

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:19 24 4
gpt4 key购买 nike

我在使用 spring 时阅读了有关在 View (jsp) 中使用 commandName 的信息。我在使用它时遇到了一些麻烦,任何人都可以帮助我如何有效地使用它吗?

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

我的 Controller 类

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

有人可以帮忙吗?

提前致谢

最佳答案

您需要向模型添加一个空的用户

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

另外你必须写:

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

(modelAttribute 而不是 commandName)


无论如何,您的 Controller 有点不常见。试试这个:

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

关于java - 在 spring view : IllegalStateException: Neither BindingResult nor plain target object 中使用 commandName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7751965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com