首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2 trim拦截器 过滤始末空格

2012-09-06 
struts2 trim拦截器 过滤首尾空格package com.sais.inkaNet.login.interceptor?import java.util.Iterato

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 zouqiang@neusoft.com

?* @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;

?

?

? ? }

}


热点排行