简体   繁体   English

Spring MVC HTTP状态400-

[英]Spring MVC HTTP Status 400 -

I am trying to create a program where I am getting the error 我正在尝试创建一个出现错误的程序

Edit.jsp 编辑.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 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);
    }

}

Whenever I click on submit button, I get HTTP Status 400 - error. 每当我单击“提交”按钮时,都会收到HTTP Status 400 -错误。 But when I remove @ModelAttribute("editForm") Employee employee from the function EditSaveMyInfo , the error doesnt appears? 但是,当我从功能EditSaveMyInfo删除@ModelAttribute("editForm") Employee employee ,错误不会出现吗?

How can I solve the error? 我该如何解决错误?

EDIT 编辑

Employee.java 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;
    }
}

use @RequestBody @Valid Employee employee instead of @ModelAttribute("editForm") Employee employee 使用@RequestBody @Valid Employee employee而不是@ModelAttribute("editForm") Employee employee

and you controller become 而你的控制者成为

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

binding result object is used for catch bad request exception. 绑定结果对象用于捕获不良请求异常。 this is the spring standard 这是春天的标准

Your problem is that the binding between the form and the model(Employee) was failed. 您的问题是表单模型(雇员)之间的绑定失败。

Change your controller ( Just read it very carefully, you will get a picture about how binding actually works ) to : 将您的控制器更改为:( 只需仔细阅读,您将获得有关绑定实际工作方式的图片 ):

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

}

And change your edit.jsp to : 并将您的edit.jsp更改为:

  • (Note that model attribute is changed to modelAttribute="employee" , because I mapped it with new ModelAndView("edit", "employee", new Employee()); in doBinding(...) controller) (请注意, 模型属性已更改为 modelAttribute="employee" ,因为我在doBinding(...)控制器中使用new ModelAndView("edit", "employee", new Employee());映射了它)
  • I changed action=.. path to /saveemployee 我将action=..路径更改为/ saveemployee
  • Add <context:annotation-config /> there in your spring-servlet.xml also xmlns:context="http://www.springframework.org/schema/context" in <beans.. to enable it. 在您的spring-servlet.xml中添加<context:annotation-config /> ,在<beans..也添加xmlns:context="http://www.springframework.org/schema/context"以启用它。

      <%@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> 
  • Now request the form with --> ...../employeeform 现在使用-> ..... / employeeform申请表格

  • After the form gets submitted it goes to --> EditSaveMyInfo() ( action = "/saveemployee" ) 提交表单后,转到-> EditSaveMyInfo()action =“ / saveemployee”

Note : I don't expect my answer to directly solve your problem (because the errors could come from somewhere else in your codes), but I just want you to get the binding process in spring mvc . 注意: 我不希望我的答案能直接解决您的问题(因为错误可能来自代码中的其他地方),但是我只希望您在spring mvc中获得绑定过程

HTTP STATUS 400 means some thing wrong with the parameters passing from front end to back end. HTTP STATUS 400表示从前端到后端传递的参数有些错误。 @ModelAttribute tells get value from session, I suppose to use @RequestParam should be used at your situation which is getting parameters from http request. @ModelAttribute告诉从会话中获取值,我想在您从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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM