》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();
}
}