SSH+sitemesh

昨天想把自己学习的项目加个sitemesh,没用过,网上找了些资料,照着做了一个,碰到了些问题,写下来做备忘
第一个问题
缺少部分jar包,除了struts2-sitemesh2.1.6、sitemesh2.3,还需要其它的jar,去官方网站下了个完整的sitemesh2.3,将里面的包全部导入,结果就出现了个异常,找来找去,发现是crimson.jar惹的祸,删除jar就OK
另外一个问题是提示struts2-sitemesh的过滤器找不到,明明就在那里的jar,非说找不到,很费解,不管那么多,肯定是和哪个包冲突了,将spring多余的包删除后ok,也不知道是哪个包的问题

终于运行成功啦,结果发现html是乱码,还有项目中的一个jsTree出不来,当场崩溃
html的乱码网上一堆说法,试了半天,只有重写过滤器才是最有效的终极解决办法
代码如下:
package com.cool.filter;


import java.io.IOException;

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

import org.springframework.util.ClassUtils;
import org.springframework.web.filter.OncePerRequestFilter;

// Referenced classes of package org.springframework.web.filter:
//            OncePerRequestFilter

public class MyCharacterEncodingFilter extends OncePerRequestFilter
{

    private static final boolean responseSetCharacterEncodingAvailable;
    private String requestEncoding;
    private String responseEncoding;
   
    private boolean forceEncoding;

    public void setResponseEncoding(String responseEncoding) {
this.responseEncoding = responseEncoding;
}

public MyCharacterEncodingFilter()
    {
        forceEncoding = false;
    }



    public void setRequestEncoding(String requestEncoding) {
this.requestEncoding = requestEncoding;
}

public void setForceEncoding(boolean forceEncoding)
    {
        this.forceEncoding = forceEncoding;
    }

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException
    {
        if(requestEncoding != null && (forceEncoding || request.getCharacterEncoding() == null))
        {
            request.setCharacterEncoding(requestEncoding);
            if(responseEncoding!=null&&!responseEncoding.equals(""))
            {
                response.setCharacterEncoding(responseEncoding);
              
            }
        }
        filterChain.doFilter(request, response);
    }



    static
    {
        responseSetCharacterEncodingAvailable = ClassUtils.hasMethod(javax.servlet.http.HttpServletResponse.class, "setCharacterEncoding", new Class[] {
            java.lang.String.class
        });
    }
}

web配置文件如下:
<!-- 乱码配置 -->
<filter>
        <filter-name>encodingFilter </filter-name>
        <filter-class>com.cool.filter.MyCharacterEncodingFilter </filter-class>
        <init-param>
            <param-name>requestEncoding </param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>responseEncoding </param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter </filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

你可能感兴趣的:(spring,html,Web,servlet,ssh)