I've looked through alot of post on this topic but still not found a solution.
When I send data (url) from my JSP to my Spring controller it appears as 'fc-bayern-münchen-5'. The umlaut is encoded in this way but I've tried to ensure my app is using UTF-8 encoding, which appears not to be working.
The URL in firefox shows as http://localhost:8081/team/fc-bayern-münchen-5 and when pasted elsewhere shows as http://localhost:8081/team/fc-bayern-m%C3%BCnchen-5 which I think is expected for UTF-8.
I've tried using URLDecoder.decode(url.getBytes(), "UTF-8")
but that didnt work either.
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";
}
SecurityConfig:
@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);
Thanks
Update I switched to a Jetty server and it seems to be working fine with that. Although I wanted to use Tomcat, I had tried to change the encoding in the Tomcat server.xml for the connector but even that didn't work.
Final Update
I had to use URIEncoding="UTF-8" useBodyEncodingForURI="true"
in my Tomcat server.xml. Without the useBodyEncodingForURI="true"
it wouldn't work.
Post 5 here UTF-8 encoding in Spring MVC, problem with FORMs helped me.