스프링(Spring)에서 글자가 깨질 경우

    스프링에서 글자가 깨질 경우, Tomcat에서 URIEncoding을 할 경우 한번 더 깨지게 된다. 즉 스프링을 Tomcat 서버 설정을 해도 무의미하다. 스프링에서는 자체적인 encodingFilter를 제공하는데 이 인코딩 필터를 web.xml에 추가로 넣으면 된다




      문제상황


    1
    2
    3
    4
    5
    6
    7
    12월 15, 2017 10:15:05 오전 org.apache.coyote.AbstractProtocol start
    정보: Starting ProtocolHandler ["http-bio-8080"]
    12월 15, 2017 10:15:05 오전 org.apache.coyote.AbstractProtocol start
    정보: Starting ProtocolHandler ["ajp-bio-8009"]
    12월 15, 2017 10:15:05 오전 org.apache.catalina.startup.Catalina start
    정보: Server startup in 3153 ms
    INFO : co.kr.test.rami.controller.RamiController - ?????¤??¸
    cs


    logger를 찍어보면, 글자가 깨진 것을 볼 수 있다.



    마찬가지로 화면도 이렇게 깨져있음



      추가소스


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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
    cs



      최종소스


    web.xml에 위 소스를 추가한다


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5">
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <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
      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>
    cs



      문제해결


    1
    2
    3
    4
    5
    6
    7
    8
    12월 15, 2017 10:19:01 오전 org.apache.coyote.AbstractProtocol start
    정보: Starting ProtocolHandler ["http-bio-8080"]
    12월 15, 2017 10:19:02 오전 org.apache.coyote.AbstractProtocol start
    정보: Starting ProtocolHandler ["ajp-bio-8009"]
    12월 15, 2017 10:19:02 오전 org.apache.catalina.startup.Catalina start
    정보: Server startup in 3334 ms
    INFO : co.kr.saramin.rami.controller.RamiController - 테스트
    INFO : co.kr.saramin.rami.controller.RamiController - 1
    cs





    반응형

    댓글

    Designed by JB FACTORY