注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

struts2 IP地址校验(IPv4) IpValidator 自定义校验器  

2010-06-30 11:04:51|  分类: Struts2 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: - 和申 - 和申的个人主页 

自定义校验器代码如下:

IpValidator .java

import com.my163.framework.util.IpUtil;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

/**
 * @author wanggang
 * @version 2010-06-30
 */
public class IpValidator extends FieldValidatorSupport {
 private boolean trim;
 public IpValidator() {
  
  trim = true;
 }

 @Override
 public void validate(Object object) throws ValidationException {
  String fieldName = getFieldName();
  Object value = getFieldValue(fieldName, object);
  if (value == null || value.toString().length() == 0)
   return;
  String compare = (String)value;
  if (trim)
   compare = compare.trim();  
  
  if (!value.getClass().equals(java.lang.String.class) || !IpUtil.verifyIp(compare))
   addFieldError(fieldName, object);
 }
 
 public boolean isTrimed()
 {
  return trim;
 }

 public void setTrim(boolean trim)
 {
  this.trim = trim;
 }
}

--------------------------------------------------------------------------------------------------------------------------------------------------
IpUtil .java

public class IpUtil {
 public static final String ipPattern = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
 public static final boolean verifyIp(String ip)
 {
  if(ip != null && ip.matches(ipPattern)){
   String[] ipA = ip.split("\\.");
   for(String s:ipA){
    if(Integer.parseInt(s)>255){
     return false;
    }
   }
   return true;
  }
  return false;
 }
 public static void  main(String[] args){
  System.out.println(verifyIp("0.sdf.255.255"));
  System.out.println(verifyIp("0.255.255.255"));
  System.out.println(verifyIp("255.255.255.255"));
  System.out.println(verifyIp("0.2.255.255"));
  System.out.println(verifyIp("0.1.255.255"));
  System.out.println(verifyIp("0.7.255.256"));
  System.out.println(verifyIp("192.168.255.255"));
 }
}
----------------------------------------------------------------------------------------------------------------------------------

在项目src下(我用的是eclipse)加入validators.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
    <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
    <validator name="ip" class="com.my163.framework.struts2.validators.IpValidator"/>
</validators>

-------------------------------------------------------------------------------------------------

在校验xml中使用示例:

<field name="domainIp">
  <field-validator type="ip">
   <message>IP地址格式错误</message>
  </field-validator> 
  
 </field>

org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: - 和申 - 和申的个人主页 

  评论这张
 
阅读(1342)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016