HTML부분
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
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html>
 
<head>
    <meta charset="UTF-8">
    <title>jquery를 이용한ajax json</title>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
 
<body>
    <input type="email" id="email" placeholder="email" />
    <button id="chkemail">중복확인</button>
</body>
<script>
var data = {}; //data 변수를 배열로 생성합니다.
  //중복확인 클릭시
  //jquery에서는 #은 아이디 .은 클래스를 표현합니다. $("#ID명") $(".CLASS명")
    $("#chkemail").click(function () {    // 아이디값이 chkemail인 것을 클릭했을시 function을 실행합니다.
        data.email = $("#email").val();    // data 변수에 email항목을 만듭니다. 그리고 id값이 email의 value값을 대입합니다.
        data.url = "./emailAuth";        // data변수에 url항목을 생성하고 그 값에 emailAuth를 대입합니다.
        sendServer(data);                // sendServer함수에 data를 담아서 보냅니다.
    });
 
    function sendServer(data) {            
/*        
        $.ajax({
            type: "get",                // type에는 "get || post"를 사용할 수 있습니다.
            url : data.url,                // url은 서버에 보내질 url위치를 적어줍니다.
            data : data,                // 서버에 보낼 data를 입력합니다. data : data, 라고 적혀있지만 앞에는 ajax의 형식이고 뒤에는 피라메터 값 입니다.
            dataType: "JSON",            // json을 사용할 것이기에 json을 적어줍니다.
            success: function(success) {
                console.log(success);    // 성공시 서버에서 가져온 값을 콘솔에 보여줍니다.
            },
            error: function(error) {
                console.log(error);        // 실패시 에러값을 보여줍니다.
            }
        
        });
*/                
        $.ajax({
            type: "get",
            url: data.url,
            data: data,
            dataType: "JSON",
            success: function (data) {
                if(url == "./rest/emailAuth"){
                    if (data.emailChk < 0) {
                        alert('이미 사용중인 이메일 입니다.');
                    } else {
                        alert('사용 가능한 이메일 입니다.');
                    }                      
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
 
</script>
 
</html>

cs

@ResponseBody가 오류가 날때
pom.xml에 아래 항목을 추가합니다.
1
2
3
4
5
6
<!-- JACKSON -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
cs

Controller 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Controller("restController")    //컨트롤러 명 지정
@RequestMapping(value = "/rest"// 컨트롤러의 위치를 정해줍니다. 
public class restController {
 
    @Autowired
    OverlayService service;
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    // 중복확인(이메일)
    @RequestMapping(value = "/emailAuth")
    public @ResponseBody Map<StringString> emailAuth(@RequestParam("email"String email) {
        return service.emailAuth(email);
    }
 
}
cs


Service 부분
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
@Service
public class OverlayService {
 
    @Autowired
    SqlSession sqlSession;
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    OverlayInterface inter = null;
 
// 이메일 중복확인
    public Map<StringString> emailAuth(String email) {
        Map<StringString> jsonObj = new HashMap<StringString>();
 
        inter = sqlSession.getMapper(OverlayInterface.class);
        String emailAuth = inter.emailAuth(email);
 
        if (emailAuth != null) {
            jsonObj.put("chk""-1");
        } else {
            jsonObj.put("chk""1");
        }
        return jsonObj;
    }
    
}
cs


DAO 부분
1. Interface
1
2
3
4
5
6
public interface OverlayInterface {
 
    //이메일 중복확인
    String emailAuth(String email);
 
}
cs

2. sql.xml부분
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
<mapper namespace="com.rest.dao.OverlayInterface">
    <!--중복체크-->
    <select id="emailAuth" resultType="String">
        SELECT mm_email FROM members WHERE mm_email = #{param1}
    </select>
</mapper>
cs


DTO 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
public class UserInfoDTO {
 
    private String mm_email; // 이메일
 
    public String getMm_email() {
        return mm_email;
    }
 
    public void setMm_email(String mm_email) {
        this.mm_email = mm_email;
    }
 
}
cs


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

회원가입  (0) 2017.08.24
MyBatis 세팅  (0) 2016.06.15
STS설정 및 스프링 한글깨짐 방지 설정  (0) 2016.06.13

Controller 부분

1
2
3
4
5
6
@RequestMapping(value = "regist")
public ModelAndView regist(HttpServletRequest req) {
    String id = req.getParameter("email");
    String pw = req.getParameter("password");
    return service.regist(id, pw);
}
cs

req.getParameter("JSP_NAME");
jsp에서 form 안에있는 name값을 가져온다.

service 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Service
public class TestService {
 
    @Autowired
    SqlSession sqlSession;
    
    TestInterface inter = null;
    
    // 회원가입
    public ModelAndView regist(String id, String pw) {
        ModelAndView mav = new ModelAndView();
        inter = sqlSession.getMapper(TestInterface.class);
        int success = inter.regist(id, pw);
        
        mav.setViewName("redirect:/");
        return mav;
    }
 
}
cs


DTO 부분
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class TestDTO {
    private String test_id;
    private String test_pw;
 
    public String getTest_id() {
        return test_id;
    }
 
    public void setTest_id(String test_id) {
        this.test_id = test_id;
    }
 
    public String getTest_pw() {
        return test_pw;
    }
 
    public void setTest_pw(String test_pw) {
        this.test_pw = test_pw;
    }
}
cs


DAO interface 부분

1
2
3
public interface testInterface {
    int regist(String id, String pw);
}
cs
DAO sqlMapper 부분

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.test.dao.TestInterface">
    <insert id="regist">
        INSERT INTO testmem(test_id, test_pw)
            VALUES(#{param1}, #{param2})
    </insert>
</mapper>
cs


JSP 부분


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<!DOCTYPE html>
<html>
 
<head>
    <meta charset="UTF-8">
    <title>회원가입</title>
</head>
 
<body>
    <form action="regist" method="post">
        <input type="email" id="email" name="email" placeholder="email"/>
        <input type="password" id="password" name="password" placeholder="password"/>
        <input type="submit" />
    </form>
 
</body>
 
</html>
cs

form 태그의 action은 컨트롤러에서 @RequestMapping(value = "JSP_ACTION") 에 들어간다.
input 태그 안에 name은 서버로 넘겨지는 값.


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

jquery를 이용한 ajax json  (0) 2017.08.28
MyBatis 세팅  (0) 2016.06.15
STS설정 및 스프링 한글깨짐 방지 설정  (0) 2016.06.13

프로젝트 생성 : Spring Legacy Project
프로젝트명 : 03_MyBatis
템플릿 : Spring MVC Project
top-level package : com.mybatis.controller

pom.xml 추가 부분은 굵은 글씨로 표현

repositoriy추가관련 : http://mavenrepository.com/


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mybatis</groupId>
    <artifactId>controller</artifactId>
    <name>03_MyBatis</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.0.0.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
 
 
    <!-- 사설 저장소 추가 -->
    <repositories>
        <repository>
            <id>codelds</id>
            <url>http://code.lds.org/nexus/content/groups/main-repo</url>
        </repository>
    </repositories>
 
 
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
 
        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>
 
 
        <!-- Oracle JDBC -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
 
        <!-- commons-dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
 
        <!--Spring-JDBC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
 
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <!-- mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
 
 
        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>
 
        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
 
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
 
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
 


cs


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
<?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-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>
    </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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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">
 
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
 
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
 
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <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.mybatis.controller" />
    
    <!-- 데이터 소스(DB 접속 정보) -->
    <beans:bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName"    value="oracle.jdbc.driver.OracleDriver" />
        <beans:property name="url"                value="jdbc:oracle:thin:@localhost:1521:xe" />
        <beans:property name="username"           value="DATABASE_ID" />
        <beans:property name="password"           value="DATABASE_PASSWORD" />
    </beans:bean>
    
    <!-- 마이바티스 mapper(쿼리 xml) 위치설정 -->
    <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="mapperLocations" value="classpath:com/mybatis/dao/*.xml"/>
    </beans:bean>
    
    <!-- sqlsession template 설정 (DB처리 관련기능 처리) -->
    <beans:bean id="mybatisMapper" class="org.mybatis.spring.SqlSessionTemplate">
        <beans:constructor-arg index="0" ref="sqlSessionFactory"/>
    </beans:bean>
    
</beans:beans>
 
cs


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

jquery를 이용한 ajax json  (0) 2017.08.28
회원가입  (0) 2017.08.24
STS설정 및 스프링 한글깨짐 방지 설정  (0) 2016.06.13

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