我浏览了很多关于这个主题的帖子,但仍然没有找到解决方案。
当我将数据(url)从 JSP 发送到 Spring Controller 时,它显示为“fc-bayern-münchen-5”。元音变音是以这种方式编码的,但我试图确保我的应用程序使用 UTF-8 编码,但这似乎不起作用。
Firefox 中的 URL 显示为 http://localhost:8081/team/fc-bayern-münchen-5 当粘贴到其他地方时显示为 http://localhost:8081/team/fc-bayern-m%C3%BCnchen-5我认为这是 UTF-8 所期望的。
我尝试过使用 URLDecoder.decode(url.getBytes(), "UTF-8")
但也不起作用。
JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<html>
<head>
<jsp:include page="../partialviews/globaheader.jsp" >
<jsp:param name="title" value="Teams"/>
</jsp:include>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<jsp:include page="../partialviews/menu.jsp"/>
<h3>Teams</h3>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Short Name</th>
</tr>
</thead>
<tbody>
<c:forEach items="${teams}" var="team">
<tr>
<td><img class="smallBadge" src="/static/images/${team.getEncodedCrestUrl()}"/></td>
<c:set var="url" value="${team.getTeamNameUrlFriendly()}-${team.id}" scope="request"/>
<td><a href="${pageContext.request.contextPath}/team/${url}">${team.name}</a></td>
<td>${team.shortName}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
Controller :
@Transactional
@RequestMapping(value = "/team/{id}", method = RequestMethod.GET)
public String getBttsTips(@PathVariable final String id, final Model model) throws UnsupportedEncodingException {
final int teamId = Integer.valueOf(id.substring(id.lastIndexOf("-")+1, id.length()));
final String teamName = id.substring(0, id.lastIndexOf("-"));
final Team team = teamService.findById(teamId);
if (team == null || !team.getTeamNameUrlFriendly().equals(teamName)){
return "error/404";
}
final List<Fixture> allFixtures = fixtureService.findAllFixturesByTeam(team);
final List<Fixture> homeFixtures = fixtureService.findHomeFixturesByTeam(team);
final List<Fixture> awayFixtures = fixtureService.findAwayFixturesByTeam(team);
model.addAttribute("allFixtures", allFixtures);
model.addAttribute("homeFixtures", homeFixtures);
model.addAttribute("awayFixtures", awayFixtures);
model.addAttribute("team", team);
return "team/teamDetail";
}
安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
final CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
http.addFilterBefore(filter,CsrfFilter.class);
谢谢
更新 我切换到了 Jetty 服务器,它似乎工作得很好。虽然我想使用 Tomcat,但我尝试更改 Tomcat server.xml 中连接器的编码,但即使这样也不起作用。
最终更新
我必须在 Tomcat server.xml 中使用 URIEncoding="UTF-8"useBodyEncodingForURI="true"
。如果没有 useBodyEncodingForURI="true"
它将无法工作。
在这里发布 5 UTF-8 encoding in Spring MVC, problem with FORMs帮助了我。
最佳答案
CharacterEncodingFilter
仅适用于POST请求。因此您需要通过post而不是get方式发送数据。如果您保留get方式,可以尝试以下方法:
- 编辑tomcat conf文件夹中的server.xml,在Connector元素中添加属性
URIEcoding="utf-8"
。 - 通过
new String(url.getBytes("iso-8859-1"),"utf-8") 转换 url
祝你好运。
关于Java、Spring、JSP编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674425/