和风细雨

世上本无难事,心以为难,斯乃真难。苟不存一难之见于心,则运用之术自出。

如何配置filter解决Struts的中文问题

1.制作一filter类如下:
package com.sitinspring.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;

public class SetCharacterEncodingFilter implements Filter {
    
protected String encoding = null;

    
protected FilterConfig filterConfig = null;

    
protected boolean ignore = true;

    
public void destroy() {
        
this.encoding = null;
        
this.filterConfig = null;
    }


    
public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) 
throws IOException, ServletException {

        
// Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding 
= selectEncoding(request);
            
if (encoding != null)
                request.setCharacterEncoding(encoding);
        }


        
// Pass control on to the next filter
        chain.doFilter(request, response);

    }


    
public void init(FilterConfig filterConfig) throws ServletException {

        
this.filterConfig = filterConfig;
        
this.encoding = filterConfig.getInitParameter("encoding");
        String value 
= filterConfig.getInitParameter("ignore");
        
if (value == null)
            
this.ignore = true;
        
else if (value.equalsIgnoreCase("true"))
            
this.ignore = true;
        
else if (value.equalsIgnoreCase("yes"))
            
this.ignore = true;
        
else
            
this.ignore = false;

    }


    
protected String selectEncoding(ServletRequest request) {
        
return (this.encoding);
    }


}


2.在Web.xml中配置这个filter。
    <!-- filter -->
    
<filter>
        
<filter-name>Set Character Encoding</filter-name>
        
<filter-class>com.sitinspring.filter.SetCharacterEncodingFilter</filter-class>
        
<init-param>
            
<param-name>encoding</param-name>
            
<param-value>UTF-8</param-value>
        
</init-param>
    
</filter>
    
<filter-mapping>
        
<filter-name>Set Character Encoding</filter-name>
        
<url-pattern>/*</url-pattern>
    
</filter-mapping>

3.Action调用示例:
package com.sitinspring.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Action定义处
 * 
@author sitinspring
 *
 * @date 2008-3-20
 
*/

public final class HelloAction extends Action {
    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws Exception {
        request.setCharacterEncoding(
"UTF-8");

        
// 取Form中参数name
        String name = (String) PropertyUtils.getSimpleProperty(form, "name");
        
        request.setAttribute(
"msg""欢迎你!"+name);

        
return (mapping.findForward("SayHello"));
    }

}


以上。

posted on 2008-03-20 12:46 和风细雨 阅读(791) 评论(0)  编辑  收藏 所属分类: Struts


只有注册用户登录后才能发表评论。


网站导航: