웹mvc 시작 예제만 벌써 몇번째인가.. 그만큼 스프링 쉽게 볼 대상이 아니다. 오늘 포스팅을 확실히 해두어 또 하는 일은 없어야 겠지..


<이클립스 프로젝트 구조>
스프링mvc를 테스트해보기 위한 최소한의 설정이다.
라이브러리에 spring.jar 외 기타 스프링관련 jar는 몽땅 집어넣자.
servlet-api.jar도 필요하니 빌드패스에서 추가하자. 외부 jar 추가 인가 그 메뉴로...














web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 <display-name>SpringMVC</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 
<!--디스패쳐설정  서블릿네임으로 설정한 것이 디스패쳐서블릿이 springmvc-servlet.xml로드하려고 할 것   -->
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 기본 위치는 [servlet name]-servlet.xml 은
   web-inf에서 불러 오지만 기타 다른위치에 있을때는 위치를 지정한다 -->
<!--    <init-param>-->
<!--     <param-name>contextConfigLocation</param-name>-->
     <!-- 클래스 패스에 위치한 파일로부터 설정정보를 읽으려면 "classpath:" 를 붙이고 경로를 써준다 -->
     <!-- ex) classpath:kr/co/springboard/config.xml -->
<!--     <param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>-->
<!--    </init-param>-->
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   /WEB-INF/config/*.xml
  </param-value>
 </context-param>
 
</web-app>

디스패쳐서블릿 설정에서 따로 경로를 지정해주지 않으면 기본적으로 [서블릿네임]-servlet.xml로 인식하니까 주의하자.
url-pattern은 .htm로 들어오는 모든 주소값을 디스패쳐로 보낸다.

springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
                          
<bean id="mvcService" class="mvc.service.HomePageService">
</bean>

<bean name="/home.htm" class="mvc.controller.HomePageController">
 <property name="mvcService" ref="mvcService"></property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix">
  <value>/view/</value>
 </property>
 <property name="suffix">
  <value>.jsp</value>
 </property>
</bean>
</beans>                          

HomePageController.java
package mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.service.HomePageService;
import java.util.List;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HomePageController extends AbstractController{
 private HomePageService mvcService;
 
 public HomePageController(){};
 
 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
   HttpServletResponse arg1) throws Exception {
  List<String> recent = mvcService.getName();
  return new ModelAndView("home","name",recent);
 }
 //Dependency injection
 public void setMvcService(HomePageService mvcService) {
  this.mvcService = mvcService;
 }
}

HomePageService .java
package mvc.service;
import java.util.ArrayList;
import java.util.List;

public class HomePageService {

 public List<String> getName(){
  List<String> nameList = new ArrayList<String>();
  nameList.add("김태균");
  nameList.add("이범호");
  nameList.add("김태완");
  nameList.add("송광민");
  nameList.add("류현진");
  return nameList;
 }
}

서비스에서 비즈니스 로직을 작성.
빈의 id값과 setter값이 같아야 한다. 그러니까 bean id="mvcService"  와  setMvcService 같아야한다.

home.jsp에서 EL태그사용허면 ${name}


AND