< 소스 구조 >
web.xml>>
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring04_mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 인코딩필터 설정하기 : 시작 utf-8 설정. -->
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/-</url-pattern>
</filter-mapping>
<!-- 인토딩필터 설정하기 : 종료 -->
<!-- DispatcherServlet 설정 시작-->
<!-- mvc서블릿(<servlet-name> 이름)이 읽을 Bean 설정파일이 있어야 하며,
파일 이름은 반드시 서블릿이름-servlet.xml이어야 한다.
예) 아래 서블릿의 이름이 mvc이므로 설정파일이름은 mvc-servlet.xml이 있어야 한다.
(여기에 mvc에 관련된 Bean객체들이 정의 된다.-컨트롤러,뷰리절버 등등 .. )
-->
<servlet-name>mvc</servlet-name><!-- 임의로 지정. -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- spring 제공 class. -->
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name><!-- mvc : 위에서 정의한 이름 -->
<url-pattern>*.do</url-pattern><!-- 확장자가 do인 경우 호출한다. -->
</servlet-mapping>
<!-- DispatcherServlet 설정 종료-->
</web-app>
==> 클라이언트에서 *.do 로 들어온경우 DispatcherServlet에서 설정한 정보 참조한다.
==> mvc-servlet.xml 로
(1) main.jsp
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>
<ul>
<li><a href="fortune.do">오늘의 운세</a></li>
<li><a href="news.do">오늘의 뉴스</a></li>
</ul>
</body>
</html>
(2) mvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 컨트롤로 설정 -->
<bean name="newsController" class="test.controller.NewsController" />
<bean name="insertController" class="test.controller.InsertController" />
<!-- 뷰리절버 설정
뷰이름이 showFortune 이라면 뷰이름앞에 prefix값이 붙고 뷰이름뒤에 suffix붙어서
아래와 같은 경로가 된다.
showFortune
prefix : /view/showFortune
suffix : /view/showFortune.jsp
이동할 페이지: /view/showFortune.jsp
-->
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
(3) FortuneController.java
package test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class FortuneController {
//fortune.do라는 이름으로 요청이 들어오면 getFortune메소드가 자동 호출됨.
@RequestMapping("/fortune.do")
public ModelAndView getFortune(){
String str="동쪽으로 가면 귀인을 만남!";
//모델과 뷰에 대한 정보를 담을 객체 생성.
ModelAndView mv = new ModelAndView();
//결과값 담기
mv.addObject("fortune", str);
//이동할 뷰이름 담기
mv.setViewName("showFortune");
// DispatcherServlet에 모델앤뷰객체 보내기.
return mv;
}
}
(4) showFortune.jsp
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>/view/showFortune.jsp</title>
</head>
<body>
<h2>오늘의 운세: ${ fortune }</h2>
</body>
</html>
(5) NewsController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class NewsController {
@RequestMapping("/news.do")
public ModelAndView news(){
String str="오늘 날시가 따듯해요!!";
ModelAndView mv = new ModelAndView();
mv.addObject("news", str);
mv.setViewName("showNews");
return mv;
}
}
(6) showNews.jsp
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>
<h1>오늘의 뉴스</h1>
${ news }
</body>
</html>
(1) insert.jsp
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>
<form action="insert1.do" method="post">
이름<input type="text" name="name"/><br/>
나이<input type="text" name="age"/><br/>
이메일<input type="text" name="email"/><br/>
<input type="submit" value="전송"/>
</form>
</body>
</html>
==> name, age, email을 post방식으로 전송.
방법 2 - HttpServletRequest request
; public ModelAndView insert1(HttpServletRequest req){
(2) InsertController.java
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class InsertController {
@RequestMapping("/insert.do")
@RequestParam("age") int age,
@RequestParam("email") String email){
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.setViewName("member/showResult");
return mv;
}
@RequestMapping("/insert1.do")
String name=req.getParameter("name");
String age=req.getParameter("age");
String email=req.getParameter("email");
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.setViewName("member/showResult");
return mv;
}
}
(3) mvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 컨트롤로 설정 -->
<bean name="fortuneController" class="test.controller.FortuneController"/>
<bean name="newsController" class="test.controller.NewsController" />
<bean name="insertController" class="test.controller.InsertController" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
(4) showResult.jsp
<%@ 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>
${ name }
</body>
</html>
'WEB > Spring3.0' 카테고리의 다른 글
SPRING3.0 #11(annotation) (0) | 2013.01.27 |
---|---|
SPRING3.0 #10( MVC ) (0) | 2013.01.27 |
SPRING3.0 #08( Mybatis ) (0) | 2013.01.27 |
SPRING3.0 #07( jdbc ) (0) | 2013.01.27 |
SPRING3.0 #06( annotation ) (0) | 2013.01.27 |