简体   繁体   English

CharacterEncoding DispatcherServlet Spring Boot MVC响应jsp

[英]CharacterEncoding DispatcherServlet Spring Boot MVC Response jsp

I'm using a Spring Boot MVC startup, with using DispatcherServlet. 我正在使用Spring Boot MVC启动,并使用DispatcherServlet。 I understand, that there are many implementations of it. 我了解,它有很多实现。 The problem is, when a form with a POST (with a choice of Footbal CLubs with different characters like ü, ë, è are chosen), these are not well responded to the Controller. 问题是,当带有POST的表单(选择了带有ü,ë,è等不同字符的Footbal CLub)时,这些对控制器的响应不佳。 I use in the jsp's UTF-8, I guess everywhere (RootContext, tomcat (maybe not), jsp's the character encoding is set to UTF-8. So, the problem is, how do I change the character-encoding for my response?? 我在jsp的UTF-8中使用,我猜到处都有(RootContext,tomcat(也许不是)),jsp的字符编码设置为UTF-8。所以问题是,如何更改响应的字符编码? ?

I can add a lot of sources, if you want or need .... 如果您想要或需要,我可以添加很多资源。

The controller which send input to the jsp: 将输入发送到jsp的控制器:

@RequestMapping(value = "zoekenclubsduels", method = RequestMethod.POST)
public ModelAndView tonenVanClub(@RequestParam("land1") String land1, @RequestParam("land2") String land2) {
    LocalDate ld = LocalDate.now();
    String contextPath = context.getContextPath();
    List<Club> clubs1 = this.clubManager.getAllByClland(land1);
    if (clubs1 == null) {
        return new ModelAndView("redirect:/clubs/zoekenlandenduels");
    }
    Collections.sort(clubs1);
    List<Club> clubs2 = this.clubManager.getAllByClland(land2);
    if (clubs2 == null) {
        return new ModelAndView("redirect:/clubs/zoekenlandenduels");
    }
    Collections.sort(clubs2);
    ModelAndView modelAndView = new ModelAndView("clubs/zoekenclubsduels");
    modelAndView.addObject("clubs1", clubs1);
    modelAndView.addObject("clubs2", clubs2);
    modelAndView.addObject("contextPath", contextPath);
    modelAndView.addObject("ld", ld);
    return modelAndView;
}

The jsp: JSP:

    <%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<c:set var="contextPath"
    value="${pageContext.servletContext.contextPath}" />
<!doctype html>
<html lang="nl">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Select Clubs</title>
        <link rel="stylesheet" href="<c:url value="/styles/reset.css"/>" />
        <link rel="stylesheet" href="<c:url value="/styles/default.css"/>" />
        <link rel="stylesheet" href="<c:url value="/styles/responsive.css" />" />
        <link rel="stylesheet" href="<c:url value="/styles/menu.css" />" />

        <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">window.jQuery|| document
                        .write('<script type="text/javascript" src="/EuropeanCupFootball\/scripts\/vendor\/1.7.2.jquery.min.js"><\/script>')
        </script>
        <script src="${contextPath}/scripts/vendor/jquery.slides.min.js"></script>
        <script src="${contextPath}/scripts/scriptslidejs.js"></script>
        <script src="${contextPath}/scripts/script_menu.js"></script>
        <!-- script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script-->
    </head>
    <body>
        <!-- body style="background-image: url(${contextPath}/images/page_bg.jpg)"-->
        <header>
            <c:import url="/WEB-INF/jsp/header.jsp" />
        </header>
        <br>
        <h2>Select Clubs for mutual Results</h2>
        <br> <br>
        <div class="pageczld">
            <c:if test="${not empty clubs1}">
                <form action="<c:url value='/clubs/toonuitslagenduels'/>"
                    method="post">
                    <label>Club 1 : <select required name="club1" size="15">
                            <c:forEach items="${clubs1}" var="club1">
                                <option value="${club1.clNaam}"
                                    ${club1.clNaam == param.club1.clNaam ? 'selected="selected"' : ''}>
                                    ${club1.clNaam}</option>
                            </c:forEach>
                    </select>
                    </label> <label>Club 2 : <select required name="club2" size="15">
                            <c:forEach items="${clubs2}" var="club2">
                                <option value="${club2.clNaam}"
                                    ${club2.clNaam == param.club2.clNaam ? 'selected="selected"' : ''}>
                                    ${club2.clNaam}</option>
                            </c:forEach>
                    </select>
                    </label> <input type="submit" value="OK" /> ${fout}
                </form>
            </c:if>
        </div>
        <footer>
            <c:import url="/WEB-INF/jsp/footer.jsp" />
        </footer>
        <div style="clear: both;"></div>
    </body>
</html> 

The controller which receives output from jsp (java): 从jsp(java)接收输出的控制器:

    @RequestMapping(value = "toonuitslagenduels", method = RequestMethod.POST)
public ModelAndView toonUitslagenDuels(@RequestParam("club1") String club1string,
        @RequestParam("club2") String club2string) {
    // List<UitslagClub> uitslagclubs =
    // this.clubManager.getByClNaamUitslagclubs(clubstring);
    LocalDate ld = LocalDate.now();
    String contextPath = context.getContextPath();
    Club clubByName1 = this.clubManager.getByClNaam(club1string);
    Club club1 = this.clubManager.getOne(clubByName1.getClNr());
    if (club1 == null) {
        return new ModelAndView("redirect:/clubs/zoekenlandenduels");
    }
    Club clubByName2 = this.clubManager.getByClNaam(club2string);
    Club club2 = this.clubManager.getOne(clubByName2.getClNr());
    if (club2 == null) {
        return new ModelAndView("redirect:/clubs/zoekenlandenduels");
    }
    List<Uitslag> uitslagen = new ArrayList<Uitslag>();
    List<UitslagClub> uitslagclubs = new ArrayList<UitslagClub>();
    List<UitslagClub> uitslagclub1 = club1.getUitslagclubs();
    List<UitslagClub> uitslagclub2 = club2.getUitslagclubs();
    Iterator<UitslagClub> iter1 = uitslagclub1.iterator();
    while (iter1.hasNext()) {
        UitslagClub uc1 = (UitslagClub) iter1.next();
        Iterator<UitslagClub> iter2 = uitslagclub2.iterator();
        while (iter2.hasNext()) {
            UitslagClub uc2 = (UitslagClub) iter2.next();
            if (uc2.getUcUiNr() == uc1.getUcUiNr()) {
                Uitslag uitslag = this.uitslagManager.getUitslag(uc2.getUcUiNr());
                uitslagen.add(uitslag);
                uitslagclubs.add(uc1);
            }
        }
    }
    int awed = 0;
    int awinst = 0;
    int agelijk = 0;
    int averlies = 0;
    int dpvoor = 0;
    int dptegen = 0;
    Iterator<UitslagClub> ucIter = uitslagclubs.iterator();
    while (ucIter.hasNext()) {
        UitslagClub uc = ucIter.next();
        awed++;
        if (uc.getUcThuisOfUit().equals("t")) {
            if (uc.getUitslag().getUiThDoelpunten() > uc.getUitslag().getUiUitDoelpunten()) {
                awinst++;
                dpvoor = dpvoor + uc.getUitslag().getUiThDoelpunten();
                dptegen = dptegen + uc.getUitslag().getUiUitDoelpunten();
            }
            else if (uc.getUitslag().getUiThDoelpunten() == uc.getUitslag().getUiUitDoelpunten()) {
                agelijk++;
                dpvoor = dpvoor + uc.getUitslag().getUiThDoelpunten();
                dptegen = dptegen + uc.getUitslag().getUiUitDoelpunten();
            }
            else {
                averlies++;
                dpvoor = dpvoor + uc.getUitslag().getUiThDoelpunten();
                dptegen = dptegen + uc.getUitslag().getUiUitDoelpunten();
            }
        }
        else {
            if (uc.getUitslag().getUiThDoelpunten() < uc.getUitslag().getUiUitDoelpunten()) {
                awinst++;
                dpvoor = dpvoor + uc.getUitslag().getUiUitDoelpunten();
                dptegen = dptegen + uc.getUitslag().getUiThDoelpunten();
            }
            else if (uc.getUitslag().getUiThDoelpunten() == uc.getUitslag().getUiUitDoelpunten()) {
                agelijk++;
                dpvoor = dpvoor + uc.getUitslag().getUiThDoelpunten();
                dptegen = dptegen + uc.getUitslag().getUiUitDoelpunten();
            }
            else {
                averlies++;
                dpvoor = dpvoor + uc.getUitslag().getUiUitDoelpunten();
                dptegen = dptegen + uc.getUitslag().getUiThDoelpunten();
            }
        }
    }
    if (uitslagen.isEmpty()) {
        ModelAndView modelAndView = new ModelAndView("clubs/geenuitslagenduels", "contextPath", contextPath);
        modelAndView.addObject("ld", ld);
        modelAndView.addObject("club1", club1);
        modelAndView.addObject("club2", club2);
        return modelAndView;
    } else {
        int aantalUitslagen = uitslagen.size();
        ModelAndView modelAndView = new ModelAndView("clubs/toonuitslagenduels", "uitslagen", uitslagen);
        modelAndView.addObject("contextPath", contextPath);
        modelAndView.addObject("ld", ld);
        modelAndView.addObject("aantalUitslagen", aantalUitslagen);
        modelAndView.addObject("club1", club1);
        modelAndView.addObject("club2", club2);
        modelAndView.addObject("awed", awed);
        modelAndView.addObject("awinst", awinst);
        modelAndView.addObject("agelijk", agelijk);
        modelAndView.addObject("averlies", averlies);
        modelAndView.addObject("dpvoor", dpvoor);
        modelAndView.addObject("dptegen", dptegen);
        return modelAndView;
    }
}

You're using UTF-8 everywhere, then in your JSP page's <head> you declare that the contents are ISO-8859-1 with 您在各处使用UTF-8 ,然后在JSP页面的<head>声明内容为ISO-8859-1其中

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

Also you'd want to prefer the shorter version so 另外,您还希望使用较短的版本,以便

<meta charset="utf-8" />

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

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