PropertyEditor不传递默认数据

时间:2016-06-21 14:32:20

标签: java spring spring-mvc

我使用spring mvc Framework创建了一些表单,但是当我点击提交按钮时我遇到问题,studentName没有与AdmissionSuccess.jsp文件中的其他数据绑定。我不知道我在StudentNameEditor课程中做错了什么。正在调用setAsText方法,但studentName中未呈现AdmissionSuccess.jsp的值。

我怎样才能让它发挥作用?

StudentAdmissionController

package com.stack;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
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;

@Controller
public class StudentAdmissionController {

    @InitBinder
    public void iniBinder(WebDataBinder binder){
        //binder.setDisallowedFields(new String[] {"studentMobile"});
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        binder.registerCustomEditor(Date.class, "studentDOB", new CustomDateEditor(dateFormat, false));
        //add to handle a specific data. 
        binder.registerCustomEditor(String.class,"studentName", new StudentNameEditor());
    }

    @RequestMapping(value = "/admission.html", method = RequestMethod.GET)
    public ModelAndView getAdmissionForm() {
        ModelAndView model = new ModelAndView("AdmissionForm");
        return model;
    }

    // Aded
    // This method is being called by every request.
    @ModelAttribute
    public void addingCommonObjects(Model model1) {
        model1.addAttribute("headerMessage", "University of Lübeck, Germany ");

    }

    @RequestMapping(value = "/submitAdmissionForm.html", method = RequestMethod.POST)
    public ModelAndView submitAdmissionForm(@ModelAttribute("student1") Student student1, BindingResult result) {
        if (result.hasErrors()) {
            ModelAndView model = new ModelAndView("AdmissionForm");
            return model;

        }

        ModelAndView model = new ModelAndView("AdmissionSuccess");
        return model;
    }
}

StudentNameEditor类

package com.stack;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;

public class StudentNameEditor implements PropertyEditor {
    /*
     * (non-Javadoc) when you will submit the admission form, Spring mvc will run
     * serAsText function this class before performing data binding task for
     * studentName property of student.
     */

    @Override
    public void setAsText(String studentName) throws IllegalArgumentException {

        if(studentName.contains("Mr.") || studentName.contains("Ms.")){
            setValue(studentName);
        }else{
            studentName = "Ms." + studentName;
            setValue(studentName);
        }

    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        // TODO Auto-generated method stub

    }

    @Override
    public String getAsText() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Component getCustomEditor() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getJavaInitializationString() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String[] getTags() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getValue() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isPaintable() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void paintValue(Graphics gfx, Rectangle box) {
        // TODO Auto-generated method stub

    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setValue(Object value) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean supportsCustomEditor() {
        // TODO Auto-generated method stub
        return false;
    }

}

AdmissionForm.jsp

    <!-- Added -->
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<body>
    <h1>${headerMessage}</h1>

    <h1>Student admission form for Engineeering courses</h1>

    <!-- Added -->
    <form:errors path="student1.*" />

    <form action="/FirstSpringMVCProject/submitAdmissionForm.html"
        method="post">

        <table>
            <tr>
                <td>Student's Name:</td>
                <td><input type="text" name="studentName" /></td>
            </tr>

            <tr>
                <td>Student's Hobby:</td>
                <td><input type="text" name="studentHobby" /></td>
            </tr>

            <tr>
                <td>Student's Mobile:</td>
                <td><input type="text" name="studentMobile" /></td>
            </tr>

            <tr>
                <td>Student's DOB:</td>
                <td><input type="text" name="studentDOB" /></td>
            </tr>


            <tr>
                <td>Student's Skill set:</td>
                <td><select name="studentSkills" multiple>
                        <option value="Java Core">Java Core</option>
                        <option value="Spring Core">Spring Core</option>
                        <option value="Spring MVC">Spring MVC</option>
                </select></td>
            </tr>
        </table>

        <table>
            <tr>
                <td>Student's Address:</td>
            </tr>
            <tr>
                <td>county: <input type="text" name="studentAddress.country" />
                </td>
                <td>city: <input type="text" name="studentAddress.city" />
                </td>
                <td>street: <input type="text" name="studentAddress.street" />
                </td>
                <td>pincode: <input type="text" name="studentAddress.pincode" />
                </td>
            </tr>
        </table>


        <input type="submit" value="Submit this form by clicking here" />
    </form>
</body>
</html>

AdmissionSuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<h1>${headerMessage}</h1>

<h3>STUDENT ADMISSION FROM ENGINEERING COURSES</h3>

<h2>Detials submitted by you::</h2>

<table>
    <tr>
        <td>Student Name:</td>
        <td>${student1.studentName}</td>
    </tr>

    <tr>
        <td>Student Hobby:</td>
        <td>${student1.studentHobby}</td>
    </tr>
    <tr>
        <td>Student Mobile:</td>
        <td>${student1.studentMobile}</td>
    </tr>
    <tr>
        <td>Student DOB:</td>
        <td>${student1.studentDOB}</td>
    </tr>
    <tr>
        <td>Student skills:</td>
        <td>${student1.studentSkills}</td>
    </tr>

    <tr>
        <td>Student Adress:</td>
        <td>${student1.studentAddress.country}
        ${student1.studentAddress.city}
        ${student1.studentAddress.street}
        ${student1.studentAddress.pincode}</td>
    </tr>



</table>
<body>

</body>
</html>

enter image description here

1 个答案:

答案 0 :(得分:0)

所以我有StudentNameEditor implements PropertyEditor,且必须是StudentNameEditor extends PropertyEditorSupport。使用下面的代码,它可以正常工作。

package com.stack;

import java.beans.PropertyEditorSupport;

public class StudentNameEditor extends PropertyEditorSupport {
    /*
     * (non-Javadoc) when you will submit the admission form, Spring mvc will run
     * serAsText function this class before performing data binding task for
     * studentName property of student.
     */

    @Override
    public void setAsText(String studentName) throws IllegalArgumentException {

        if(studentName.contains("Mr.") || studentName.contains("Ms.")){
            setValue(studentName);
        }else{
            studentName = "Ms." + studentName;
            setValue(studentName);
        }

    }

}