至于问题“c.jsp文件中的中文到浏览器后显示时也是乱码” 你就要用与第4步类似的方法来重新对*.jsp 文件编码,这时-encoding的参数就要用UTF-8了,假如你用的也是struts studio 5.2 pro for eclipse工具,这一步就免了。它会自动用UTF-8的格式存储。 至于问题“d.由jsp传给bean的中文值,再由bean传回页面又是乱码”的解决,我只是加了个过滤器。 你可以现在web.xml中加入:
Set Character Encoding com.wiley.SetCharacterEncodingFilter
encoding utf-8
ignore true
Set Character Encoding action
然后在你指定的包内加个java文件 我放在了/web-inf/classes/com/wiley 里,下面是源代码: /* * XP Forum * * Copyright (c) 2002-2003 RedSoft Group. All rights reserved. * */ package com.huahang.tj.struts.filters;
/** * Filter that sets the character encoding to be used in parsing the * incoming request, either unconditionally or only if the client did not * specify a character encoding. Configuration of this filter is based on * the following initialization parameters:
*
* encoding - The character encoding to be configured * for this request, either conditionally or unconditionally based on * the ignore initialization parameter. This parameter * is required, so there is no default. * ignore - If set to "true", any character encoding * specified by the client is ignored, and the value returned by the * selectEncoding() method is set. If set to "false, * selectEncoding() is called only if the * client has not already specified an encoding. By default, this * parameter is set to "true". *
* * Although this filter can be used unchanged, it is also easy to * subclass it and make the selectEncoding() method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (sUCh as the values of the Accept-Language * and User-Agent headers, or a value stashed in the current * user´s session.
* * @author John Wong * * @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $ */ public class SetCharacterEncodingFilter implements Filter {
// ------------------------ Instance Variables
/** * The default character encoding to set for requests that pass through * this filter. */ protected String encoding = null;
/** * The filter configuration object we are associated with. If this value * is null, this filter instance is not currently configured. */ protected FilterConfig filterConfig = null;
/** * Should a character encoding specified by the client be ignored? */ protected boolean ignore = true;
// ---------------------Public Methods
/** * Take this filter out of service. */ public void destroy() {
this.encoding = null; this.filterConfig = null;
}
/** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request. * * @param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ 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); }