【问题标题】:Spring MVC HTTP Status 400 -Spring MVC HTTP 状态 400 -
【发布时间】:2016-01-22 03:29:04
【问题描述】:

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

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>

登录成功.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") 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;
    }
}

【问题讨论】:

  • 你能分享一下编辑表格吗
  • 你的意思是EditSuccess.jsp页面?
  • @AjmalMuhammad,更新了代码。
  • @HarshitShrivastava 您得到的确切错误跟踪是什么?我无法使用您的代码重现错误,并且无法查看我的 EditSuccess 页面
  • @TimeTravel,当我删除 &lt;form:input path="birthdate" /&gt; 时,它工作正常。错误描述为The request sent by the client was syntactically incorrect

标签: java spring jsp spring-mvc


【解决方案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);
    }

绑定结果对象用于捕获错误请求异常。 这是弹簧标准

【讨论】:

  • 导入 javax.validation.Valid;使用这个头文件
  • 导入它给出错误。看来您使用了我的系统中不存在的其他一些弹簧库。
【解决方案2】:

您的问题是 formmodel(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 更改为:

  • (请注意,model 属性更改为 modelAttribute="employee",因为我在doBinding(...) 控制器中使用new ModelAndView("edit", "employee", new Employee()); 映射它)
  • 我将action=.. 路径更改为/saveemployee
  • 在您的 spring-servlet.xml 中添加 &lt;context:annotation-config /&gt; 以及 xmlns:context="http://www.springframework.org/schema/context"in &lt;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>
    
  • 现在使用 --> ...../employeeform

  • 请求表单
  • 提交表单后,它会转到 --> EditSaveMyInfo() (action = "/saveemployee")

注意:我不希望我的回答直接解决你的问题(因为错误可能来自你代码中的其他地方),但我只是希望你在 spring mvc 中获得绑定过程.

【讨论】:

  • 感谢您的回复。正如我已经提到的,由于&lt;form:input path="birthdate" /&gt;,我收到了错误消息。当我删除它时,它工作正常。
【解决方案3】:

HTTP STATUS 400 表示从前端传递到后端的参数有问题。 @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">

【讨论】:

  • 如果我删除 modelAttribute,则会发生错误 neither bindingresult nor plain target object for bean name 'command' available as request attribute!!
  • 我想一一检查这些参数,尤其是BirthDate 。我遇到的大多数 HTTP 400 错误都是由于模型注入失败或某些参数无法转换造成的。
  • 你说得对,我一一检查了所有的文本框。问题在于birthdate 之一。当我删除&lt;form:input path="birthdate" /&gt; 时,它工作正常。 birthdate 有什么问题?
  • 问题是如果使用text box 注入date 类型值,这意味着Spring MVC 需要将值从String 转换为Date。但不是每个String 都可以格式化成Date 值,而是一些特定格式的字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
  • 2021-09-24
  • 2017-12-20
  • 2016-04-20
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多