<?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 https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<!-- 3) ContextLoaderListener => Context 파일을 호출
=> root-context.xml 실행-->
<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 -->
<!-- 2) appServlet 이름을 찾아오면 DispatcherServlet 동작 (서블릿으로 이동하겠다)
=> 서블릿을 지정 servlet-context.xml -->
<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>
<!-- 1) 가상주소가 /로 시작하는 주소가 들어오면 => 모든 주소
=> servlet-name 동일한 곳을 찾아감 -->
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servlet-context.xml
<?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 https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<!-- 2) 패키지 검색하면 annotation(@) 찾음 => 기능 부여 => 기능에 따라서 자동으로 동작 -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- 4) 이미지, 자바스크립트, css 파일 위치 설정 -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<!-- 3) jsp 파일 위치 설정 prefix 접두사, suffix 접미사 -->
<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>
<!-- 1) 패키지 자동으로 스캔(검색) -->
<context:component-scan base-package="com.itwillbs.myweb" /> <!-- 이 폴더를 자동 스캔해서 동작 -->
</beans:beans>
root-context.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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 데이터베이스 설정 -->
</beans>
HomeController.java
package com.itwillbs.myweb;
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;
/**
* Handles requests for the application home page.
*/
// 1) 자바파일 => @Controller 주소매핑 역할 부여 => 파일 동작
@Controller // 자동화 시켜주는 역할
public class HomeController {
// 주소매핑 작업
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
// 2) 메서드 => @RequestMapping 하나씩 주소 매핑 => 가상주소 자동으로 뽑아와서 value="/" 비교
// => 전송 방식 method = RequestMethod.GET 확인 => 메서드 자동으로 동작
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
// 로고 출력 sysout 해도 됨
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);
// request => 스프링 model 데이터 담기
model.addAttribute("serverTime", formattedDate );
// /WEB-INF/views/파일이름.jsp
// /WEB-INF/views/home.jsp
return "home";
}
}
-home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
가상 주소 http ://localhost:8080/myweb/ 주소매핑 -> home.jsp
가상 주소 http ://localhost:8080/myweb/insert.me 주소매핑 -> insert.jsp
가상 주소 http ://localhost:8080/myweb/write 주소매핑 -> write.jsp
views 폴더에 insert.jsp , wirte.jsp 파일 만들기
컨트롤러에 메서드 추가
@RequestMapping(value = "/insert.me", method = RequestMethod.GET)
public String insert() {
// 처리작업
//
// /WEB-INF/views/insert.jsp
return "insert";
}
@RequestMapping(value = "/write", method = RequestMethod.GET)
public String write() {
// 처리작업
// /WEB-INF/views/write.jsp
return "write";
}