Spring MVC - 无法将类型'java.lang.String'的属性值转换为必需类型'java.lang.Integer'

时间:2016-12-09 10:51:54

标签: java spring spring-mvc data-binding

是Spring MVC的新手。我开发了一个示例应用程序,它执行SELECT,INSERT,UPDATE和DELETE。

以下是我的Bean类

@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*/

以下是我的控制器类

@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";
}
}

以下是我的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>

现在面临两个问题。 1.输入值并单击“添加学生”按钮后,出现以下错误。

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. 默认情况下,我声明为int的值在我的JSP中默认显示为0.当我将手机变量从int更改为Integer时,0 dint到来。为什么会这样?

5 个答案:

答案 0 :(得分:5)

问题是值9962287970超出了Integer类型的范围。

Integer.MAX_VALUE < 9962287970 

要将此更改private Integer phone;修改为private Long phone;private String phone;

如果您使用String,则可以将+等其他字符存储到变量phone

参考http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

int的默认值为0,但对于Integer,它不是零,因为它是一个对象,所以你必须将值初始化为0;

答案 1 :(得分:1)

您已超出整数范围,因此要么使用Long而不是int,要么使用String变量类型。应用更改后,请不要忘记删除表。

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

答案 2 :(得分:1)

问题1.看到这个&#34;嵌套异常是java.lang.NumberFormatException:对于输入字符串:&#34; 9962287970&#34;&#34;。因为它超出了int的范围。尝试将其更改为String或Long。

问题2.在Java中,int是基本类型,它不被视为对象。只有对象可以具有空值。一旦类的对象初始化所有基元设置为其默认值,如果是int,则为0。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

答案 3 :(得分:0)

电话号码不是数字,而是一串数字。

e.g。 00159 222 111 和 0159 222 111

是不同的电话号码,但是相同的整数。

答案 4 :(得分:-1)

我在测试时遇到了同样的问题,实际上我没有从jsp输入文本中传递任何数字(传递空值-我删除了javascript验证以便于测试),该数字无法转换为整数。我知道这是愚蠢的错误,但认为它可以帮助犯同样错误的人。