本文所写涉及的struts的版本是1.3x.
一、改写struts的ActionServlet. 重写process()方法:
配置相应的web.xml文件
Xml
一、改写struts的ActionServlet. 重写process()方法:
Java代码
Java
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/e2037647da2ec67b600df8247465653a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.soft.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionServlet; public class MyActionServlet extends ActionServlet { protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { /** @todo Override this org.apache.struts.action.ActionServlet method */ request.setCharacterEncoding("GB2312"); super.process(request, response); } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<servlet> <servlet-name>action</servlet-name> <servlet-class>com.soft.struts.action.MyActionServlet</servlet-class> ...... </servlet> 二、写一个过滤器类EncodingFilter.class,执行相关路径之前执行这个过滤器类。 Java代码 收藏代码 package com.soft.struts.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * class EncodingFilter * * @author xiaoqing.yu */ public class EncodingFilter implements Filter { /** encoding */ protected String encoding = null; /** config */ protected FilterConfig config; /** log */ protected static final Log log = LogFactory.getLog(EncodingFilter.class); /** * Mehtod destroy */ public void destroy() { this.config = null; this.encoding = null; } /** * Method doFilter * * @param request * ServletRequest * @param response * ServletResponse * @param filterChain * FilterChain */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { log.info("Before excuted the body of method EncodingFilter.doFilter(), Character Encoding is : " + request.getCharacterEncoding()); log.info("Excuting the body of method EncodingFilter.doFilter()..."); if (request.getCharacterEncoding() == null) { if (this.getEncoding() != null) { request.setCharacterEncoding(this.getEncoding()); } } log.info("After excuted the body of method EncodingFilter.doFilter(), Character Encoding is : " + request.getCharacterEncoding()); filterChain.doFilter(request, response); } /** * Method init * * @param filterConfig * FilterConfig */ public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; this.encoding = this.config.getInitParameter("Encoding"); log.info("Init Parameter Encoding : " + this.encoding); } /** * Method getEncoding * * @return String */ public String getEncoding() { return encoding; } }
二、写一个过滤器类EncodingFilter.class,执行相关路径之前执行这个过滤器类。
Java代码
Java
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/e2037647da2ec67b600df8247465653a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.soft.struts.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * class EncodingFilter * * @author xiaoqing.yu */ public class EncodingFilter implements Filter { /** encoding */ protected String encoding = null; /** config */ protected FilterConfig config; /** log */ protected static final Log log = LogFactory.getLog(EncodingFilter.class); /** * Mehtod destroy */ public void destroy() { this.config = null; this.encoding = null; } /** * Method doFilter * * @param request * ServletRequest * @param response * ServletResponse * @param filterChain * FilterChain */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { log.info("Before excuted the body of method EncodingFilter.doFilter(), Character Encoding is : " + request.getCharacterEncoding()); log.info("Excuting the body of method EncodingFilter.doFilter()..."); if (request.getCharacterEncoding() == null) { if (this.getEncoding() != null) { request.setCharacterEncoding(this.getEncoding()); } } log.info("After excuted the body of method EncodingFilter.doFilter(), Character Encoding is : " + request.getCharacterEncoding()); filterChain.doFilter(request, response); } /** * Method init * * @param filterConfig * FilterConfig */ public void init(FilterConfig filterConfig) throws ServletException { this.config = filterConfig; this.encoding = this.config.getInitParameter("Encoding"); log.info("Init Parameter Encoding : " + this.encoding); } /** * Method getEncoding * * @return String */ public String getEncoding() { return encoding; } }
配置相应web.xml文件:
Xml代码
filter
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/e2037647da2ec67b600df8247465653a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<filter> <filter> <filter-name>encodingFilter</filter-name> <filter-class>com.soft.struts.filter.EncodingFilter</filter-class> <init-param> <param-name>Encoding</param-name> <param-value>GB2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
三、通过Chain来处理:
使用chain的方法来处理的前提是,struts用org.apache.struts.chain.ComposableRequestProcessor类来处理用户提交的请求。不是RequestProcessor类。
写一个custom-chain-config.xml:
EncodingChain.java实现org.apache.commons.chain.Command:
配置相应web.xml文件:
使用chain的方法来处理的前提是,struts用org.apache.struts.chain.ComposableRequestProcessor类来处理用户提交的请求。不是RequestProcessor类。
写一个custom-chain-config.xml:
Java代码
Java
![收藏代码](https://i-blog.csdnimg.cn/blog_migrate/e2037647da2ec67b600df8247465653a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.soft.struts.action.chain; import javax.servlet.http.HttpServletRequest; import org.apache.commons.chain.Command; import org.apache.commons.chain.Context; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.chain.contexts.ServletActionContext; /** * class EncodingChain * @author xiaoqing.yu */ public class EncodingChain implements Command { /** log */ protected static final Log log = LogFactory.getLog(TestChain.class); public boolean execute(Context context) throws Exception { ServletActionContext sacontext = (ServletActionContext) context; HttpServletRequest request = sacontext.getRequest(); request.setCharacterEncoding("GB2312"); log.info("Character Encoding : " + request.getCharacterEncoding()); log.info("Chain test successfully..."); return false; } }