处理get请求时编码的拦截器

来源:互联网 发布:python中文输出 编辑:程序博客网 时间:2024/06/04 00:54
<struts><package name="entity.workspaceManager" extends="json-default" namespace="/entity/manager"><interceptors><interceptor name="urlDecodeFilterInterceptor"class="com.whaty.platform.sso.web.interceptor.UrlDecodeFilterInterceptor"></interceptor><interceptor-stack name="urlDecodeStack"><interceptor-ref name="urlDecodeFilterInterceptor"></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors><default-interceptor-ref name="interceptorStack"></default-interceptor-ref><global-results>


具体实现:

package com.whaty.platform.sso.web.interceptor;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.util.Iterator;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class UrlDecodeFilterInterceptor extends AbstractInterceptor {private static final long serialVersionUID = -2335290125344040914L;private static final Log LOG = LogFactory.getLog(UrlDecodeFilterInterceptor.class);public String intercept(ActionInvocation invocation) throws Exception {Map parameters = invocation.getInvocationContext().getParameters();HttpServletRequest request = ServletActionContext. getRequest(); if("get".equalsIgnoreCase(request.getMethod())){for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {String param = (String) i.next();String[] values = (String[])parameters.get(param);if(values != null && values.length > 0){for (int j = 0; j < values.length; j++) {if(values[j] != null && !"".equals(values[j])){try {values[j] = new String(values[j].getBytes("iso8859-1"),"UTF-8");parameters.put(param, values[j]);} catch (UnsupportedEncodingException e1) {e1.printStackTrace();} }}}}}if("application/x-www-form-urlencoded;charset=UTF-8".equals(request.getContentType())){for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {String param = (String) i.next();String[] values = (String[])parameters.get(param);if(values != null && values.length > 0){for (int j = 0; j < values.length; j++) {if(values[j] != null && !"".equals(values[j])){try {values[j] = URLDecoder.decode(values[j], "UTF-8");parameters.put(param, values[j]);} catch (UnsupportedEncodingException e1) {e1.printStackTrace();} }}}}}return invocation.invoke();}}


原创粉丝点击