【问题标题】:sitemesh and UTF-8 encoding站点网格和 UTF-8 编码
【发布时间】:2011-05-27 00:48:17
【问题描述】:

我有一个使用 sitemesh 的 spring-mvc 应用程序。我遇到的问题是我的页面需要是 UTF-8,但 sitemesh 支持 ISO-8859-1 字符集。是否可以将 Sitemesh 配置为使用 UTF-8 页面? 我正在使用一个简单的示例,我试图显示页面的正确性,但我得到了无效字符,例如

我使用的文件是:

sitemesh.xml

<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml" />
    <excludes file="${decorators-file}" />

    <page-parsers>
        <parser content-type="text/html"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
        <parser content-type="text/html;charset=ISO-8859-1"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    </page-parsers>

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>
    </decorator-mappers>
</sitemesh>

web.xml

...
<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
...

装饰器.xml

<decorators defaultdir="/decorators">
    <decorator name="main" page="main.jsp">
          <pattern>/*</pattern>
    </decorator>
</decorators>

main.jsp

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
<body>
 some stuff here
...
<div class="main"><decorator:body /></div>
</body>
</html>

我的示例页面:

<html>
...
<body>
     mùpeeàçè
</body>
</html>

有人知道吗? 谢谢

【问题讨论】:

    标签: encoding sitemesh


    【解决方案1】:

    回复有点晚了,但我希望其他人可以从我浪费的时间中受益。 Spring的过滤器对我也不起作用。我自己编写并手动设置了 servletResponse 的 contentType。我现在没有问题。

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        chain.doFilter(req, resp);
    }
    
    
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.muratdozen.mvc.filters.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/ui/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/WEB-INF/views/*</url-pattern>
        <dispatcher>ERROR</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    

    【讨论】:

      【解决方案2】:

      你可以在 web.xml 中尝试(强制编码很重要,它对我有用)

          <filter>
          <filter-name>SetCharacterEncodingFilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <init-param>
              <param-name>encoding</param-name>
              <param-value>UTF8</param-value>
          </init-param>
          <init-param>
              <param-name>forceEncoding</param-name>
              <param-value>true</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>SetCharacterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      

      【讨论】:

        【解决方案3】:

        您是否尝试将 CharacterEncodingFilter 添加到您的 web.xml?见:http://ibnaziz.wordpress.com/2008/06/10/spring-utf-8-conversion-using-characterencodingfilter/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-16
          • 2021-06-06
          • 2012-06-17
          • 2015-01-20
          • 1970-01-01
          相关资源
          最近更新 更多