2016-06-21 15 views
0

Я создал некоторую форму с весной mvc Framework, но я столкнулся с проблемой при нажатии кнопки отправки, studentName не связан с другими данными в файле AdmissionSuccess.jsp. Я не знаю, что я делаю неправильно в классе StudentNameEditor. Вызывается метод setAsText, но значение studentName не отображается в AdmissionSuccess.jsp.PropertyEditor не передает данные по умолчанию

Как я могу заставить его работать?

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

+0

В вашем классе 'StudentNameEditor', когда вы' setValue() 'в методе метода нет, чтобы фактически установить имя Студента. Вам нужно будет добавить там код, чтобы убедиться, что при вызове 'setValue (studentName);' что имя задано правильно. –

ответ

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); 
     } 

    } 

}