我正在尝试创建一个程序,我收到错误
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" %>
<%@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>
LoginSuccess.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 -
错误。但是当我从函数@ModelAttribute("editForm") Employee employee
中删除EditSaveMyInfo
时,错误不会出现?
如何解决错误?
修改
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;
}
}
答案 0 :(得分:1)
使用@RequestBody @Valid Employee employee
代替@ModelAttribute("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);
}
绑定结果对象用于捕获错误请求异常。 这是春季标准
答案 1 :(得分:1)
您的问题是表单与模型(Employee)之间的绑定失败。
更改您的控制器(只是仔细阅读,您将获得有关绑定实际如何工作的图片):
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;
//Give you a form (edit.jsp) as response after binding happens
@RequestMapping(value = "/employeeform", method=RequestMethod.POST)
public ModelAndView doBinding() {
ModelAndView mv = new ModelAndView("edit", "employee", new Employee());
return mv;
}
// Now the form comes here after it gets submitted.
// And you can start populating the fields
@RequestMapping(value = "/saveemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @ModelAttribute("employee") Employee employee) {
model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
}
将您的edit.jsp
更改为:
modelAttribute="employee"
,因为我将其与new ModelAndView("edit", "employee", new Employee());
控制器中的doBinding(...)
进行了映射action=..
路径更改为 / saveemployee 在 spring-servlet.xml 中添加<context:annotation-config />
xmlns:context="http://www.springframework.org/schema/context"
中的<beans..
以启用它。
<%@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>
现在请求表格 - &gt; ..... / employeeform
EditSaveMyInfo()
( action =“/ saveemployee”)注意:我不希望我的答案直接解决您的问题(因为错误可能来自您的代码中的其他地方),但我只是希望您在spring mvc中获得绑定过程
答案 2 :(得分:0)
HTTP STATUS 400表示从前端传递到后端的参数有问题。
@ModelAttribute
告诉从会话中获取值,我想使用@RequestParam
应该在你的情况下使用,这是从http请求获取参数。
@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">