2014년 7월 26일 토요일

[스프링설정파일,Spring MVC설정]스프링설정에서 서로다른업무에 디스패처 서블릿을 각각 할당하기(web.xml, Spring DispatcherServlet)

[스프링설정파일,Spring MVC설정]스프링설정에서 서로다른업무에 디스패처 서블릿을 각각 할당하기(web.xml, Spring DispatcherServlet)

스프링 MVC 프로젝트에서 인사업무, 영업업무에 따라 디스패처 서블릿을 하나씩 할당하고, 설정파일은 따로 두지만 ContextLoaderListener에 같이 설정파일을 로드했습니다.

각 디스패처 서블릿의 설정파일에서 <component-scan base-package>만 따로 두면 @Controller를 통해 컨트롤러는 각각 둘 수 있습니다.

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/appServlet1-servlet.xml,
            /WEB-INF/appServlet2-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>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet1</servlet-name>
<url-pattern>/insa/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>appServlet2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet2</servlet-name>
<url-pattern>/sale/*</url-pattern>
</servlet-mapping>

</web-app>

 
2. /insa쪽의 디스패처 서블릿의 설정파일 (/WEB-INF/appServlet1-servlet.xml)

 
<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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 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="onj.edu.mvc" />
</beans:beans>


3. /sale쪽의 디스패처 서블릿의 설정파일인 /WEB-INF/appServlet-servlet.xml 파일은 빈 파일만 두고

4. 컨트롤러


package onj.edu.mvc;

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.
 */
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
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);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}



5. jsp (/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>



오라클자바커뮤니티교육센터, 개발자전문교육, 개인80%환급
www.oraclejavacommunity.com


평일주간(9:30~18:10) 개강
(7/28)[기업100%환급]안드로이드개발자과정
(8/04)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍
(8/04)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
(8/04)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(8/11)[기업100%환급]SQL기초에서 Schema Object까지
(8/11)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(8/11)[채용예정교육]오라클자바개발잘하는신입뽑기프로젝트,교육전취업확정

평일야간(19:00~21:50) 개강
(7/29)안드로이드개발자과정
(7/29)Spring3.X, MyBatis, Hibernate실무과정
(8/04)웹퍼블리싱 마스터
(8/05)JSP,Ajax,jQUERY,Spring,MyBatis,Hibernate속성과정
(8/08)닷넷(C#,Network,ADO.NET,ASP.NET)마스터과정
(8/11)SQL기초에서실무까지
(8/28)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지

주말(10:00~17:50) 개강
(8/02)Spring3.X, MyBatis, Hibernate실무과정
(8/02)C#,ASP.NET마스터
(8/02)웹퍼블리싱 마스터
(8/02)SQL초보에서 Schema Object까지
(8/09)개발자를위한PLSQL,SQL튜닝,힌트
(8/09)안드로이드개발자과정
(8/09)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(8/23)JAVA,Network&WEB&Framework(자바기초에서웹스프링까지)

주말저녁(18:30~22:20) 개강
(8/02)JAVA,Network&WEB&Framework
(8/09)SQL기초에서실무까지

댓글 없음:

댓글 쓰기