Spring MVC - a Échoué à convertir la valeur de la propriété type " java.lang.Chaîne "de type" java.lang.Integer'

Suis nouveau sur Spring MVC. J'ai développé un exemple d'application qui effectue SELECT, INSERT, UPDATE et DELETE.

Ci-dessous est ma classe d'Haricot

@Id
@Column
private int student_id;
private String name;
private String age;
private String city;
private String country;
private Integer phone;
private int hsc;
private int sslc;
private int college;

/*getter and setters*/

Ci-dessous est ma Classe de Contrôleur

@Controller
public class StudentController {

private static final Logger logger = Logger.getLogger(StudentController.class);

@Autowired
private StudentService studentService;

@RequestMapping(value = "/students", method = RequestMethod.GET)
public String listStudents(Model model){
    if(logger.isDebugEnabled()){
        logger.debug("listStudents method is executed!");
    }

    logger.error("This is an error message", new Exception("Testing"));
    model.addAttribute("student", new StudentDO());
    model.addAttribute("listStudents", this.studentService.listStudents());
    return "students";
}


@RequestMapping(value = "/students/add", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("student") StudentDO studentDO){
    if(studentDO.getStudent_id() == 0){
        /**new person, add it*/
        this.studentService.addStudent(studentDO);
    }else{
        /**existing person, call update*/
        this.studentService.updateStudent(studentDO);
    }
    return "redirect:/students";
}
}

Ci-dessous est ma page JSP

<c:url var="addAction" value="/students/add" ></c:url>
<form:form action="${addAction}" commandName="student">
<table>
<c:if test="${!empty student.name}">
<tr>
<td>
<form:label path="student_id">
<spring:message text="STUDENT_ID"/>
</form:label>
</td>
<td>
<form:input path="student_id" readonly="true" size="8"  disabled="true" />
<form:hidden path="student_id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td>
<form:label path="age">
<spring:message text="Age"/>
</form:label>
</td>
<td>
<form:input path="age" />
</td>
</tr>
<tr>
<td>
<form:label path="city">
<spring:message text="City"/>
</form:label>
</td>
<td>
<form:input path="city" />
</td>
</tr>
<tr>
<td>
<form:label path="country">
<spring:message text="Country"/>
</form:label>
</td>
<td>
<form:input path="country" />
</td>
</tr>
<tr>
<td>
<form:label path="phone">
<spring:message text="Phone"/>
</form:label>
</td>
<td>
<form:input path="phone" />
</td>
</tr>
<tr>
<td>
<form:label path="hsc">
<spring:message text="HSC"/>
</form:label>
</td>
<td>
<form:input path="hsc" />
</td>
</tr>
<tr>
<td>
<form:label path="sslc">
<spring:message text="SSLC"/>
</form:label>
</td>
<td>
<form:input path="sslc" />
</td>
</tr>
<tr>
<td>
<form:label path="college">
<spring:message text="College"/>
</form:label>
</td>
<td>
<form:input path="college" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty student.name}">
<input type="submit" value="<spring:message text="Edit Student"/>" />
</c:if>
<c:if test="${empty student.name}">
<input type="submit" value="<spring:message text="Add Student"/>" />
</c:if>
</td>
</tr>

Maintenant je suis confronté à deux problèmes.
1. Après avoir entré les valeurs et cliqué sur Ajouter un bouton de Student, obtiens l'erreur ci-dessous.

org.springframework.validation.BindException:     
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'student' on field 'phone': rejected value [9962287970]; 
codes [typeMismatch.student.phone,typeMismatch.phone,typeMismatch.java.lang.Integer,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [student.phone,phone]; arguments []; 
default message [phone]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'phone'; 
nested exception is java.lang.NumberFormatException: For input string: "9962287970"]
  1. Par des valeurs par défaut que j'ai déclaré en tant que int sont affichés dans ma JSP par défaut à 0. Quand j'ai changé le téléphone variable de type int Entier, 0 dint venir. Pourquoi est-il comme cela?
InformationsquelleAutor Arpan Sharma | 2016-12-09