java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗方式
Spring MVC 提供了兩種數(shù)據(jù)校驗的方式:
1、基于 Validator 接口。
2、使用 Annotation JSR - 303 標準進行校驗。
基于 Validator 接口的⽅式需要自定義 Validator 驗證器,每⼀條數(shù)據(jù)的驗證規(guī)則需要開發(fā)者⼿動完成, 使⽤ Annotation JSR - 303 標準則不需要⾃定義驗證器,通過注解的方式可以直接在實體類中添加每個屬性的驗證規(guī)則,這種方式更加方便,實際開發(fā)中推薦使用。
1.定義實體類Account
package entity; import lombok.Data; @Data public class Account { private String name; private String password; }
2.自定義驗證器 AccountValidator,實現(xiàn) Validator 接口
package validator; import entity.Account; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; public class AccountValidator implements Validator { @Override public boolean supports(Class<?> aClass) { return Account.class.equals(aClass); } @Override public void validate(Object o, Errors errors) { ValidationUtils.rejectIfEmpty(errors,"name",null,"姓名不能為空"); ValidationUtils.rejectIfEmpty(errors,"password",null,"密碼不能為空"); } }
Validator中有兩個方法, supports是判斷傳入是否為目標類,如果是的話則進行下一步,數(shù)據(jù)驗證的操作。
3.控制器
package Mycontroller; import entity.Account; import entity.Person; 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.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; @Controller @RequestMapping("/validator") public class ValidatorHandler { @GetMapping("/login") public String login(Model model){ model.addAttribute("account",new Account()); return "login"; } @PostMapping("/login") public String login(@Validated Account account, BindingResult bindingResult){ if(bindingResult.hasErrors()){ return "login"; } return "index"; }
4. springmvc.xml 配置驗證器
<bean id="accountValidator" class="com.southwind.validator.AccountValidator"> </bean> <mvc:annotation-driven validator="accountValidator"></mvc:annotation-driven>
5.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <title>Title</title> </head> <body> <form:form modelAttribute="account" action="/validator/login" method="post"> 姓名:<form:input path="name"></form:input><form:errors path="name"></form:errors><br> 密碼:<form:input path="password"></form:input><form:errors path="password"></form:errors><br> <input type="submit" value="登錄"> </form:form> </body> </html>
以上就是java開發(fā)之基于Validator接口的SpringMVC數(shù)據(jù)校驗方式的詳細內(nèi)容,更多關于SpringMVC數(shù)據(jù)校驗的資料請關注腳本之家其它相關文章!
相關文章
運行Jar包出現(xiàn)提示xxx中沒有主清單屬性報錯問題解決方法
這篇文章主要介紹了運行Jar包出現(xiàn):xxx中沒有主清單屬性報錯,當出現(xiàn)報錯:xxx中沒有主清單屬性,解決方法也很簡單,在pom.xml配置中,加上相應配置即可,需要的朋友可以參考下2023-08-08解決java-jar報錯:xxx.jar 中沒有主清單屬性的方法
在使用 java -jar xxx.jar 命令運行 Java 應用程序時,遇到了以下錯誤:xxx.jar 中沒有主清單屬性,這個錯誤表示 JAR 文件缺少必要的啟動信息,本文將介紹該錯誤的原因以及如何通過修改 pom.xml 文件來解決,需要的朋友可以參考下2024-11-11IntelliJ IDEA 2023.2正式發(fā)布新UI和Profiler轉(zhuǎn)正(最新推薦)
北京時間2023年7月26日,IntelliJ IDEA 2023.2正式發(fā)布,IntelliJ IDEA 2023.2 引入 AI Assistant(AI助手),通過一組由 AI 提供支持的功能助力開發(fā),今天給大家分享IntelliJ IDEA 2023.2正式發(fā)布新UI和Profiler轉(zhuǎn)正,感興趣的朋友一起看看吧2023-10-10SpringBoot集成minio實現(xiàn)文件上傳和刪除功能
這篇文章主要介紹了SpringBoot集成minio實現(xiàn)文件上傳和刪除功能,詳細介紹每個功能的實現(xiàn)步驟和代碼示例,具有一定的參考價值,感興趣的可以了解一下2023-11-11