我正面臨Spring表單驗證問題。 它適用於@NotNull註釋,但有些不適用於@Size。我在下面附上一些代碼。提前致謝。Spring 3表單驗證正在爲@NotNull工作,但不適用於@Size註釋
package com.doctor;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Doctor
{
@NotNull(message="Cannot be Null")
private String uname;
@Size(min=1,max=8,message="Min 1 and Max 8")
private String password;
private String doctor_fname,doctor_lname,address,dept_id,experience,email,phone,resume,image;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
....................
...................
DoctorController
package com.doctor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import javax.validation.Valid;
@Controller
public class DoctorController {
@RequestMapping(value="/registerDoctor", method=RequestMethod.GET)
public String showRegisterForm(Model model)
{
System.out.println("test");
model.addAttribute(new Doctor());
//return "register1";
return "doctor/edit";
}
@RequestMapping(value="/registerDoctor", method=RequestMethod.POST)
public String addDoctorformForm(@Valid Doctor doctor,BindingResult bindingresult)
{
if(bindingresult.hasErrors())
{
return "doctor/edit";
}
else
{
return "doctor/added";
}
}
}
顯示頁面
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<h2>Create a Doctor account</h2>
<sf:form method="POST" modelAttribute="doctor"
enctype="multipart/form-data">
<fieldset>
<table>
<tr>
<td><sf:label path="uname">User Name:</sf:label></td>
<td><sf:input path="uname" /><br /> <sf:errors
path="uname" /> </td>
</tr>
<tr>
<td><sf:label path="password">Password:</sf:label></td>
<td><sf:password path="password" showPassword="true" />
<br /> <sf:errors
path="password" /></td>
</tr>
<tr>
<td><sf:label path="doctor_fname">First Name:</sf:label></td>
<td><sf:input path="doctor_fname" size="15" /><br /> </td>
</tr>
<tr>
<td><sf:label path="doctor_lname">Last Name:</sf:label></td>
<td><sf:input path="doctor_lname" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="address">Address:</sf:label></td>
<td><sf:input path="address" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="dept_id">Department:</sf:label></td>
<td><sf:input path="dept_id" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="experience">Experience:</sf:label></td>
<td><sf:input path="experience" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="email">Email Address:</sf:label></td>
<td><sf:input path="email" size="30" /> <small>In case
you forget something</small><br /> </td>
</tr>
<tr>
<td><sf:label path="phone">Phone:</sf:label></td>
<td><sf:input path="phone" size="30" /> <small>In case
you forget something</small><br /> </td>
</tr>
<tr>
<td><label for="resume">Resume:</label></td>
<td><input name="resume" type="file" />
</tr>
<tr>
<td><label for="image">Profile image:</label></td>
<td><input name="image" type="file" />
</tr>
<tr>
<th></th>
<td><input name="commit" type="submit">
</tr>
</table>
</fieldset>
</sf:form>
我不明白爲什麼@Size密碼不工作。
幫助將不勝感激。
要排除你的問題是不相關的視圖中未顯示的消息,你有嘗試調試bindingresult? – gipinani
做了一個System.out.println(bindingresult.getAllErrors());在控制器內部if(bindingresult.hasErrors())方法...得到這個結果----- [字段'uname'對象'doctor'中的字段錯誤:被拒絕的值[null];代碼[NotNull.doctor.uname,NotNull.uname,NotNull.java.lang.String,NotNull];參數[org.springframework.context.support.DefaultMessageSourceResolvable:codes [doctor.uname,uname];參數[];默認消息[uname]];默認信息[不能爲空]] ............................沒有提供密碼.... –
如果你從那個Doctor對象打印出用戶名的長度? –