Spring MVC UTF-8 字符编码

问题描述 投票:0回答:4

目前,我正在尝试使用Spring MVC开发一个网站。但我的观点有一个问题,我们可以将其命名为性格问题。我想在我的 JSP 视图页面中显示 UTF-8 字符。

在视图页面中,我在 JSP 文件中分离了 headersfootersbody,并且我正在将页眉和页脚实现到我的正文中。 (以防万一是要说的重要事情)。

这是我的标题介绍:

<%@ 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">...

这是我的 home.jsp 简介:

<%@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>...

(如果需要完整的代码,我可以将其粘贴到这里) 我在这篇文章中找到了答案:Spring MVC UTF-8 Encoding

这表示我需要在我的 web.xml 中实现一些过滤器,但它也不起作用。这是我在 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>

以下是部分截图:

我需要将其视为:іlan Tarihiіlan No。因此,我们将不胜感激任何帮助,提前致谢。

java jsp spring-mvc utf-8 character-encoding
4个回答
1
投票

要将所有 JSP 的编码设置为 UTF-8,请将此代码段添加到 web.xml 中:

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

参见此处https://sorenpoulsen.com/utf-8-encoding-a-jsp-with-spring-mvc


1
投票

尝试在tomcat目录下的server.xml中的

URIEncoding = "UTF-8"
标签中添加
<Connector >
。另外,如果您使用 Maven,您可以尝试将其添加到
pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
... 
</properties>

1
投票

每个 jsp 文件都需要这一行:

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>

另请检查 IDE 中的编码。


0
投票

我刚刚开始学习 Spring Boot (v3) 我使用Spring boot创建简单的Web应用程序Spring MVC,以支持JSP视图页面 在 pom.xml 中: ... org.apache.tomcat.embed tomcat-嵌入-jasper 11.0.0-M4 javax.servlet 杰斯特尔 1.2 ....
并支持UTF-8编码:按照上面@alex的说明进行操作。你看到了https://stackoverflow.com/search?q=user%3A1614378+Every+jsp-file+needs+this+line%3A

© www.soinside.com 2019 - 2024. All rights reserved.