我试图使用UTF-8编码我正在开发Spring应用程序,但我已经从地砖插入属性时,得到正确的编码问题。

我有这样的片段在我的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" />
...

我有检查在蚀,此文件具有UTF-8编码。 在title属性传递的字不正确显示(重音字符显示在一个错误的方式)在页面虽然JSP的其余部分是正确的(例如其插在头中的JSP片段)。如果我更改编码为ISO-8859-1的标题是确定的,但其余ODF的页面是错误的。看来我不能去改变编码为UTF-8在我的瓷砖文件。我也以我创建的文件寻找“ISO-8859-1”,我还没有建立这个配置中的任何文件。

谁能告诉我怎么可以设置正确的编码砖?

由于

有帮助吗?

解决方案 2

这是一个问题的字符集,而不是与编码。我不得不集合

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

在每JSP和它的工作。我不知道是否有在Spring Web应用程序的所有JSP配置这更简单的方法。

其他提示

以下添加到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>

另一种方法可能是 ReloadableResourceBundleMessageSource 的(具有属性defaultEncoding = “UTF-8”)也为内容被从瓦片插入用法。

我的意思是可以传递从瓷砖的关键字,并且用它来输出需要的内容从资源束,如下所示:

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

在我的Struts 2.3〜2.5的迁移我遇到了类似的问题: 所有的JavaScript内容类型(响应报头).JS由JSP引用的文件均现在 “应用/ JavaScript的;字符集= ISO-8859-1”(支柱2.5)代替字符集= UTF-8(在支柱2.3)。 charset属性设置为UTF-8为JSP和脚本标记引用JS文件。

我从莱昂内尔添加的代码和它终于研究出: 但编码现在 “;字符集= UTF-8的text / html” 是。所以,我已经失去了应用程序/ JavaScript的。它did'nt正常工作。

<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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top