2017-09-20 5 views
0

私は最初の春のmvcアプリケーションを構築しようとしています。そして、春のMvcフォーム検証に取り組んでいます。空のフィールドをチェックしようとしています。しかし、@ NotNullアノテーションを使って 'long'型の変数を検証するときは、上記のエラーが出ます。java.lang.String型のプロパティ値を、プロパティphoneの必須のlong型に変換できませんでした。ネストされたexce

私はそれを解決する方法がわかりません。ここで

は私の学生ビーン

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

ロングではなくロングを使用してください。

longはnullになることはできないため、プリミティブ型ではありません。

ロングを使用する必要があります。

+0

ありがとうございます。私の時間を節約してくれてありがとうございます。 –

関連する問題