至于問(wèn)題“c.jsp文件中的中文到瀏覽器后顯示時(shí)也是亂碼” 你就要用與第4步類似的方法來(lái)重新對(duì)*.jsp 文件編碼,這時(shí)-encoding的參數(shù)就要用UTF-8了,假如你用的也是struts studio 5.2 pro for eclipse工具,這一步就免了。它會(huì)自動(dòng)用UTF-8的格式存儲(chǔ)。 至于問(wèn)題“d.由jsp傳給bean的中文值,再由bean傳回頁(yè)面又是亂碼”的解決,我只是加了個(gè)過(guò)濾器。 你可以現(xiàn)在web.xml中加入:
Set Character Encoding com.wiley.SetCharacterEncodingFilter
encoding utf-8
ignore true
Set Character Encoding action
然后在你指定的包內(nèi)加個(gè)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); }