》Java中去除字符串两边空格拦截器StringTrimInterceptor 作者:左轩、Shiny
struts.xml:
<interceptors>
<interceptor name="stringTrimInterceptor" class="com.huawei.ssi.interceptor.StingTrimInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="stringTrimInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
StringTrimInterceptor.java
package com.huawei.ssi.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class StringTrimInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -7544853178671114344L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//获取ActionContext对象
ActionContext context = invocation.getInvocationContext();
//获取客户端参数 存放在一个map集合中
Map<String, Object> params = context.getParameters();
for(String key:params.keySet()){
String[] values = (String[]) params.get(key);
for(int i=0;i<values.length;i++){
values[i]=values[i].trim();
}
//处理完毕 筛回去
params.put(key, values);
}
//执行下一个拦截器
return invocation.invoke();
}
}