提交表单时出现错误请求错误400

时间:2017-08-31 10:29:43

标签: spring jsp spring-mvc jstl

我在提交表单时收到错误响应400。我有一个工作正常的表单,然后我添加了一个下拉列表,从DB向用户显示性别。下拉正确显示数据,但当我提交表单时,我收到错误。这只发生在我添加下拉列表时。

RegistrationController.java: -

@Controller
public class RegistrationController {
    final static Logger logger = Logger.getLogger(RegistrationController.class);
    private StaffService staffService;

    @Autowired
    private GenderDao genderDao;

    @Autowired
    public RegistrationController(StaffService staffService) {
        this.staffService = staffService;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/register")
    public String registerStaffPage(Model model) {
        List<Gender> genders = genderDao.findAll();
        Iterator<Gender> genderIterators = genders.iterator();
        Map<Gender, String> genderMap = new LinkedHashMap<Gender, String>();
        while (genderIterators.hasNext()) {
            Gender gender = genderIterators.next();
            genderMap.put(gender, gender.getGender());
        }
        model.addAttribute("gendersMap",genderMap);
        model.addAttribute("staffRegistrationBean", new StaffRegistrationBean());
        return "register";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/registerStaff")
    public String registerStaff(@ModelAttribute("staffRegistrationBean") StaffRegistrationBean staffRegistrationBean,
            @Valid StaffRegistrationBean staffRegistrationBeans, Errors errors, Model model) {
        // if (errors.hasErrors())
        // return "register";
        staffService.createStaff(staffRegistrationBean);
        return "RegistrationDone";
    }

    @PostConstruct
    public void init() {
        logger.debug("RegistrationController Bean has been Initialized.");
    }

    @PreDestroy
    public void destroy() {
        logger.debug("RegistrationController Bean has been Destroyed.");
    }
}

StaffRegistrationBean.java

public class StaffRegistrationBean {
private String userName;
    private String password;
    private String firstName;
    private String lastName;
    private String email;
    private String Organization;
    private String phoneNo;
    private Gender gender;
    // getter and setter follows
}

regsiter.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
</head>
<body>
    <h1>Registration Form</h1>
    <form:form id="regForm" modelAttribute="staffRegistrationBean"
        action="registerStaff" method="post">
        <table>
            <tr>
                <td><form:label path="userName" cssErrorClass="error">
                        <spring:message code="userName" /> : 
                    </form:label> <form:input path="userName" name="username" id="username"
                        cssErrorClass="error" /></td>
            </tr>
            <tr>
                <td>Gender: <form:select path="gender">
                        <form:options items="${gendersMap}" />
                    </form:select>
                </td>
            </tr>
            <tr>
                <td><form:label path="organization" cssErrorClass="error">
                        <spring:message code="organizationName" /> : 
                    </form:label> <form:input path="organization" name="username" id="organization"
                        cssErrorClass="error" /></td>
            </tr>
            <tr>
                <td><form:label path="password" cssErrorClass="error">
                        <spring:message code="password" /> : 
                    </form:label> <form:input path="password" name="password" id="password"
                        cssErrorClass="error" /></td>
            </tr>
            <tr>
                <td><form:label path="firstName" cssErrorClass="error">
                        <spring:message code="firstName" /> : 
                    </form:label> <form:input path="firstName" name="firstname" id="firstname"
                        cssErrorClass="error" /></td>
            </tr>
            <tr>
                <td><form:label path="lastName" cssErrorClass="error">
                        <spring:message code="lastName" /> : 
                    </form:label> <form:input path="lastName" name="lastname" id="lastname"
                        cssErrorClass="error" /></td>
            </tr>
            <%-- <tr>
                <td><form:label path="gender">
                        <spring:message code="gender" /> : </form:label>
                <td><form:select path="${gender}">
                        <form:options items="${genderList}" id="id" itemValue="gender">
                        </form:options>
                    </form:select></td>
            </tr> --%>
            <tr>
                <td><form:label path="email" cssErrorClass="error">
                        <spring:message code="email" /> : 
                    </form:label> <form:input path="email" name="email" id="email"
                        cssErrorClass="error" /></td>
            </tr>
            <tr>
                <td><form:label path="phoneNo" cssErrorClass="error">
                        <spring:message code="phoneNo" /> : 
                    </form:label> <form:input path="phoneNo" name="phoneNo" id="phoneNo"
                        cssErrorClass="error" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="register"></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

**

  • 错误: -

**

HTTP状态400 - 错误请求

输入状态报告

描述服务器由于被认为是客户端错误(例如,格式错误的请求语法,无效的请求消息框架或欺骗性请求路由)而无法或不会处理请求。

GenderMap是一个包含要在屏幕上显示的模型的地图。关键是性别类,价值是性别描述。用户将选择将引用性别表中的ID的性别。

整个代码位于 - https://github.com/iftekharkhan09/ExpenseCalculator_Nex_Gen/tree/DevBranch

转到URL - localhost:8080 / ExpenseCalculator / register

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:2)

正如Razmin所说,spring正在尝试从POST请求的主体填充您的ModelAttribute(类型为StaffRegistrationBean)(由HTML表单自动填充)。

如果查看POST请求正文,您会看到类似 性别:1 的内容。 Spring无法将此Integer映射到Gender(即使此Integer只是引用您的Gender的id。)。 您必须从数据库中手动检索Gender,并将其设置为您的模型。

答案 1 :(得分:0)

当spring无法从HTML表单填充ModelAttribute时会发生这种情况。

staffRegistrationBean ModelAttribute无法将Html的Gender属性转换为您的Gender对象

答案 2 :(得分:0)

在邮递员中启用“主机”标头时它起作用