简体   繁体   English

Java,Spring,JSP编码

[英]Java, Spring, JSP encoding

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'. 当我从我的JSP向我的Spring控制器发送数据(url)时,它显示为'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. 变音符号是以这种方式编码的,但我试图确保我的应用程序使用UTF-8编码,这似乎不起作用。

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. firefox中的URL显示为http:// localhost:8081 / team / fc-bayern-münchen-5 ,当粘贴在其他地方时显示为http:// localhost:8081 / team / fc-bayern-m%C3%BCnchen-5我认为这是UTF-8的预期。

I've tried using URLDecoder.decode(url.getBytes(), "UTF-8") but that didnt work either. 我尝试过使用URLDecoder.decode(url.getBytes(), "UTF-8")但这也URLDecoder.decode(url.getBytes(), "UTF-8")

JSP: 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: 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. 更新我切换到Jetty服务器,它似乎工作正常。 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. 虽然我想使用Tomcat,但我曾尝试更改连接器的Tomcat server.xml中的编码,但即使这样也无效。

Final Update I had to use URIEncoding="UTF-8" useBodyEncodingForURI="true" in my Tomcat server.xml. 最终更新我必须在我的Tomcat server.xml中使用URIEncoding="UTF-8" useBodyEncodingForURI="true" Without the useBodyEncodingForURI="true" it wouldn't work. 没有useBodyEncodingForURI="true"它将无法正常工作。 Post 5 here UTF-8 encoding in Spring MVC, problem with FORMs helped me. 在Spring MVC中发布了5个UTF-8编码,FORMs的问题帮助了我。

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: CharacterEncodingFilter仅适用于POST请求。所以你需要通过post而不是get发送你的数据。如果你保持get,你可以尝试以下方法:

  1. Edit server.xml in tomcat conf folder,put an attribute URIEcoding="utf-8" in Connector element. 在tomcat conf文件夹中编辑server.xml,在Connector元素中放置一个属性URIEcoding="utf-8"
  2. convert url by new String(url.getBytes("iso-8859-1"),"utf-8") 通过new String(url.getBytes("iso-8859-1"),"utf-8")转换url new String(url.getBytes("iso-8859-1"),"utf-8")

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM