无法将类型为java.lang.String的属性值转换为属性手机所需的long类型;嵌套的

时间:2017-09-20 11:57:24

标签: java spring spring-mvc

我正在尝试构建我的第一个Spring mvc应用程序并在Spring Mvc Form Validation上工作。我正在尝试检查空字段。但是什么时候验证“长期”#39;带有@NotNull注释的类型变量,它给我上面的错误。

我不知道如何解决它。

这是My Student 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
@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>

1 个答案:

答案 0 :(得分:1)

尝试使用Long而不是long。

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

所以你应该使用Long。