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 jspI'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"%>
declarativeI'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";
}