springMVC註解方式的校驗
除了利用前端框架對錶單裡的資料校驗外,可以利用springMVC框架對資料進行校驗,下面總結一下如何校驗
第一步:準備相應的jar包,這裡就不列舉了,可以參照下面類的匯入包,就是需要的jar包
第二步:寫一個pojo,需要檢驗的欄位上面寫上相應的註解
package iss.com.nanhang;
import java.util.Date;
import javax.validation.constraints.Future;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
public class POJO {
@NotBlank(message="blank ,使用者Id不能為空-1")
@Pattern(regexp="^\\w{6}$",message="為6為英文或者數字")
private String userId;
@Pattern(regexp="^[\\u4e00-\\u9fa5]{2,6}$",message="姓名只能2--6漢字")
private String name;
@Past(message="日期只能當前日期之前。。。")
@Future(message="日期只能當前日期之後。。。")
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
@Range(max=3,min=1,message="身高只在1.0--3.0之間")
private Double height;
@Range(max=200,min=40,message="體重只在40.0--200.0之間")
private Double weight;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
}
第三步:寫一個校驗的控制器
/**
*
*/
package iss.spmvc.valid_ann;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import qh.Log;
/**
* 作者:南航
*2017年11月27日,上午11:08:56
*/
@Controller
@RequestMapping("/valid1")
public class TestValidAnnController {
public TestValidAnnController() {
Log.getLog().info("TestValidAnnController?");
}
@RequestMapping("/open.html")
public ModelAndView open(){ //ModelAndView向前臺傳遞資料
ModelAndView mv=new ModelAndView("/user/TestValid1");
ValidAnnPOJO pojo=new ValidAnnPOJO();
mv.addObject("po", pojo);
return mv;
}
@RequestMapping("/open2.html")
public String open2(Model mo){ //Model的方式向前臺傳遞資料
ValidAnnPOJO po=new ValidAnnPOJO();
mo.addAttribute("po", po);
return "/user/TestValid1";
}
@RequestMapping("/open3.html")
public String open3(Map map){ //Map的方式向前臺傳遞資料
ValidAnnPOJO po=new ValidAnnPOJO();
map.put("po", po);
return "/user/TestValid1";
}
@RequestMapping("/save.html")
public String save(@Valid @ModelAttribute("po") ValidAnnPOJO pojo, BindingResult bind){
//public String save(@Valid @ModelAttribute("po") ValidAnnPOJO pojo, BindingResult bind,Model mo){
// Model可以省略,當前面寫了註解的時候 @ModelAttribute("po")
//@Valid @Validated 二選其一
int errors=bind.getErrorCount();
Log.getLog().info(
"出錯個數="+errors+
",="+pojo.getUserId()+
",="+pojo.getName()+
",="+pojo.getBirthday()+
",="+pojo.getHeight()+
",="+pojo.getWeight()
);
//mo.addAttribute("po", pojo);當上面寫了@ModelAttribute("po")的時候,前面的可以省略
return "/user/TestValid1";
}
}
註解方式的校驗算是最簡單了,上面只是部分的內容,如若有需求的,可以私信我