Java – Spring MVC UTF-8 Character Encoding

character encodingjavajspspring-mvcutf-8

At the moment, I'm trying to develop a website by using Spring MVC. But there is a problem in my views, that we can name it as character problem. I want to display UTF-8 characters in my JSP view pages.

In view pages, I've seperated my headers, footers and bodies in my JSP files, and I am implementing my headers and footers into my bodies. (in case to be an important thing to say).

Here is my header intro:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!--[if lt IE 7]> <html dir="ltr" lang="en-US" class="no-js lt-ie9 lt-ie8 lt-ie7" > <![endif]-->
<!--[if IE 7]>    <html dir="ltr" lang="en-US" class="no-js ie7 lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>    <html dir="ltr" lang="en-US" class="no-js ie8 lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html dir="ltr" lang="en-US" class="no-js">
<!--<![endif]-->

<head>
    <!-- Basic Page Needs
  ================================================== -->
    <meta charset="latin5">
    <title>orcunyilmaz.com</title>
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Mobile Specific Metas
  ================================================== -->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    <script src="<c:url value="/resources/js/galleria-1.4.2.min.js"/> "></script>
    <style>
        .galleria {
            width: 600px;
            height: 300px;
            background: #000;
        }
    </style>
    <!-- CSS
  ================================================== -->
    <link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
    <link href="<c:url value="/resources/css/foundation.css"/>" rel="stylesheet">
    <link href="<c:url value="/resources/css/foundation-icons.css"/>" rel="stylesheet">
    <link href="<c:url value="/resources/css/flexslider.css"/>" rel="stylesheet">
    <link href="<c:url value="/resources/css/style.css"/>" rel="stylesheet">
    <link href="<c:url value="/resources/css/mediaqueries.css"/>" rel="stylesheet">
    <link href="<c:url value="/resources/colors/default.css"/>" rel="stylesheet">...

And here is my home.jsp intro:

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@include file="/WEB-INF/views/template/header.jsp" %>

<div id="page">
<div id="main">
    <div class="row search-bar">
        <div class="three columns"></div>
        <aside class="widget widget_search six columns">
            <form action="/" class="searchform" method="get" role="search">
                <input size="27" type="text" title="Ara.." class="s" name="s"> </form>
        </aside>
        <div class="three columns"></div>...

(If whole of the code is needed, I can paste it here)
I've found an answer in this post: Spring MVC UTF-8 Encoding

That says I need to implement some filters into my web.xml, but it didn't work either. Here is the filter that I've implemented to my web.xml:

<filter>
    <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>

Here is the sctreenshot of a part:

In conclusion, here is the screenshot of broken chars

I need it to be seen as: İlan Tarihi & İlan No. So any help will be greatly appreciated, thanks in advance.

Best Answer

To set the encoding of all JSPs to UTF-8 add this snippet to web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

see here https://sorenpoulsen.com/utf-8-encoding-a-jsp-with-spring-mvc