[ 0-1. 기본 자바 웹에 스프링 설정하기 ]

1. Dynamin Web Project 생성

2. WEB-INF/lib에 Spring관련 lib 넣기

실제 작동을 하는데 필요한 것을 넣어 준다.

3. build path에 Spring관련 lib 잡기

개발을 하는데 필요한 것을 넣어 준다.

4. web.xml에 DispatcherServlet 등록하기

WEB-INF/config/dispatcher-servlet.xml을 만들어 주기

<?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_4_0.xsd" id="WebApp_ID" version="4.0">
	
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping> 
</web-app>

이제 web.xml에 DispatcherServlet 등록하기

<?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" 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>01JavaEESpring</display-name>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>
    </init-param>
  </servlet>
   
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

5. servletName-servlet.xml이라는 이름 규칙을 따른 bean 설정 파일 만들기

action-servlet.xml을 만들어 주었다.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>


</beans>

6. HomeController만들기 (implements Controller)

package web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HomeController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		return new ModelAndView("home.jsp");
	}
	
}

7. bean 설정 파일에 HomeController 등록하기 (id=homeController)

action-servlet.xml을 다음과 같이 수정

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="homeController" class= "web.controller.HomeController"></bean>

</beans>

8. bean 설정 파일에 HomeController 라우팅 설정하기

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="homeController" class= "web.controller.HomeController"></bean>
	 <bean id="urlMapping" 
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  	<property name="mappings">
  		<props>
  			<prop key="/home.do">home</prop>
  		</props>
  	</property>
  </bean>
	
</beans>

9. view 페이지 작성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	home jsp
</body>
</html>

10. 최종 실행

좋은 웹페이지 즐겨찾기