1. STS(Spring Tool Suite)에서 한글이 깨지는 이유

STS(스프링) 에서 한글이 깨지는 이유는 기본 인코딩이 MS949로 되어 있어서 한글이 깨진다.


인코딩을 모두 UTF-8로 변경을 해주어야 한글이 깨지지 않는다.


2. STS 한글깨짐 방지 설정

먼저 Window - Preferences 로 들어가준다.




CSS, HTML, JSP 항목에서 Encoding 항목을 ISO 10646/Unicode(UTF-8)로 변경해 준다.




General -> Workspace의 Text file encoiding 을 UTF-8로 바꿔준다.




General -> Web Brower에서 New를 누른후 Chrome을 설정한다.




Location은 Chrome이 설치된 위치를 지정한다.


3. 프로젝트 생성




Spring Legacy Project로 새로운 Project를 생성한다.



Project Name을 입력하고, Spring MVC Project를 선택한후 Next를 누른다.


네모칸을 채운후 Finish를 누른다.



처음 프로젝트를 생성하면 빨간색 X표가 뜨는데 자동으로 설치가 된 후에 x표가 사라진다.



src/main/java-com.spring.staff-HomeController.java

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
package com.spring.staff;
 
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class HomeController {
    
    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale); // 콘솔창에 나오는 영역
        
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        
        String formattedDate = dateFormat.format(date);
        
        model.addAttribute("serverTime", formattedDate ); // home.jsp에 serverTime부분을 변경해준다.
        
        return "home";
    }
    
}
cs



src-main-webapp-WEB-INF-views-home.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>
 
<P>  The time on the server is ${serverTime}. </P>
</body>
</html>
 
cs

새로운 jsp파일을 만들고 <html>영역을 붙여넣는다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>
 
<P>  The time on the server is ${serverTime}. </P>
</body>
</html>
cs



프로젝트 생성 후에 아래의 경로로 이동하여 한글이 깨지지 않게 인코딩 항목을 추가해 준다.


web.xml에서 한글깨짐 방지 부분을 추가해 준다.

src-main-webapp-WEB-INF-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
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!-- Processes application requests -->
    <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>
    
    <!-- 한글깨짐 방지 --> 
    <!-- filter와 filter-mapping을 만들어 준다. -->
    <filter> 
        <!-- filter안에는 filter-name, filter-class, init-param을 추가해 준다.
              filter-name은 원하는대로 지정해도됨 -->
        <filter-name>encodingFilter</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param> 
            <!-- encoidng값을 UTF-8로 만들어 준다. -->
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
</web-app>
 
cs



src-main-webapp-WEB-INF-spring-appServlet-servlet-context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 컴파일시 참조 역활 /@Controller, @Autowired, @Inject 등을 사용 가능하게 함 -->
    <annotation-driven />
 
    <!-- 좌측에 있는 내용을 우측처럼 축약해서 사용 -->
    <resources mapping="/resources/**" location="/resources/" />
 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    
    <context:component-scan base-package="com.spring.controller" />
    
</beans:beans>
cs



4. Tomcat에서 한글깨짐 설정


실행할 프로젝트명에서 마우스 오른쪽 클릭후 Run As - Run on Server를 클릭한다.



Apache Tomcat v8,0 Server를 선택해 준다.


실행할 프로젝트를 오른쪽으로 보낸후 Finish를 클릭한다.



서버가 실행된 후에 아래와같이 설정을 추가해 준다.

URIEncoding="UTF-8" 인코딩 설정을 UTF-8로 해준다.



63
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="9090" protocol="HTTP/1.1" redirectPort="8443"/>
cs


'프로그래밍 > Spring' 카테고리의 다른 글

jquery를 이용한 ajax json  (0) 2017.08.28
회원가입  (0) 2017.08.24
MyBatis 세팅  (0) 2016.06.15

+ Recent posts