我正在尝试对我正在开发的Spring应用程序使用UTF-8编码,但是在从tiles插入属性时,我在获得正确的编码时遇到了问题。
我的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" />
....在我的tiles 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编码。传入title属性的单词在页面中显示不正确(重音字符以错误的方式显示),而JSP的其余部分是正确的(例如,插入在标题中的JSP片段)。如果我将编码改为ISO-8859-1,标题是可以的,但页面的其余部分是错误的。似乎我无法在我的tiles文件中将编码更改为UTF-8。我还在我创建的文件中查找了"ISO-8859-1“,但我没有在任何文件中设置此配置。
谁能告诉我怎样才能为tiles设置正确的编码?
谢谢
发布于 2010-03-05 18:24:35
这是字符集的问题,不是编码的问题。我必须设置
<%@ page contentType="text/html; charset=utf-8"%> 在每个JSP中,它都起作用了。我不知道是否有更简单的方法可以在Spring Web应用程序的所有JSP中配置它。
发布于 2012-07-05 03:21:01
将以下内容添加到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>发布于 2018-08-29 20:02:33
在我将Struts2.3迁移到2.5时,我遇到了一个类似的问题: JSP引用的所有javascript应用程序文件的内容类型(在响应头中)现在是“.JS /javascript;charset=ISO-8859-1”(Struts2.5),而不是charset=UTF-8 (在Struts2.3中)。对于引用js文件的JSP和脚本标记,字符集属性设置为utf-8。
我添加了莱昂内尔的代码,它终于可以工作了:但是编码现在是"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>https://stackoverflow.com/questions/2370921
复制相似问题