【问题标题】:Tiles encoding problem瓷砖编码问题
【发布时间】:2010-03-03 11:57:14
【问题描述】:

我正在尝试对我正在开发的 Spring 应用程序使用 UTF-8 编码,但在从图块中插入属性时,我无法获得正确的编码。

我的 JSP 模板中有这个片段:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title><tiles:getAsString name="title" /></title>   
</head>
<body>
    <tiles:insertAttribute name="header" ignore="true" />
....

在我的瓷砖 XML 配置文件中,我有类似的内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
   <definition name="tiles:base" template="/WEB-INF/views/templates/main.jsp">
     <put-attribute name="title" value="Título" />
...

我在 Eclipse 中检查了这些文件是否具有 UTF-8 编码。 尽管 JSP 的其余部分是正确的(例如插入到标题中的 JSP 片段),但在页面中未正确显示在 title 属性中传递的单词(重音字符以错误的方式显示)。如果我将编码更改为 ISO-8859-1,则标题正常,但页面的其余 odf 错误。看来我无法在我的图块文件中将编码更改为 UTF-8。我还在我创建的文件中查找了“ISO-8859-1”,但我没有在任何文件中设置此配置。

谁能告诉我如何为图块设置正确的编码?

谢谢

【问题讨论】:

    标签: encoding utf-8 tiles iso-8859-1


    【解决方案1】:

    将以下内容添加到web.xml。这与在每个 JSP 文件中添加标头的效果相同。

    web.xml:

    <web-app>
        ...
        <jsp-config>
            <jsp-property-group>
                <url-pattern>*.jsp</url-pattern>
                <page-encoding>UTF-8</page-encoding>
                <trim-directive-whitespaces>true</trim-directive-whitespaces>
            </jsp-property-group>
        </jsp-config>    
    </web-app>
    

    【讨论】:

    • 这个答案应该在顶部,因为它就像一个魅力,比在每个 jsp 中指定字符集要好得多。
    【解决方案2】:

    这是字符集的问题,而不是编码的问题。我必须设置

    <%@ page contentType="text/html; charset=utf-8"%> 
    

    在每个 JSP 中都有效。我不知道在 Spring Web 应用程序的所有 JSP 中是否有更简单的方法来配置它。

    【讨论】:

      【解决方案3】:

      另一种方法可能是使用 ReloadableResourceBundleMessageSource(具有属性 defaultEncoding="UTF-8")也用于从图块插入的内容。

      我的意思是你可以从图块中传递一个关键字,并使用它从资源包中输出所需的内容,如下所示:

      <tiles:useAttribute id="title_key" name="title"/>
      <spring:message code="${title_key}"/>
      

      【讨论】:

      • &lt;property name="defaultEncoding" value="UTF-8" /&gt; 为我工作!
      【解决方案4】:

      在我的 Struts 2.3 到 2.5 迁移期间,我遇到了类似的问题: JSP 引用的所有 javascript .JS 文件的内容类型(在响应头中)现在是“application/javascript;charset=ISO-8859-1”(struts 2.5)而不是 charset=UTF-8(在 struts 2.3 中)。 对于引用 js 文件的 JSP 和脚本标记,Charset 属性设置为 utf-8。

      我添加了 Leonel 的代码,它终于奏效了: 但编码现在是“text/html;charset=UTF-8”。所以我丢失了应用程序/javascript。它没有正常工作。

      <web-app>
      ...
      <jsp-config>
          <jsp-property-group>
              <url-pattern>*.js</url-pattern>
              <page-encoding>UTF-8</page-encoding>
              <trim-directive-whitespaces>true</trim-directive-whitespaces>
          </jsp-property-group>
      </jsp-config>    
      

      所以我尝试了其他方法: https://www.baeldung.com/tomcat-utf-8 有了这个,我得到了正确的字符集和内容类型。

      让我们定义一个名为 CharacterSetFilter 的类:

      public class CharacterSetFilter implements Filter {
      
      // ...
      
      public void doFilter(
        ServletRequest request, 
        ServletResponse response, 
        FilterChain next) throws IOException, ServletException {
          request.setCharacterEncoding("UTF-8");
          response.setContentType("text/html; charset=UTF-8");
          response.setCharacterEncoding("UTF-8");
          next.doFilter(request, response);
      }
      
      // ...
      }
      

      我们需要将过滤器添加到应用程序的 web.xml 中,以便将其应用于所有请求和响应:

      <filter>
       <filter-name>CharacterSetFilter</filter-name>
       <filter-class>com.baeldung.CharacterSetFilter</filter-class>
      </filter>
      
      <filter-mapping>
       <filter-name>CharacterSetFilter</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多