简体   繁体   English

Spring MVC中的UTF-8编码

[英]UTF-8 encoding in Spring MVC

MvcConfiguration MvcConfiguration

@Configuration
@ComponentScan(basePackages="com")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{



       @Bean
        public ViewResolver getViewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }

jsp: jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">


    </head>
    <body class="nav-md">
     <form:form method="POST" modelAttribute="fluxDomaine" class="form-horizontal form-label-left" >
                        <form:input type="hidden" path="id" id="id"/>
                <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="INDCTR_DS">INDCTR DS </label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                              <form:input type="text" path="INDCTR_DS" id="INDCTR_DS" name="INDCTR_DS" class="form-control col-md-7 col-xs-12"/>
                           </div>
                            <form:errors path="INDCTR_DS" cssClass="alerttt" />
                          </div>
<input type="submit" class="btn btn-success" value="Add"/>

</form:form>
    </body>
    </html>

i'm facing a problem with encoding , for example if i save a " à " it becomes " à " and " é " becomes " é ", and when i try to manually save it in my mysql DB its saving proprely. 我在编码方面遇到问题,例如,如果我保存一个“ à ”,它将变成“ à ”,而“ é ”变成“ é ”,当我尝试手动将其保存在我的mysql DB中时,它会适当保存。

Thanks for any advices.. 感谢您的任何建议。

I used to get this trouble while working with Spring MVC too. 在使用Spring MVC时,我也经常遇到这种麻烦。
Just followed this article: http://fazlansabar.blogspot.com/2012/06/how-to-enable-utf-8-support-on-tomcat.html and it's worked. 紧随本文之后: http : //fazlansabar.blogspot.com/2012/06/how-to-enable-utf-8-support-on-tomcat.html ,它已经起作用。

You need to define a SpringCharacterEncoding filter . 您需要定义一个SpringCharacterEncoding过滤器。 Simply add below lines to the web.xml in your Spring MVC project .This will enforce UTF-8 encoding strictly in your code. 只需在Spring MVC项目中的web.xml中添加以下行即可。这将严格在您的代码中强制执行UTF-8编码。

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

Special characters issue, i replaced : 特殊字符问题,我替换为:

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

by 通过

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>

and i was able to persist special characters as i want.. 而且我能够根据需要保留特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM