gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:54 26 4
gpt4 key购买 nike

我是 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 类

@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。当我将 phone 变量从 int 更改为 Integer 时,0 dint 出现。为什么会这样?

最佳答案

问题是值 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;

关于java - Spring MVC - 无法将类型 'java.lang.String' 的属性值转换为所需类型 'java.lang.Integer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41058728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com