struts2開發時經過interceptor攔截器實現輸入數據過濾先後空格的功能

        由於作的項目管理項目居多,有不少查詢列表頁面,少不了名稱查詢等功能,可是若是每一個邏輯中都驗證過濾先後空格會比較麻煩,就像用struts的攔截器實現所有輸入的字符串過濾來實現,效果不錯,可是在實現過程當中有幾個地方耽誤了點時間,也溫故知新了些知識,這裏總結一下,互相學習一下html

        介紹下結構,項目採用SSH框架java

        首先在攔截器註冊文件interceptorContext.xml中聲明攔截器:
spring

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="onlineInterceptor" class="main.com.eca.interceptor.OnlineInterceptor"> </bean> <!-- 空格過濾攔截器 --> <bean id="trimInterceptor" class="main.com.eca.interceptor.TrimInterceptor"> </bean> </beans>

 

        而後配置struts.xml文件,加入攔截隊列apache

 

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="config/catalog.xml"/> <package name="default" extends="struts-default"> <interceptors> <interceptor name="onlineInterceptor" class="onlineInterceptor"></interceptor> <interceptor name="trimInterceptor" class="trimInterceptor"></interceptor> <interceptor-stack name="baseInterceptorStack"> <interceptor-ref name="onlineInterceptor"></interceptor-ref> <interceptor-ref name="trimInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="baseInterceptorStack"></default-interceptor-ref> <global-results> <result name="user" type="redirect">/index.jsp?url=${url}</result> </global-results> </package> </struts> 

        這裏注意一下,在攔截器棧中,trimInterceptor的位置要在defaultStack之上,不然不會生效甚至報錯數組

        下一步就是實現攔截器代碼了框架

        這裏在開發中犯了一次二,就是在取參數值的時候,一開始獲得value後直接.toString(),而後trim()了,再塞回去,但是報錯,參數違法什麼的;以後輸出了class類型:jsp

System.out.println(value.getClass());

輸出爲:學習

class [Ljava.lang.String; class [Ljava.lang.String; class [Ljava.lang.String; class [Ljava.lang.String; class [Ljava.lang.String;

         當時當作了String類型了,一看沒錯啊,怎麼不對呢,找了會問題,才譁然大悟,form裏面的參數實際上是數組的,相同name提交後獲得是參數是數組的數據,並且顯示的class顯示爲String[]的,而後修改輸出一看OK了url

System.out.println(value.getClass()+"--"+JsonUtil.toJson(value));
class [Ljava.lang.String;--["22222"] class [Ljava.lang.String;--[""] class [Ljava.lang.String;--[""] class [Ljava.lang.String;--["11111"] class [Ljava.lang.String;--["3333"]


下面是詳細代碼,也能夠在這裏下載:spa

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

 

package main.com.eca.interceptor; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.commons.lang.StringUtils; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class TrimInterceptor implements Interceptor { private static final long serialVersionUID = -2578561479301489061L; public void destroy() { } /* * @Description:攔截全部參數,去掉參數空格 * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation) */ public String intercept(ActionInvocation invocation) throws Exception { Map map=invocation.getInvocationContext().getParameters(); Set keys = map.keySet(); Iterator iters = keys.iterator(); while(iters.hasNext()){ Object key = iters.next(); Object value = map.get(key); map.put(key, transfer((String[])value)); } return invocation.invoke(); } /** * @Description: 遍歷參數數組裏面的數據,取出空格 * @param params * @return */ private String[] transfer(String[] params){ for(int i=0;i<params.length;i++){ if(StringUtils.isEmpty(params[i]))continue; params[i]=params[i].trim(); } return params; } public void init() { } } 
相關文章
相關標籤/搜索