解决srping2.5.4+sitemesh2.2.1+weblogic页面乱码问题

环境: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%"=="" (
    @REM The then part of this block is telling us we are either starting an admin server OR we are non-clustered
    set CLUSTER_PROPERTIES=-Dweblogic.management.discover=true -Dserver.config=./server-config.properties -Dfile.encoding=utf-8
) else (
    set CLUSTER_PROPERTIES=-Dweblogic.management.discover=false -Dweblogic.management.server=%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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值