Java Spring MVC HTTP状态400-
我试图创建一个程序,在那里我得到了错误 Edit.jspJava Spring MVC HTTP状态400-,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我试图创建一个程序,在那里我得到了错误 Edit.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:if test="${session_username != null }">Hello ${session_username}!</c:if>
<font face="verdana" size="2">
${welcomeMessage} <BR><BR>
<form:form action="${pageContext.request.contextPath}/editemployee" method="POST" modelAttribute="editForm">
<form:errors path="studenterrors" />
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Edit</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td>Mobile No.</td>
<td><form:input path="mobileno" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>BirthDate (mm/dd/yyyy)</td>
<td><form:input path="birthdate" /></td>
</tr>
<tr>
<td>Profession</td>
<td><form:select path="profession" items="${professionList}" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</font>
</body>
</html>
每当我点击submit按钮时,我都会得到HTTP状态400-
error。但是当我从函数EditSaveMyInfo
中删除@modeldattribute(“editForm”)Employee Employee
时,错误不会出现吗
如何解决错误
编辑
Employee.java
package java4s;
import java.util.Date;
public class Employee {
private String userid;
private String firstname;
private String lastname;
private String mobileno;
private String username;
private String password;
private String email;
private Date birthDate;
private String profession;
private String studenterrors;
public String getUserid()
{
return userid;
}
public void setUserid(String userid)
{
this.userid = userid;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public String getMobileno()
{
return mobileno;
}
public void setMobileno(String mobileno)
{
this.mobileno = mobileno;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getBirthdate()
{
return birthDate;
}
public void setBirthdate(Date birthDate)
{
this.birthDate = birthDate;
}
public String getProfession()
{
return profession;
}
public void setProfession(String profession)
{
this.profession = profession;
}
public String getstudenterrors()
{
return studenterrors;
}
public void setstudenterrors(String studenterrors)
{
this.studenterrors = studenterrors;
}
}
使用
@RequestBody@Valid Employee Employee
而不是@modeldattribute(“editForm”)Employee Employee
你变成了一个控制者
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @RequestBody @Valid Employee employee,BindingResult result) {
if (result.hasErrors()) {
throw new BadRequestException();
} model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
绑定结果对象用于捕获错误请求异常。
这是spring标准使用
@RequestBody@Valid Employee
而不是@modeldattribute(“editForm”)Employee Employee
你变成了一个控制者
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @RequestBody @Valid Employee employee,BindingResult result) {
if (result.hasErrors()) {
throw new BadRequestException();
} model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
绑定结果对象用于捕获错误请求异常。
这是spring标准HTTP STATUS 400表示从前端到后端传递的参数有问题。
@modeldattribute
告诉我们从会话中获取值,我想在从http请求获取参数的情况下应该使用@RequestParam
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @RequestParam("employee") Employee employee) {
model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
<form:form action="${pageContext.request.contextPath}/editemployee" method="POST">
@RequestMapping(value=“/editemployee”,method=RequestMethod.POST)
public model和view EditSaveMyInfo(ModelMap模型,@RequestParam(“employee”)employee){
model.addAttribute(“消息”、“信息更新”);
返回新模型和视图(“编辑成功”,模型);
}
HTTPStatus 400表示从前端到后端传递的参数有问题。
@modeldattribute
告诉我们从会话中获取值,我想在从http请求获取参数的情况下应该使用@RequestParam
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @RequestParam("employee") Employee employee) {
model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
<form:form action="${pageContext.request.contextPath}/editemployee" method="POST">
@RequestMapping(value=“/editemployee”,method=RequestMethod.POST)
public model和view EditSaveMyInfo(ModelMap模型,@RequestParam(“employee”)employee){
model.addAttribute(“消息”、“信息更新”);
返回新模型和视图(“编辑成功”,模型);
}
您的问题是表单和模型(员工)之间的绑定失败
将控制器(只需仔细阅读,您将了解绑定的实际工作原理)更改为:
并将您的edit.jsp
更改为:
- (注意,模型属性更改为
,因为我在modeldattribute=“employee”
控制器中将其映射为doBinding(…)
)newmodelandview(“编辑”,“员工”,newemployee());
- 我将
路径更改为/saveemployeeaction=..
- 将
添加到spring-servlet.xml中也xmlns:context=”http://www.springframework.org/schema/context“
…..../employeeform”中的
- 表单提交后,将转到-->
(action=“/saveemployee”)EditSaveMyInfo()
注意:我不希望我的答案直接解决您的问题(因为错误可能来自代码中的其他地方),但我只希望您在spring mvc中获得绑定过程。您的问题是表单和模型(员工)之间的绑定失败 将控制器(只需仔细阅读,您将了解绑定的实际工作原理)更改为: 并将您的
edit.jsp
更改为:
- (注意,模型属性更改为
,因为我在modeldattribute=“employee”
控制器中将其映射为doBinding(…)
)newmodelandview(“编辑”,“员工”,newemployee());
- 我将
路径更改为/saveemployeeaction=..
- 将
添加到spring-servlet.xml中也xmlns:context=”http://www.springframework.org/schema/context“
…..../employeeform”中的
- 表单提交后,将转到-->
(action=“/saveemployee”)EditSaveMyInfo()
注意:我不希望我的答案直接解决您的问题(因为错误可能来自代码中的其他地方),但我只希望您在spring mvc中获得绑定过程。您能分享编辑表单吗?您的意思是
editsucture.jsp
page?@AjmalMuhammad,更新了代码。@HarshitShrivastava您得到的确切错误跟踪是什么?我无法用您的代码重现错误,并且无法看到我的editsucture
page@TimeTravel,当我删除
时,它工作正常。错误描述是客户端发送的请求语法不正确
您能否共享编辑表单您的意思是editsucture.jsp
page?@AjmalMuhammad,更新了代码。@HarshitShrivastava您得到的确切错误跟踪是什么?我无法用您的代码重现错误,并且无法看到我的editsucture
page@TimeTravel,当我删除
时,它工作正常。错误描述是客户端发送的请求在语法上不正确
如果我删除modeldattribute
,则会发生一个错误bean name“command”的bindingresult或普通目标对象均不可用作请求属性
!!我想逐一检查这些参数,尤其是出生日期。我得到的大多数HTTP 400故障都是由失败的模型注入或某些参数无法转换引起的。你是对的,我逐个检查了所有的文本框。问题在于出生日期one。当我删除
时,它工作正常。生日
有什么问题?问题是如果使用文本框
插入日期
类型值,这意味着Spring MVC需要将值从字符串
转换为日期
。但并非每个字符串
都可以格式化为日期
值,而是可以格式化为某些特定格式的字符串。bindingresult和plain ta都出现了错误
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:if test="${session_username != null }">Hello ${session_username}!</c:if>
<font face="verdana" size="2">
${welcomeMessage} <BR><BR>
<form:form action="${pageContext.request.contextPath}/saveemployee" method="POST" modelAttribute="employee">
<form:errors path="studenterrors" />
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Edit</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td>Mobile No.</td>
<td><form:input path="mobileno" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>BirthDate (mm/dd/yyyy)</td>
<td><form:input path="birthdate" /></td>
</tr>
<tr>
<td>Profession</td>
<td><form:select path="profession" items="${professionList}" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</font>
</body>
</html>