<?xml version="1.0" encoding="UTF-8"?> <web-app ...=""> ... <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
src/main/webapp/WEB-INF/web.xml
- "<param-value>/WEB-INF/mvc-config.xml</param-value>" --> 스프링 설정 파일로 사용
- "<url-pattern>*.do</url-pattern>" --> *.do로 요청되는 것을 처리한다는 의미
@Controller public class IndexContoller { @RequestMapping("index.do") public ModelAndView index() { ModelAndView mav = new ModelAndView(); mav.setViewName("index"); mav.addObject("hello", "Hello World!!"); return mav; } }
src/main/java/com.chakannom.simple.controller/IndexController.java
<?xml version="1.0" encoding="UTF-8"?> <beans ...> ... <context:component-scan base-package="com.chakannom.simple.controller" /> ... </beans>
src/main/webapp/WEB-INF/mvc-config.xml
- "<context:component-scan base-package="com.chakannom.simple.controller" />" --> @controller를 검색
<?xml version="1.0" encoding="UTF-8"?> ... <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> ... </beans>
src/main/webapp/WEB-INF/mvc-config.xml
뷰 코드<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> <h2>${hello}</h2> </body> </html>
src/main/webapp/WEB-INF/view/index.jsp
댓글 없음:
댓글 쓰기
참고: 블로그의 회원만 댓글을 작성할 수 있습니다.