标签:validator int 密码 val ESS 数据 efault null 调用
前端JSP:
Action类:
1 public class LoginAction1 extends ActionSupport { 2 private String username; 3 private String password; 4 5 public void setUsername(String username) { 6 this.username = username; 7 } 8 9 public void setPassword(String password) { 10 this.password = password; 11 } 12 13 @Override 14 public String execute() throws Exception { 15 System.out.println(username); 16 System.out.println(password); 17 return NONE; 18 } 19 20 //数据校验的方法 21 @Override 22 public void validate() { 23 //判断用户名不为空,把左右两边空格去掉字符串长度为0(说明用户名全是空格) 24 if( username== null || username.trim().length() == 0){ 25 //阻止execute方法执行 26 this.addFieldError("username","用户名不能为空"); 27 } 28 //判断密码不为空 29 if(password == null || password.trim().length() == 0 ){ 30 this.addFieldError("password","密码不能为空"); 31 } 32 } 33 public void validateExecute(){//针对某个方法校验 34 if(password.length() <6 || password.length()>12){ 35 this.addFieldError("password", "密码应该在6到12位之间"); 36 } 37 } 38 }
效果:
1 在action所在包下创建一个与action类名相同的 LoginAction2-validation.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE validators PUBLIC 4 "-//Apache Struts//XWork Validator 1.0.3//EN" 5 "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> 6 <!-- 约束在xworkjar包下面,type参考default.xml --> 7 <validators> 8 <field name="username"> 9 <field-validator type="requiredstring"> 10 <message>用户名不能为空!(XML)</message> 11 </field-validator> 12 </field> 13 14 <field name="password"> 15 <field-validator type="requiredstring"> 16 <message>密码不能为空!(XML)</message> 17 </field-validator> 18 </field> 19 </validators>
2 Action类:
1 package com.itheima.struts2.demo4; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class LoginAction2 extends ActionSupport { 6 private String username; 7 private String password; 8 //提供get方法,用于XML的调用 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getUsername() { 13 return username; 14 } 15 public String getPassword() { 16 return password; 17 } 18 public void setPassword(String password) { 19 this.password = password; 20 } 21 22 23 @Override 24 public String execute() throws Exception { 25 System.out.println(username); 26 System.out.println(password); 27 return NONE; 28 } 29 }
前端JSP同上所述
效果页面:
在Action所有包下创建一个文件与Action类名-要访问的方法对应的Action名称-validation.xml
例如:<action name="login2" class="com.itheima.struts2.demo4.LoginAction2" method="execute"></action>
创建校验execute的文件:
LoginAction2-login2-validation.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="password"> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">12</param> <message>密码需要在6-12位之间</message> </field-validator> </field> </validators>
效果如下:
:
二十一 Struts的数据校验两种方式:手动编码和xml校验
标签:validator int 密码 val ESS 数据 efault null 调用
原文地址:https://www.cnblogs.com/ltfxy/p/9846448.html