Sunny
Sunny

Reputation: 928

Bad request error 400 when submitting a form

I am getting Error Response 400 when submitting the form. I have a form which was working fine before i added a drop down list which displays gender to the user from DB. The Drop down is displaying the data correctly but when i am submitting the form i am getting an error. This only happened when i added the drop down list.

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 Status 400 – Bad Request

Type Status Report

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

GenderMap is a map that has the model to display on the screen. The key is Gender class and value is the gender description. The user will select a gender which will be referring to an id in the gender table.

The Entire code is available at - https://github.com/iftekharkhan09/ExpenseCalculator_Nex_Gen/tree/DevBranch

Go to the URL - localhost:8080/ExpenseCalculator/register

Any help will be highly appreciated.

Upvotes: 1

Views: 6103

Answers (3)

Ashwin Anaveri
Ashwin Anaveri

Reputation: 1

It worked when enabled "Host" headers in postman

Upvotes: 0

Alexandre Behaghel
Alexandre Behaghel

Reputation: 69

As Razmin said, spring is trying to populate your ModelAttribute (which is of type StaffRegistrationBean) from the body of the POST request (automatically filled by the HTML form).


If you look at the POST request body you will see something like gender: 1. Spring cannot map this Integer to a Gender (even if this Integer simply refers to the id of your Gender..). You will have to retrieve manually the Gender from your DB, and set it to your Model.

Upvotes: 2

Razmin
Razmin

Reputation: 75

It happens when spring can't populate the ModelAttribute from HTML form.

staffRegistrationBean ModelAttribute can't convert Html's Gender attribute to your Gender object

Upvotes: 0

Related Questions