簡體   English   中英

無法將類型為java.lang.String的屬性值轉換為屬性電話所需的long類型; 嵌套的超驗

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

我正在嘗試構建我的第一個Spring mvc應用程序並正在處理Spring Mvc Form Validation.I正在嘗試檢查空字段。 但是,當使用@NotNull批注驗證'long'類型變量時,會出現上述錯誤。

我不知道該怎么解決。

這是我的學生豆

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>

嘗試使用Long代替long。

因為long永遠不能為null,所以它不是原始類型。

因此,您應該使用Long。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM