struts1.3设置编码三种方法

本文所写涉及的struts的版本是1.3x.
一、改写struts的ActionServlet. 重写process()方法:

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

配置相应的web.xml文件

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.soft.struts.action.MyActionServlet</servlet-class>
......
</servlet>


二、写一个过滤器类EncodingFilter.class,执行相关路径之前执行这个过滤器类。

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文件:

<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:

<?xml version="1.0" ?>
<catalog name="struts">
<chain name="servlet-standard-preprocess">
<command className="com.soft.struts.action.chain.EncodingChain"/>
</chain>
</catalog>


EncodingChain.java实现org.apache.commons.chain.Command:

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;
}
}


配置相应web.xml文件:

<servlet>
......
<init-param>
<param-name>chainConfig</param-name>
<param-value>
org/apache/struts/chain/chain-config.xml,
/WEB-INF/custom-chain-config.xml
</param-value>
</init-param>
......
</servlet>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值