gpt4 book ai didi

java - Spring MVC HTTP 状态 400 -

转载 作者:行者123 更新时间:2023-12-02 04:00:46 25 4
gpt4 key购买 nike

我正在尝试创建一个出现错误的程序

编辑.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" %>
<%@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>

登录成功.java

package java4s;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java4s.EmployeeService;

@Controller
public class LoginSuccessController {

@Autowired
EmployeeService emp_service;

@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @ModelAttribute("editForm") Employee employee) {

model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}

}

每当我点击提交按钮时,我都会收到 HTTP Status 400 - 错误。但是当我从函数 EditSaveMyInfo 中删除 @ModelAttribute("editForm") EmployeeEmployee 时,错误不会出现?

如何解决该错误?

编辑

员工.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员工而不是@ModelAttribute("editForm") Employee员工

你的 Controller 就变成了

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

绑定(bind)结果对象用于捕获错误请求异常。这是 Spring 标准

关于java - Spring MVC HTTP 状态 400 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34938497/

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