Spring MVC - 无法将“java.lang.String”类型的属性值转换为所需类型“java.lang.Integer”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41058728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC - Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer'
提问by Arpan Sharma
Am new to Spring MVC. I have developed a sample application which performs SELECT, INSERT, UPDATE and DELETE.
我是 Spring MVC 的新手。我开发了一个示例应用程序,它执行 SELECT、INSERT、UPDATE 和 DELETE。
Below is my Bean class
下面是我的 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*/
Below is my Controller Class
下面是我的控制器类
@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";
}
}
Below is my JSP 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>
Now am facing two issues. 1. After entering the values and clicked Add Student button, am getting the below error.
现在我面临两个问题。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"]
- By default values which I have declared as int are being displayed in my JSP by default as 0. When I changed the phone variable from int to Integer, 0 dint come. Why is it like this?
- 默认情况下,我声明为 int 的值在我的 JSP 中默认显示为 0。当我将 phone 变量从 int 更改为 Integer 时,0 dint 来了。为什么会这样?
回答by JoB?N
The problem is that value 9962287970 is out of range for type Integer
.
问题是值 9962287970 超出了 type 的范围Integer
。
Integer.MAX_VALUE < 9962287970
To fix this change private Integer phone;
to private Long phone;
OR private String phone;
将此更改修复private Integer phone;
为private Long phone;
ORprivate String phone;
If you use String
you can store other chars like +
to the variable phone
.
如果您使用,String
您可以存储其他字符,如+
变量phone
。
refer http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
参考http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The defauld value of int
is 0 but for Integer
it is not zero since it a object, so you have to initialize the value to 0;
的默认值为int
0 但因为Integer
它不是零,因为它是一个对象,因此您必须将该值初始化为 0;
回答by Smit
You have exceeded the integer range so either use Long
instead of int
or make it of String
variable type. After applying the changes don't forget to drop the table.
您已超出整数范围,因此请使用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;
回答by bbajaj
Issue 1. see this "nested exception is java.lang.NumberFormatException: For input string: "9962287970"". Since its beyond the range of int. try change it to String or Long.
问题 1。请参阅此“嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“9962287970””。因为它超出了 int 的范围。尝试将其更改为 String 或 Long。
Issue 2. In Java, int is a primitive type and it is not considered an object. Only objects can have a null value. As soon as the object of an class initialized all primitives set to their default value which is 0 in case of int.
问题 2. 在 Java 中,int 是一种原始类型,不被视为对象。只有对象可以有空值。一旦类的对象初始化所有原语,就将其设置为它们的默认值,在 int 的情况下为 0。
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
回答by Alex Block
Phone number isn't a number, it's a string of numbers.
电话号码不是数字,而是一串数字。
e.g. 00159 222 111 and 0159 222 111
例如 00159 222 111 和 0159 222 111
are different phone numbers but the same integer.
是不同的电话号码但相同的整数。
回答by Gnanaprakash K
I faced the same issue while testing, actually I am not passing any number from jsp input text(passing empty value - I removed javascript validation for easy testing) which cannot be converted to an integer. I know it is a silly mistake but thought it helps someone who is doing the same mistake.
我在测试时遇到了同样的问题,实际上我没有从 jsp 输入文本中传递任何数字(传递空值 - 我删除了 javascript 验证以便于测试)无法转换为整数。我知道这是一个愚蠢的错误,但认为它可以帮助犯同样错误的人。