0

I'm using a Spring Boot MVC startup, with using 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. 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??

I can add a lot of sources, if you want or need ....

The controller which send input to the 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:

    <%@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):

    @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;
    }
}
rolandl
  • 1
  • 4
  • 1
    How do those characters appear and where? In the controller? – Kayaman Mar 28 '18 at 18:44
  • When it comes in, it is like Bayern München and when it comes into my controller it comes back from the jsp to the controller, it is Bayern München ... – rolandl Mar 28 '18 at 21:27
  • So that means it's entered as `UTF-8`, but at some point it's being interpreted in a single byte encoding, most likely `ISO-8859-1` or its variants. Can edit the question and include snippets from the JSP and the controller where the data is being entered/received? – Kayaman Mar 29 '18 at 06:08
  • Added the asked source, I guess ... – rolandl Mar 30 '18 at 12:06
  • Is there someone, who can have an answer??? – rolandl Apr 04 '18 at 12:25

1 Answers1

0

You're using UTF-8 everywhere, then in your JSP page's <head> you declare that the contents are ISO-8859-1 with

<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" />
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • I saw that when I posted the sources and deleted it ... but the problem is still there. But I guess/think all info in meta tags are informational, so this does not matter. – rolandl Apr 05 '18 at 08:34
  • @rolandl it's still a bit unclear where you see it as `Bayern München`. Do you mean inside the controller as in the code? If so, how do you view that value (you can't "see" inside the controller with your bare eyes after all)? Or do you mean you see the garbled value in a JSP? – Kayaman Apr 05 '18 at 08:40
  • Well, it goes wrong with all the clubs, which do have, let's say, a special character in their name, like é, ö, ü, etc. I see it when I debug, and set a breakpoint, when the code arrives in the controller, which must search for the results between these two clubs .... In general it goes wrong in every menu-option where I can/have to choose a club with a special character in it ... – rolandl Apr 05 '18 at 14:05
  • Well, the menu-option is about searching for results between 2 clubs in a European Cup (does not matter Cup). First I choose countries, then I choose 2 clubs inside these countries (of both countries all clubs which have ever played in a European Cup are shown and you have choose 2 in 2 different option lists within one form (method=post) ). So in this (jsp) Bayern München is shown as it must be and also known in the database ... But when I choose Bayern München .... it comes in the controller as Bayern München. – rolandl Apr 05 '18 at 14:16
  • I searched with very different option on the internet, and can't find a possible answer or tried some so called options, it did not work. I added URIEncoding="UTF-8" to HTTP and AJP in server.xml. and other possibilities mentioned with the searches – rolandl Apr 05 '18 at 14:20
  • I use a Spring Boot with DispatcherServlet, I cannot find how to change the character encoding of the response, if it is possible ... I use Spring 4, I guess, because spring jar-files are with 4.0.6 ... Could that the problem? – rolandl Apr 05 '18 at 14:24
  • So the text displays correctly on the JSP, but when you post the form to the server it's garbled? Then it's the **request** that's getting garbled, not the *response*. Try setting up the `CharacterEncodingFilter` from here and see if it helps: https://stackoverflow.com/questions/5928046/spring-mvc-utf-8-encoding – Kayaman Apr 06 '18 at 06:30
  • Thank you very much, it works. I thought I tried this earlier, but I guess there was no tag with forceEncoding ... but anyway, thank you again!!!! – rolandl Apr 06 '18 at 11:45
  • Now you need to accept my answer so I get points, precious points! – Kayaman Apr 06 '18 at 19:44