■ @Controller 어노테이션 : 클래스가 스프링 MVC의 컨트롤러를 구현한 클래스라는 것을 지정
■ @RequestMapping 어노테이션 : 값으로 지저한 요청 경로를 처리할 메서드를 설정
■ ModelAndView : 컨트롤러의 처리 결과를 보여줄 뷰와 뷰에서 출력할 모델을 지정
HelloController.java
package sp.mvc.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
@Controller public class HelloController {
@RequestMapping("/hello.sp") public ModelAndView hello(){ ModelAndView modelAndView = new ModelAndView(); System.out.println("==================="); modelAndView.setViewName("hello"); modelAndView.addObject("name", "guest"); return modelAndView; } } |
spring303-servlet.xml
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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spring303</display-name>
<filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- 공통 빈 설정 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> -->
<servlet> <servlet-name>spring303</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring303</servlet-name> <url-pattern>*.sp</url-pattern> </servlet-mapping>
</web-app> |
hello.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> |
[참고자료] Spring 3.0 프로그래밍-최범균