struts2开发时通过interceptor拦截器实现输入数据过滤前后空格的功能

来源:互联网 发布:打开软件必备组件 编辑:程序博客网 时间:2024/05/22 14:15

因为做的项目管理项目居多,有很多查询列表页面,少不了名称查询等功能,但是如果每个逻辑中都验证过滤前后空格会比较麻烦,就像用struts的拦截器实现全部输入的字符串过滤来实现,效果不错,但是在实现过程中有几个地方耽误了点时间,也温故知新了些知识,这里总结一下,互相学习一下

        介绍下结构,项目采用SSH框架

        首先在拦截器注册文件interceptorContext.xml中声明拦截器:

[html] view plaincopyprint?
  1. <strong><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  9.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  10.       
  11.     <bean id="onlineInterceptor" class="main.com.eca.interceptor.OnlineInterceptor">        
  12.     </bean>     
  13.     <!-- 空格过滤拦截器 -->  
  14.          <bean id="trimInterceptor" class="main.com.eca.interceptor.TrimInterceptor">       
  15.     </bean>     
  16. </beans></strong>  

 

        然后配置struts.xml文件,加入拦截队列

 

[html] view plaincopyprint?
  1. <strong><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>    
  7.     <include file="config/catalog.xml"/>  
  8.       
  9. <package name="default" extends="struts-default">   
  10.      <interceptors>  
  11.     <interceptor name="onlineInterceptor" class="onlineInterceptor"></interceptor>  
  12.     <interceptor name="trimInterceptor" class="trimInterceptor"></interceptor>  
  13.     <interceptor-stack name="baseInterceptorStack">  
  14.         <interceptor-ref name="onlineInterceptor"></interceptor-ref>  
  15.         <interceptor-ref name="trimInterceptor"></interceptor-ref>  
  16.         <interceptor-ref name="defaultStack"></interceptor-ref>  
  17.     </interceptor-stack>  
  18.     </interceptors>  
  19.     <default-interceptor-ref name="baseInterceptorStack"></default-interceptor-ref>  
  20.       
  21.     <global-results>  
  22.       <result name="user" type="redirect">/index.jsp?url=${url}</result>  
  23.     </global-results>           
  24. </package>  
  25.   
  26. </struts>  
  27. </strong>  

        这里注意一下,在拦截器栈中,trimInterceptor的位置要在defaultStack之上,否则不会生效甚至报错

        下一步就是实现拦截器代码了

        这里在开发中犯了一次二,就是在取参数值的时候,一开始得到value后直接.toString(),然后trim()了,再塞回去,可是报错,参数违法什么的;之后输出了class类型:

[java] view plaincopyprint?
  1. <strong>System.out.println(value.getClass());</strong>  

输出为:

[html] view plaincopyprint?
  1. <strong>class [Ljava.lang.String;  
  2. class [Ljava.lang.String;  
  3. class [Ljava.lang.String;  
  4. class [Ljava.lang.String;  
  5. class [Ljava.lang.String;</strong>  

         当时看成了String类型了,一看没错啊,怎么不对呢,找了会问题,才哗然大悟,form里面的参数其实是数组的,相同name提交后得到是参数是数组的数据,而且显示的class显示为String[]的,然后修改输出一看OK了

[java] view plaincopyprint?
  1. <strong>System.out.println(value.getClass()+"--"+JsonUtil.toJson(value));</strong>  
[html] view plaincopyprint?
  1. <strong>class [Ljava.lang.String;--["22222"]  
  2. class [Ljava.lang.String;--[""]  
  3. class [Ljava.lang.String;--[""]  
  4. class [Ljava.lang.String;--["11111"]  
  5. class [Ljava.lang.String;--["3333"]</strong>  


下面是详细代码,也可以在这里下载:

http://download.csdn.net/detail/songylwq/4896548

 

[java] view plaincopyprint?
  1. <strong>package main.com.eca.interceptor;  
  2.   
  3. import java.util.Iterator;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.   
  7. import org.apache.commons.lang.StringUtils;  
  8.   
  9. import com.opensymphony.xwork2.ActionInvocation;  
  10. import com.opensymphony.xwork2.interceptor.Interceptor;  
  11.   
  12. public class TrimInterceptor implements Interceptor {  
  13.     private static final long serialVersionUID = -2578561479301489061L;  
  14.   
  15.     public void destroy() {  
  16.     }  
  17.   
  18.     /*  
  19.      * @Description:拦截所有参数,去掉参数空格 
  20.      * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation) 
  21.      */  
  22.     public String intercept(ActionInvocation invocation) throws Exception {  
  23.           
  24.         Map map=invocation.getInvocationContext().getParameters();  
  25.         Set keys = map.keySet();  
  26.                   Iterator iters = keys.iterator();  
  27.                 while(iters.hasNext()){  
  28.                     Object key = iters.next();  
  29.                     Object value = map.get(key);  
  30.                     map.put(key, transfer((String[])value));  
  31.                 }  
  32.         return invocation.invoke();  
  33.     }  
  34.       
  35.     /** 
  36.      * @Description: 遍历参数数组里面的数据,取出空格 
  37.      * @param params 
  38.      * @return 
  39.      */  
  40.     private String[] transfer(String[] params){  
  41.         for(int i=0;i<params.length;i++){  
  42.             if(StringUtils.isEmpty(params[i]))continue;  
  43.             params[i]=params[i].trim();  
  44.         }  
  45.         return params;  
  46.     }  
  47.   
  48.     public void init() {  
  49.   
  50.     }  
  51.   
  52. }  
  53. </strong>  
0 0
原创粉丝点击