java - 无法将 java.lang.String 类型的属性值转换为属性 Phone 所需的 long 类型;嵌套的异常

标签 java spring spring-mvc

我正在尝试构建我的第一个 Spring mvc 应用程序并致力于 Spring Mvc 表单验证。我正在尝试检查空字段。但是当使用 @NotNull 注释验证“long”类型变量时,它会给出上述错误。

不知道怎么解决

这是我的学生 Bean

public class Student {
@NotNull
@Pattern(regexp="[A-Za-z]+")
private String name;
@Size(min=2, max=10) 
private String father;

private String cnic; 
@NotBlank
private String email;

@NotNull 
private long phone;

public long getPhone() {
    return phone;
}

public void setPhone(long phone) {
    this.phone = phone;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCnic() {
    return cnic;
}

public void setCnic(String cnic) {
    this.cnic = cnic;
}

public String getFather() {
    return father;
}

public void setFather(String father) {
    this.father = father;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

还有我的 Studentmessage.properties 文件

  Pattern.student1.name=The should not contain any Digits 
        Size.student1.father=please give the {0} between {2} and {1} characters 
        NotBlank.student1.email=please the email are requried 
        NotNull.student1.phone=sPlease provide integer data

Controller 类

@Controller
@RequestMapping(value="/student")
public class StudentController {

    @RequestMapping(value="/Student",method=RequestMethod.GET)
    public String std(Model m){
        m.addAttribute("message", "Welcome To Registration ");
        return "studentreg";
    }

    public ModelAndView insert(@Valid @ModelAttribute("student1") Student student1,BindingResult result)   //ModelAttribute is used to directly connect the form data to the bean class
    {
if(result.hasErrors()){

    ModelAndView model=new ModelAndView("studentreg");

    return model;
}
    ModelAndView mo=new ModelAndView("success");
    mo.addObject("message","user detail");
    mo.addObject("stu", student1);
    return mo;
    }
}

Jsp文件是

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h2 align="center">${ message}</h2>

<form:errors path="student1.phone" style="color:red"/><br>
<form:form name="form" method="post" action="stu" >
<div align="center">
<table>
${ error}
   <tr>  
     <td>Name :</td><td><input type="text" name="name" placeholder="EnterName"></td>
     </tr>
                <tr>
                    <td>Father Name</td>
                    <td><input type="text" name="father" placeholder="EnterFatherName"/></td>
                </tr>
                 <tr>
                    <td>CNIC</td>
                    <td><input type="text" name="cnic" placeholder="Enter CNIC"/></td>
                </tr>
                  <tr>
                    <td>Email</td>
                    <td><input type="text" name="email" placeholder="Enter Email"/></td>
                </tr>
                  <tr>
                    <td>Phone:</td>
                    <td><input type="text" name="phone" placeholder="Enter Phone"/></td>
                  <form:errors path="phone"/>
                </tr>

                  <tr>

                    <td><input type="submit" name="submite" value="Send"/></td>
                </tr>
</table>
</table>
</table>
</div>


</table>
</form:form>
</body>
</html>

最佳答案

尝试使用 Long 而不是 long。

因为 long 永远不能为 null,所以它不是原始类型。

所以你应该使用 Long。

关于java - 无法将 java.lang.String 类型的属性值转换为属性 Phone 所需的 long 类型;嵌套的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46321428/

相关文章:

java - Java算子的疑惑

java - 如何在两个不同的类中使用相同的方法(JavaFX 和 Scenebuilder)?

java - 无法从 Hashmap 获取对象的值,即使它返回相同的哈希码

java - 使用junit启动和控制spring应用程序

spring - spring mvc 应用程序中的 main() 方法

html - 在 Thymeleaf 中为图像设置 CSS 样式

java - buildroot 与 jamvm 2.0 for java 8

java - 使用 Postgres 在 Spring Data JPA 中的事务中发出脏读

spring-mvc - 在 Spring MVC 3.0 中是否有基于注解的方式来全局注册 PropertyEditor?

java - 显示默认页面直到服务器启动