环境:srping2.5.4+sitemesh2.2.1+weblogic
问题:在页面上sitemesh里的模板是没有乱码的,在有的子系统页面显示正常,在有的子系统显示时乱码。
修改页面编码方式均不好使。
解决办法:
解决方式1(硬解决):
第一步:修改weblogic配置文件,把启动后所有文件变成utf-8
D:/oracle/Middleware/user_projects/domains/base_domain/bin/setDomainEnv.cmd
@REM Clustering support (edit for your cluster!) if "%ADMIN_URL%"=="" ( |
第二步:修改POM,升级sitemesh
<dependency> <groupId>opensymphony</groupId> <artifactId>sitemesh</artifactId> <version>2.4.1</version> </dependency>
第三步:修改web.xml,修改过滤器
<filter> <filter-name>sitemesh</filter-name> <filter-class> com.opensymphony.sitemesh.webapp.SiteMeshFilter </filter-class> </filter>
解决方式2(软解决):
第一步:在项目app/webapp/filter包下添加CharacterEncodingFilter.java(从spring中挑出来的)
/** * Copyright 2002-2010 the original author or authors. */ package com.ucap.app.webapp.filter; import java.io.IOException; import java.util.Properties; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.filter.OncePerRequestFilter; /** * * @author Wang Jun */ public class CharacterEncodingFilter extends OncePerRequestFilter { private String encoding; private boolean forceEncoding = false; /** * Set the encoding to use for requests. This encoding will be passed into a * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call. * <p>Whether this encoding will override existing request encodings * (and whether it will be applied as default response encoding as well) * depends on the {@link #setForceEncoding "forceEncoding"} flag. */ public void setEncoding(String encoding) { this.encoding = encoding; } /** * Set whether the configured {@link #setEncoding encoding} of this filter * is supposed to override existing request and response encodings. * <p>Default is "false", i.e. do not modify the encoding if * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} * returns a non-null value. Switch this to "true" to enforce the specified * encoding in any case, applying it as default response encoding as well. * <p>Note that the response encoding will only be set on Servlet 2.4+ * containers, since Servlet 2.3 did not provide a facility for setting * a default response encoding. */ public void setForceEncoding(boolean forceEncoding) { this.forceEncoding = forceEncoding; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { Properties prop = System.getProperties(); prop.put("file.encoding", "utf-8"); if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) { request.setCharacterEncoding(this.encoding); if (this.forceEncoding) { response.setCharacterEncoding(this.encoding); response.setContentType("text/html; charset=" + this.encoding); } } filterChain.doFilter(request, response); } }
第二步:修改web.xml
<filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter<--修改成上面CharacterEncodingFilter.java的位置 </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>