1

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.

5
  • lets 1st try to put this within the head section of yourjsp file : <meta http-equiv="Content-Type" content="text/html; charset="utf- 8"> Commented Jul 30, 2016 at 13:50
  • @Georgesvanhoutte yeah its already there. Commented Jul 30, 2016 at 13:56
  • Its maven I'm using Commented Jul 30, 2016 at 13:59
  • and your class is marked with the @Configuration ? Commented Jul 30, 2016 at 14:10
  • Yes I have the @Configuration annotation included Commented Jul 31, 2016 at 0:39

1 Answer 1

0

CharacterEncodingFilter only works on POST request.So you need to send your data by post instead of get.If you keep in get,the following method you can try:

  1. Edit server.xml in tomcat conf folder,put an attribute URIEcoding="utf-8" in Connector element.
  2. convert url by new String(url.getBytes("iso-8859-1"),"utf-8")

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I managed to get it going but I had to add this line into the server.xml. URIEncoding="UTF-8" useBodyEncodingForURI="true". Without useBodyEncodingForURI attribute it wouldn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.