創建項目
使用idea創建一個spring-boot項目,依賴選上 web, validation, freemarker 即可
先看看效果
創建實體類
創建并加上注解,代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public class person implements serializable { @notnull @length (min = 3 , max = 10 ) // username長度在3-10之間 private string username; @notnull @min ( 18 ) // 年齡最小要18歲 private integer age; @notnull // 使用正則來驗證字段,message: 設置驗證失敗的信息 @pattern (regexp = "[\\w-\\.]+@([\\w-]+\\.)+[a-z]{2,3}" , message = "郵箱格式不正確" ) private string email; public string getemail() { return email; } public void setemail(string email) { this .email = email; } public string getusername() { return username; } public void setusername(string username) { this .username = username; } public integer getage() { return age; } public void setage(integer age) { this .age = age; } } |
配置controller
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@controller public class webcontroller extends webmvcconfigureradapter { @override public void addviewcontrollers(viewcontrollerregistry registry) { //添加一個路由并設置頁面名字 registry.addviewcontroller( "/results" ).setviewname( "results" ); } @getmapping ( "/" ) public string showform(person person) { return "form" ; } @postmapping ( "/" ) public string checkpersoninfo( @valid person person, bindingresult bindingresult, redirectattributes redirectattributes) { // 使用bindingresult來驗證表單數據的正確性 if (bindingresult.haserrors()) { // 將提交的表單內容原封不動的返回到頁面再展示出來 redirectattributes.addflashattribute( "person" , person); return "form" ; } return "redirect:/results" ; } } |
注:不要忘了 @valid 注解
表單頁面
表單頁面里用到了spring標簽來取驗證失敗的數據,在spring-boot里想用spring標簽可以將 spring.ftl 文件在放在 resources 里面,然后在 application.yml 里添加上如下配置即可
spring.ftl 文件路徑: org.springframework.web.servlet.view.freemarker.spring.ftl
1
2
3
4
|
spring: freemarker: settings: auto_import: /spring.ftl as spring |
表單頁面代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<form action= "/" method= "post" > <div class = "form-group" > <label for = "username" >username</label> < @spring .bind "person.username" /> <input type= "text" id= "username" name= "username" value= "${person.username!}" class = "form-control" placeholder= "username" /> <span class = "text-danger" >< @spring .showerrors "" /></span> </div> <div class = "form-group" > <label for = "age" >age</label> < @spring .bind "person.age" /> <input type= "number" id= "age" name= "age" value= "${person.age!}" class = "form-control" placeholder= "age" /> <span class = "text-danger" >< @spring .showerrors "" /></span> </div> <div class = "form-group" > <label for = "email" >email</label> < @spring .bind "person.email" /> <input type= "text" id= "email" name= "email" value= "${person.email!}" class = "form-control" placeholder= "email" /> <span class = "text-danger" >< @spring .showerrors "" /></span> </div> <input type= "submit" value= "submit" class = "btn btn-sm btn-primary" /> </form> |
注:一定要先使用 <@spring.bind "person.username"/>
將字段綁定好,下面再使用 <@spring.showerrors ""/>
來取出來錯誤信息
參考
https://spring.io/guides/gs/validating-form-input/
總結
以上所述是小編給大家介紹的spring boot里增加表單驗證hibernate-validator并在freemarker模板里顯示錯誤信息(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://tomoya92.github.io/2018/01/11/spring-boot-hibernate-validator-freemarker