简体   繁体   中英

Failed to convert property value of type java.lang.String to required type long for property phone; nested exce

I am trying to built my first Spring mvc application And working on Spring Mvc Form Validation.I am trying to check for empty field. But when to validate the 'long' type variable with @NotNull annotation it give me the above error.

I don't know how to solve it.

Here is 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;
}

}

And my studentmessage.properties file

  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 Class

@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 file is

<%@ 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>

Try to use Long instead of long.

Because long can never be null it is not primitive type.

So you should use Long.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM