2016-07-30 30 views
1

我查看了很多關於此主題的帖子,但仍未找到解決方案。Java,Spring,JSP編碼

當我從我的JSP發送數據(url)到我的Spring控制器時,它顯示爲'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> 

控制器:

@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); 

由於

更新 我切換到Jetty服務器,它似乎工作正常。雖然我想使用Tomcat,但我試圖在連接器的Tomcat server.xml中更改編碼,但即使這樣也行不通。

最後更新 我在我的Tomcat的server.xml使用URIEncoding="UTF-8" useBodyEncodingForURI="true"。沒有useBodyEncodingForURI="true"它不會工作。 Post 5 here UTF-8 encoding in Spring MVC, problem with FORMs幫助了我。

+0

讓我們先嚐試把它放在yourjsp文件的頭部:

+0

@Georgesvanhoutte yeah its already – DANNY1000000

+0

它的maven我使用 – DANNY1000000

回答

0

CharacterEncodingFilter只適用於POST request.So你需要通過郵寄來發送數據,而不是get.If你保持搞定,下面的方法可以嘗試:

  1. 編輯的server.xml中的T​​omcat的conf文件夾中,將連接器元素中的一個屬性URIEcoding="utf-8"。通過new String(url.getBytes("iso-8859-1"),"utf-8")

好運

  • 轉換網址。

  • +0

    謝謝,我設法讓它去,但我不得不將這一行添加到server.xml 。URIEncoding =「UTF-8」useBodyEncodingForURI =「true」。如果沒有useBodyEncodingForURI屬性,它將不起作用。 – DANNY1000000