0

我正在尝试对正在开发的 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”,但我没有在任何文件中设置此配置。

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

谢谢

4

4 回答 4

9

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

网页.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>
于 2012-07-04T19:21:01.290 回答
3

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

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

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

于 2010-03-05T10:24:34.897 回答
1

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

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

<tiles:useAttribute id="title_key" name="title"/>
<spring:message code="${title_key}"/>
于 2011-07-22T10:53:05.720 回答
1

在我的 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>
于 2018-08-29T12:02:32.790 回答