스프링 디스패처 서블릿의 분리, 컨트롤러의 분리 예제

FSP 0 231 04.30 16:02

스프링 디스패처 서블릿의 분리, 컨트롤러의 분리 예제 

 

1. web.xml

 

<?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 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/spring/root-context.xml,

/WEB-INF/saleServlet-servlet.xml,

/WEB-INF/insaServlet-servlet.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>

 

<!-- 인사관리 -->

<servlet>

<servlet-name>insaServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>insaServlet</servlet-name>

<url-pattern>/insa</url-pattern>

</servlet-mapping>

<!-- 영업관리 -->

<servlet>

<servlet-name>saleServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>saleServlet</servlet-name>

<url-pattern>/sale</url-pattern>

</servlet-mapping>

 

</web-app>




2. WEB-INF/insaServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<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>
<context:component-scan base-package="a.b.hello3" />
</beans:beans>


3. WEB-INF/saleServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<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>
<context:component-scan base-package="a.b.hello3" />
</beans:beans>


4. InsaController.java

package a.b.hello3;

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;
@Controller
@RequestMapping("/insa")
public class InsaController {
private static final Logger logger = LoggerFactory.getLogger(InsaController.class);
@RequestMapping(value = "/insa", method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute("serverTime", "insa~~~" );
logger.info("insa");
System.out.println("--------------------insa");
return "home";
}
}



5. SaleController.java

package a.b.hello3;

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;
@Controller
@RequestMapping("/sale")
public class SaleController {
private static final Logger logger = LoggerFactory.getLogger(SaleController.class);
@RequestMapping(value = "/sale", method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute("serverTime", "sale~~~" );
logger.info("sale");
return "home";
}
}



6. WEB-INF/views/home.jsp

<%@ 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>

Comments