0

I've followed so many indications (One of them is this checklist: How to get UTF-8 working in Java webapps?) that I just surrended to the situation..

I have a form inside a JSP (that is then merged with a generic layout .tag) When the post is executed, all accented letters are displayed in a wrong way, here an example:

è ò à ù ì é -> è ò à ù ì é

I've checked the encoding in the browser's console, and is UTF-8.. Same inside a TestFilter I've added.. same inside the controller. All the Symbols, are displayed correctly, the problem is just related to accented letters.. it's like the POST language is wrong, but I honestly have not found anything helpful online in 3 days.. THANKS A LOT

  • I've configured URIEncoding="UTF-8" in the Connectors inside server.xml (I'm using Tomcat9)

  • I've set the: <jsp:directive.page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"/> inside the jsp

  • I've added the <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> in the head of the general .tag layout and the <%@tag description="Overall Page template" pageEncoding="UTF-8"%> declarative

  • I've configured this unique filter in web.xml

  • I've passed to the JSV -Dfile.encoding=UTF-8 this parameter.

        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>```
    

This is the JSP, until the end of the interested FORM

    <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:tags="urn:jsptagdir:/WEB-INF/tags"
          xmlns:spring="http://www.springframework.org/tags" xmlns:c="http://java.sun.com/jsp/jstl/core"
          xmlns:fmt="http://java.sun.com/jsp/jstl/fmt">

    <tags:genericpage>
        <jsp:attribute name="header">
            <title><spring:message code="expenses.counter.page.title"/></title>
            <link ref="stylesheet" type="text/css" href="/resources/css/footer.css" />
        </jsp:attribute>
        <jsp:body>
            <jsp:directive.page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"/>
            <div class="container-fluid">
                <div class="row">
                    <div class="col">
                        <h1 class="text-center">EXPENSE COUNTER</h1>
                        <hr/>
                        <form id="expForm" action="/expCounter" method="post" class="needs-validation" accepted-charset="UTF-8" novalidate="">
                            <div class="form-row mx-5 my-4 text-center">
                                <!-- DESCRIPTION -->
                                <div class="form-group col-4 text-uppercase">
                                    <label for="description">
                                        <p><spring:message code="expenses.counter.description"/></p>
                                    </label>
                                    <input type="text" class="form-control ${validationResult.hasFieldErrors('description') ? 'is-invalid' : ''}" id="description" name="description"  />
                                    <div class="invalid-tooltip">
                                        <small><spring:message code="expenses.counter.error.description"/></small>
                                    </div>
                                </div>
                                <!-- AMOUNT -->
                                <div class="form-group col-2 text-uppercase">
                                    <label for="amount">
                                        <p><spring:message code="expenses.counter.amount"/></p>
                                    </label>
                                    <input type="number" class="form-control ${validationResult.hasFieldErrors('amount') ? 'is-invalid' : ''}" id="amount" name="amount" required=""/>
                                    <div class="invalid-tooltip">
                                        <small><spring:message code="expenses.counter.error.amount"/></small>
                                    </div>
                                </div>
                                <!-- DATE -->
                                <div class="form-group col-2 text-uppercase">
                                    <label for="spentOnDate">
                                        <p><spring:message code="expenses.counter.txdate"/></p>
                                    </label>
                                    <spring:message code="expenses.placeholder.ondate" var="ondate.placeholder" />
                                    <input type="date" class="form-control ${validationResult.hasFieldErrors('spentOnDate') ? 'is-invalid' : ''}" id="spentOnDate" name="spentOnDate" placeholder="${ondate.placeholder}" required=""/>
                                    <div class="invalid-tooltip">
                                        <small><spring:message code="expenses.counter.error.spentOnDate"/></small>
                                    </div>
                                </div>
                                <!-- BUTTON -->
                                <div class="form-group col-2 align-self-end">
                                    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                                    <button type="submit" class="btn btn-secondary">
                                        <spring:message code="general.button.submit"/>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

This is the generic .tag layout, until the body start:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@attribute name="header" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<html>
    <head>
<%--        <meta charset="utf-8"/>--%>
<%--        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>--%>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <link rel="stylesheet" href="/webjars/bootstrap/4.4.1/css/bootstrap.min.css" >
        <script defer src="https://use.fontawesome.com/releases/v5.0.13/js/all.js" integrity="sha384-xymdQtn1n3lH2wcu0qhcdaOpQwyoarkgLVxC/wZ5q7h9gHtxICrpcaSUfygqZGOe" crossorigin="anonymous"></script>
        <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
        <link rel="stylesheet" href="/resources/css/footer.css">
        <link rel="stylesheet" href="/resources/css/main.css">
        <jsp:invoke fragment="header"/>
    </head>
<body>

This is the method in the controller:

    @RequestMapping(value = "/expCounter", method = RequestMethod.POST)
    public String addExpense(@Valid @ModelAttribute("expForm") Expense expense,
                             BindingResult result,
                             ModelMap model, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
        if (result.hasErrors()) {
            redirectAttributes.addFlashAttribute("validationResult", result);
            redirectAttributes.addFlashAttribute("expForm", expense);
            return "redirect:expCounter";
        }

        expenseService.save(expense);
        return "redirect:expCounter";
    }
lordcroci
  • 83
  • 8
  • One item I don't see mentioned, which has helped me in the past: When you start Tomcat (i.e. when you start the JVM in which Tomcat runs), do you specify the `-Dfile.encoding=UTF-8` option? How you do this depends on how you start Tomcat. I mostly use a `setenv` file - see [here](https://tomcat.apache.org/tomcat-9.0-doc/RUNNING.txt) for notes. And see additional discussions [here](https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding). – andrewJames Jun 11 '20 at 15:45
  • Thank you about that. I forgot to mention it, but unfortunately yes. I pass the option in the Intellij config.. I'll edit my question adding this particular. Anyway I do not know what else to try. – lordcroci Jun 12 '20 at 15:07
  • SOLUTION: Hi... after have tried everything regardin UTF-8... I just tried a couple of minutes ago, to change from UTF-8 to charset=ISO-8859-1 in JSP page and the general layout .tag .... Now accented letters are displayed fine! I decided to try this, because in a filer I added the encoding was alreay set on request/response and was UTF-8.. then I tested a conversion from charset=ISO-8859-1 to UTF-8 with online converter and it seemed the problem.. So much time thrown away.. – lordcroci Aug 10 '20 at 21:59
  • Glad you solved this - finally. Congratulations! You are welcome to write your own answer to your question - and to accept that answer, too. It makes the answer much more visible - and you may save someone else a lot of time, this way, also. – andrewJames Aug 10 '20 at 22:13
  • (I am so used to having everything always be _UTF-8 everywhere_ - I forget there are many other character encodings.) – andrewJames Aug 10 '20 at 22:14

0 Answers0