struts2 trim拦截器 过滤首尾空格

package com.sais.inkaNet.login.interceptor;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**

 * <p>类名称: [自定义trim拦截器]</p>

 * <p>类描述: [过滤请求数据的首尾空格]</p>

 * <p>所属模块: 上汽inkaNet项目</p>

 * <p>创建时间 8/10/11 2:12 PM </p> 

 * @author zouqiang [email protected]

 * @version 1.0

*/

public class TrimInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = 1L;

private static final Logger LOG = Logger.getLogger(TrimInterceptor.class);

    /**

     * <p>方法描述: [trime掉空格]</p>

    *

    * @param invocation 参数说明

    *

    * @return 返回结果的说明

    *

    * @throws Exception 抛出异常的原因

    */

    public String intercept(ActionInvocation invocation) throws Exception {

    LOG.info("过滤请求数据的首尾空格");

        Map<String, Object> parameters = invocation.getInvocationContext().getParameters();

        Set entrySet = parameters.entrySet();

        String[] strings = null;

        String[] values = null;

        int strLength = 0;

        for (Iterator it = entrySet.iterator(); it.hasNext();) {

            Entry entry = (Entry) it.next();

            Object key = entry.getKey();

            Object value = entry.getValue();

            if (value instanceof String[]) {

            values = (String[]) value;//类型转换

            strLength = values.length;

            strings = new String[strLength];

                for (int i = 0; i < strLength; i++) {

                    strings[i] = values[i].trim();

                }

                parameters.put((String) key, strings);

            }

        }

        invocation.getInvocationContext().setParameters(parameters);

        invocation.invoke();

        return null;

    }

}


猜你喜欢

转载自1278051243-qq-com.iteye.com/blog/1144858