Spring MVC 예제 (annotation방식)
Spring MVC 예제
(annotation 방식)
1. new -> other -> Dynimic Web Project 선택 후
Next 버튼 클릭 후
체크박스 선택 후
프로젝트를 생성해줍니당!
2. Maven Project로 등록을 해줍니다
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 |
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>0616</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app> |
cs |
3. Spring_mvc2 프로젝트에서
WebContetn 폴더 내
WEB-INF의 web.xml에
servlet를 등록해줍니당
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 |
package controller;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping("hello.do")
public ModelAndView hello(){
ModelAndView mav = new ModelAndView();
mav.addObject("msg","Hello World");
mav.setViewName("hello");
return mav;
}
@RequestMapping("whatTime.do")
public ModelAndView whatTime(){
ModelAndView mav = new ModelAndView();
mav.addObject("time",new Date());
mav.setViewName("whatTime");
return mav;
}
}
|
cs |
4. controller 패키지내
MyController 클래스를 생성
@Controller의 아들로써 빈으로 등록되게 함
Request를 받아 처리하는 컨트로러써 인식
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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-4.1.xsd">
<context:component-scan base-package="controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="jsp/"/>
<property name="suffix" value=".jsp" />
</bean>
</beans>
|
cs |
5. servlet설정파일을 생성해주고
(WEB-INF)파일안에 생성!
파일이름은
(자신의 ${servlet-name}-servlet.xml 이라는 이름규칙을 설정파일로 사용)
dispatcher-servlet.xml 생성해주고
위와같이 만들어줍니당!
+
요부분은
MyController.java에서
"jsp/hello.jsp"파일이름을
hello라고만 쓰게 하려고
써둔것입니당!
1
2
3
4
5
6
7
8
9
10
11
12 |
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${msg}
</body>
</html> |
cs |
1
2
3
4
5
6
7
8
9
10
11
12 |
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${time}
</body>
</html> |
cs |
6. WebContent 폴더 내에
jsp폴더를 생성 후
hello.jsp 와 whatTime.jsp를 생성해줍니당!
7. 위의 사진과 같이 구성된 것을 확인 할 수 있습니다!
8. Spring_mvc2 프로젝트의
Run As -> Run on Server클릭 후 Finish 버튼을 눌러주세요!
9. 톰캣이 실행되면
404 에러가 뜬다고
당황 NO!
hello.do
또는
whatTime.do
작성해주시면됩니당~
오늘도 열공열공~