我无法将表单发布到我的控制器的{post}方法

时间:2015-11-02 07:14:47

标签: spring jsp http spring-mvc servlets

我正在尝试提交表单并将数据发送到我的控制器的post方法,但我得到的是状态400错误我无法理解为什么当我出现此错误时我能够为不同的JSP页面提交post方法。 我在某个地方出错了,请帮助我,我无法找到我的错误

这是我的控制器

@RequestMapping(value = "personal" , method=RequestMethod.GET)
    public String test(HttpServletRequest request, Model model, HttpServletResponse response, @ModelAttribute("personalDetails") PersonalDetails personalDetails) {

        if(personalDetails==null){
            personalDetails = new PersonalDetails();
        }

        Map referenceData = new HashMap();
        Map<Long, String> salutation = new LinkedHashMap<Long, String>();
        salutation.put(1L, "Mr");
        salutation.put(2L, "Miss");
        salutation.put(3L, "Mrs");


        Map<Long,String> bloodGrp = new LinkedHashMap<Long, String>();
        bloodGrp.put(1L, "A+");
        bloodGrp.put(2L, "B+");

        Map<Long, String> gender = new LinkedHashMap<Long, String>();
        gender.put(1L, "MALE");
        gender.put(2L, "Female");

        Map<Long, String> marriageStatus = new LinkedHashMap<Long, String>();
        marriageStatus.put(1L, "Single");
        marriageStatus.put(2L, "divorsy");

        log.info("Before setting model attributes");

        model.addAttribute("salutationList", salutation);
        model.addAttribute("bloodGrp", bloodGrp);
        model.addAttribute("genderList", gender);
        model.addAttribute("marriageStatus", marriageStatus);
        model.addAttribute("persoanalDetails", personalDetails);

        log.info("after setting model attributes");

        return "personal";


    }

    @RequestMapping(value="personaldetails" , method=RequestMethod.POST)
    public String personalDetails(@ModelAttribute("personalDetails") PersonalDetails personalDetails, HttpServletRequest request, HttpServletResponse response){
        this.personalDetails = personalDetails;
        return "success";
    }

我的jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Generic Tool For Employee</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->

<link rel="stylesheet" href="layout/css/bootstrap.min.css">
<link rel="stylesheet" href="layout/css/validator/screen.css">
<link rel="stylesheet"
    href="layout/datepicker/css/bootstrap-datepicker3.min.css">
<link rel="stylesheet" href="layout/css/index.css">
<div class="container-fluid">
    <h2>Personal Details</h2>
    <form:form commandName="personalDetails" class="form-horizontal" id="personalDetailsForm" role="form"
        method="POST" action="${pageContext.request.contextPath}/personaldetails">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">Personal Information</h4>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="salutation">Salutation</label>
                    <div class="col-sm-2">
                        <form:select path="salutation" id="salutation"
                            name="salutation" class="form-control">
                            <!-- <option value="Mr">Mr</option>
                                <option value="Miss">Miss</option>
                                <option value="Mrs">Mrs</option>
                                <option value="Mx">Mx</option> -->
                            <form:options items="${salutationList}" />
                        </form:select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">First Name</label>
                    <div class="col-sm-4">
                        <form:input path="firstName" id="firstName" type="text"
                            class="form-control charcterClss" name="firstName"
                            placeholder="FIRST NAME" maxlength="250" />
                    </div>
                    <label class="col-sm-2 control-label">Middle Name</label>
                    <div class="col-sm-4">
                        <form:input path="middleName" id="middleName" type="text"
                            class="form-control charcterClss" name="middleName"
                            placeholder="MIDDLE NAME" maxlength="250" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Last Name</label>
                    <div class="col-sm-4">
                        <form:input path="lastName" id="lastName" type="text"
                            class="form-control charcterClss" name="lastName"
                            placeholder="LAST NAME" maxlength="250" />
                    </div>
                    <label class="col-sm-2 control-label">Blood group</label>
                    <div class="col-sm-4">
                        <form:select path="bloodGrp" id="bloodGroup"
                            name="bloodGroup" class="form-control">
                            <!--  <option></option>
                                <option>O +ve</option>
                                <option>O -ve</option>
                                <option>B +ve</option>
                                <option>B -ve</option>
                                <option>A +ve</option>
                                <option>A -ve</option>
                                <option>AB +ve</option>
                                <option>AB -ve</option> -->
                            <form:options items="${bloodGrp}" />
                        </form:select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Gender</label>
                    <div class="col-sm-4">
                        <form:select path="gender" id="gender" name="gender"
                            class="form-control">
                            <!--    <option value="Male">Male</option>
                                <option value="FeMale">Fe-Male</option>
                                <option value="Neutral">Transgender</option> -->
                            <form:options items="${genderList}" />
                        </form:select>
                    </div>
                    <label class="col-sm-2 control-label">Email Id</label>
                    <div class="col-sm-4">
                        <form:input path="emailId" class="form-control" type="text"
                            name="personalEmail" id="personalEmail"
                            placeholder="Personal Email" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Contact Number</label>
                    <div class="col-sm-4">
                        <form:input path="contactNo" class="form-control" type="text"
                            id="personalPhoneNno" name="personalPhoneNno"
                            placeholder="Personal Contact" />
                    </div>
                    <label class="col-sm-2 control-label">Alternative Contact
                        Number</label>
                    <div class="col-sm-4">
                        <form:input path="alterContactNo" class="form-control" type="text"
                            id="alternativePhoneNo" name="alternativePhoneNo"
                            placeholder="Alternative Contact" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Date of Birth</label>
                    <div class="col-sm-4">
                        <form:input path="dob" type="text" class="form-control datepicker"
                            placeholder="DOB" id="dOb" name="dOb" />
                    </div>
                    <label class="col-sm-2 control-label">Birth Place</label>
                    <div class="col-sm-4">
                        <form:input path="birthPlace" type="text"
                            class="form-control charcterClss" placeholder="PLACE YOU BORN"
                            id="birthPlace" name="birthPlace" maxlength="250" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Nationality</label>
                    <div class="col-sm-4">
                        <form:input path="nationality" type="text"
                            class="form-control charcterClss" placeholder="NATIONALITY"
                            id="nationality" name="nationality" />
                    </div>
                    <label class="col-sm-2 control-label">Religion</label>
                    <div class="col-sm-4">
                        <form:input path="religion" type="text"
                            class="form-control charcterClss" placeholder="RELIGION"
                            id="religion" name="religion" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">Marital Status</label>
                    <div class="col-sm-4">
                        <form:select path="marriageDetails" id="maritalStatus" name="maritalStatus"
                            class="form-control">
                            <!-- <option value="single">Single</option>
                            <option value="married">Married</option>
                            <option value="divorcee">Divorcee</option> -->
                            <form:options items="${marriageStatus}"/>
                        </form:select>
                    </div>
                    <label class="col-sm-2 control-label">Date of Wedding</label>
                    <div class="col-sm-4">
                        <form:input path="dow" type="text" class="form-control datepicker"
                            placeholder="WEDDING DATE" id="weedingDate" name="weedingDate"/>
                    </div>
                </div>
            </div>
            <div class="panel-footer">
                <input class="submit btn btn-default" type="submit" value="Submit">
            </div>
        </div>
        </form:form>
</div>

<!-- SCRIPTS -->
<script src="js/vendors/jquery-1.9.1.min.js"></script>
<script src="js/vendors/jquery.validate.min.js"></script>
<script src="js/vendors/bootstrap-3.3.5.js"></script>
<script src="datepicker/js/bootstrap-datepicker.min.js"></script>
<script src="js/index.js"></script>
<script src="js/personal.js"></script>

这是我的模特

@Entity
@Table(name="TB_PERSONAL_DETAILS")
public class PersonalDetails implements Serializable {

    @Id
    @GeneratedValue
    @Column(name="PERSON_ID")
    private Long personId;

    @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.DETACH)
    @JoinColumn(name="SALUTATION_ID")
    private SalutationMaster salutation;

    @Column(name="FIRST_NAME")
    private String firstName;

    @Column(name="MIDDLE_NAME")
    private String middleName;

    @Column(name="LAST_NAME")
    private String lastName;

    @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="BLOOD_GRP_ID")
    private BloodGrpMaster bloodGrp;

    @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="GENDER_ID")
    private GenderMaster gender;

    @Column(name="EMAIL_ID")
    private String emailId;

    @Column(name="CONTACT_NO")
    private String contactNo;

    @Column(name="ALTERNATE_CONTACT_NO")
    private String alterContactNo;

    @Column(name="DATE_OF_BIRTH")
    private Date dob;

    @Column(name="BIRTH_PLACE")
    private String birthPlace;

    @Column(name="RELIGION")
    private String religion;

    @Column(name="NATIONALITY")
    private String nationality;

    @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="MARRIAGE_DETAILS_ID")
    private MarriageMaster marriageDetails;

    @Column(name="DOW")
    private Date dow;

    @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="personalDetails")
    private EmployeeDetails employeeDetails;


    public PersonalDetails() {
        // TODO Auto-generated constructor stub
    }

    public Long getPersonId() {
        return personId;
    }
    public void setPersonId(Long personId) {
        this.personId = personId;
    }

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getMiddleName() {
        return middleName;
    }
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public BloodGrpMaster getBloodGrp() {
        return bloodGrp;
    }
    public void setBloodGrp(BloodGrpMaster bloodGrp) {
        this.bloodGrp = bloodGrp;
    }

    public GenderMaster getGender() {
        return gender;
    }
    public void setGender(GenderMaster gender) {
        this.gender = gender;
    }
    public Date getDob() {
        return dob;
    }
    public void setDob(Date dob) {
        this.dob = dob;
    }
    public String getBirthPlace() {
        return birthPlace;
    }
    public void setBirthPlace(String birthPlace) {
        this.birthPlace = birthPlace;
    }
    public String getReligion() {
        return religion;
    }
    public void setReligion(String religion) {
        this.religion = religion;
    }
    public String getNationality() {
        return nationality;
    }
    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public SalutationMaster getSalutation() {
        return salutation;
    }

    public void setSalutation(SalutationMaster salutation) {
        this.salutation = salutation;
    }

    public EmployeeDetails getEmployeeDetails() {
        return employeeDetails;
    }

    public void setEmployeeDetails(EmployeeDetails employeeDetails) {
        this.employeeDetails = employeeDetails;
    }


    /**
     * give the marital details of the employee
     * @return
     */
    public MarriageMaster getMarriageDetails() {
        return marriageDetails;
    }

    /**
     * set the marital details of the employee
     * @param marriageDetails
     */
    public void setMarriageDetails(MarriageMaster marriageDetails) {
        this.marriageDetails = marriageDetails;
    }

    public Date getDow() {
        return dow;
    }

    public void setDow(Date dow) {
        this.dow = dow;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getContactNo() {
        return contactNo;
    }

    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;
    }

    public String getAlterContactNo() {
        return alterContactNo;
    }

    public void setAlterContactNo(String alterContactNo) {
        this.alterContactNo = alterContactNo;
    }

}

提前感谢您解决我的问题,并尽力做到最好。

0 个答案:

没有答案