<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
- mapping을 따로 안했을경우
: servlet을 따로 지정하지 않으면 dispatcher-servlet.xml 파일을 디폴트로 찾음
Dynamic Web Project
-> 수동으로 Spring MVC
-> 다른 기술 방법(효과적 ..)
Maven => Web Project
MVCEx04.WebContent.WEB-INF.web.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Spring Controller 설정파일 지정 -->
<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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
그동안 했던 방식과 다르게 dispatcher를 appServlet으로 바꿔주고 context.xml을 servlet-context.xml로 바꾸어 준다. 경로도 추가되었으니 아래에 자세히 설명.
MVCEx04.WebContent.WEB-INF.spring.appServlet.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
MVCEX02에서 ListAction1,2 view, index 를 복사해 온다
MVCEx04.WebContent.WEB-INF.spring.appServlet.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean name="/list1.do" class="spring.ListAction1" />
<beans:bean name="/list2.do" class="spring.ListAction2" />
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
MVCEx04.WebContent.WEB-INF.web.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Spring Controller 설정파일 지정 -->
<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-context1.xml
/WEB-INF/spring/appServlet/servlet-context2.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCEx04.WebContent.WEB-INF.spring.appServlet.servlet-context1.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean name="/list1.do" class="spring.ListAction1" />
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
MVCEx04.WebContent.WEB-INF.spring.appServlet.servlet-context2.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean name="/list2.do" class="spring.ListAction2" />
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
MVCEx05
Spring Legacy Project
MVCEx04에서 했던 과정이 위 과정으로 생성하면 자동으로 세팅이 되어서 나온다. 이렇게 사용하기 위해 왜 이렇게 설정해야하는지를 알아야 하기 때문에 위 과정을 선행한 것이다.
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 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="com.exam.mvc" />
</beans:beans>
위와 같은 사항은 나중에 자세히 다루기로 하고 지금은 다시 MVCEx04로 돌아가자
MVCEx04.share.ShareData
package share;
public class ShareData {
private String shareData1;
public String getShareData1() {
return shareData1;
}
public void setShareData1(String shareData1) {
this.shareData1 = shareData1;
}
}
MVCEx04.spring.ListAction1
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import share.ShareData;
public class ListAction1 implements Controller {
private ShareData shareData;
public void setShareData(ShareData shareData) {
this.shareData = shareData;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction1() 생성");
System.out.println("shareData : " + shareData.getShareData1());
return new ModelAndView("listview1");
}
}
MVCEx04.spring.ListAction2
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import share.ShareData;
public class ListAction2 implements Controller {
private ShareData shareData;
public void setShareData(ShareData shareData) {
this.shareData = shareData;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction2() 생성");
System.out.println("shareData : " + shareData.getShareData1());
return new ModelAndView("listview2");
}
}
MVCEx04.WebContent.WEB-INF.web.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 공유 데이터를 위한 클래스 설정 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Controller 설정파일 지정 -->
<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-context1.xml
/WEB-INF/spring/appServlet/servlet-context2.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCEx04.WebContent.WEB-INF.spring.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 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="shareData" class="share.ShareData">
<property
name="shareData1"
value="난 공유자료임" />
</bean>
</beans>
MVCEx04.WebContent.WEB-INF.spring.appServlet.servlet-context1.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean name="/list1.do" class="spring.ListAction1" >
<beans:property name="shareData" ref="shareData" />
</beans:bean>
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
MVCEx04.WebContent.WEB-INF.spring.appServlet.servlet-context2.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean name="/list2.do" class="spring.ListAction2" >
<beans:property name="shareData" ref="shareData" />
</beans:bean>
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
공유데이터를 쓰려고 한다면 root-context를 이용하면 된다.
MVCEx04.spring.ListAction1
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import share.ShareData;
public class ListAction1 implements Controller {
private ShareData shareData;
public void setShareData(ShareData shareData) {
this.shareData = shareData;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction1() 생성");
System.out.println("shareData : " + shareData.getShareData1());
shareData.setShareData1("ListAction1에서 변경된 데이터");
return new ModelAndView("listview1");
}
}
MVCEx04.spring.ListAction2
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import share.ShareData;
public class ListAction2 implements Controller {
private ShareData shareData;
public void setShareData(ShareData shareData) {
this.shareData = shareData;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction2() 생성");
System.out.println("shareData : " + shareData.getShareData1());
shareData.setShareData1("ListAction2에서 변경된 데이터");
return new ModelAndView("listview2");
}
}
참조 변수값을 같이 출력되게 하려면
MVCEx04.spring.ListAction1
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import share.ShareData;
public class ListAction1 implements Controller {
private ShareData shareData;
public void setShareData(ShareData shareData) {
this.shareData = shareData;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction1() 생성");
System.out.println("shareData : " + shareData.getShareData1());
System.out.println("shareData : " + shareData);
shareData.setShareData1("ListAction1에서 변경된 데이터");
return new ModelAndView("listview1");
}
}
MVCEx04.spring.ListAction2
package spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import share.ShareData;
public class ListAction2 implements Controller {
private ShareData shareData;
public void setShareData(ShareData shareData) {
this.shareData = shareData;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
System.out.println("ListAction2() 생성");
System.out.println("shareData : " + shareData.getShareData1());
System.out.println("shareData : " + shareData);
shareData.setShareData1("ListAction2에서 변경된 데이터");
return new ModelAndView("listview2");
}
}
참조변수값이 같은 것을 확인할 수 있다.
single tone타입이라서 가능한 것이고 만약 scope="prototype"으로 하면 공유가 되지 않는다. 왜냐면 매번 초기화를 거치기 때문이다.
MVCEx04.WebContent.WEB-INF.spring.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 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="shareData" class="share.ShareData" scope="prototype">
<property
name="shareData1"
value="난 공유자료임" />
</bean>
</beans>
어노테이션으로 해보자
이전에 만든 MVCEx05는 지우고 다시 MVCEx05를 MVCEx04와 같은 설정으로 만들자.
MVCEx05.WebContent.WEB-INF.web.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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MVCEx</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 공유 데이터를 위한 클래스 설정 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Controller 설정파일 지정 -->
<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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
MVCEx05.WebContent.WEB-INF.spring.appServlet.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
MVCEx05.src.spring.ConfigController
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ConfigController1 {
@RequestMapping("/list1.do")
public String handleRequest1() {
System.out.println("handleRequest1() 호출");
return "listview1";
}
@RequestMapping("/list2.do")
public String handleRequest2() {
System.out.println("handleRequest2() 호출");
return "listview2";
}
}
MVCEx05.WebContent.WEB-INF.spring.appServlet.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean class="spring.ConfigController1" />
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
listview1, 2를 복사해오고 index도 복사해와서 실행하면 아래와 같이 결과가 출력된다.
MVCEx05.WebContent.index
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="./list1.do">list1.do</a><br />
<a href="./list2.do">list2.do</a><br />
</body>
ModelAndView로 수정해도 된다.
MVCEx05.src.spring.ConfigController
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController1 {
@RequestMapping("/list1.do")
public ModelAndView handleRequest1() {
System.out.println("handleRequest1() 호출");
return new ModelAndView("listview1");
}
@RequestMapping("/list2.do")
public ModelAndView handleRequest2() {
System.out.println("handleRequest2() 호출");
return new ModelAndView("listview2");
}
}
ConfigController를 1과 2로 나누어서 각 list1, 2로 나누어서 해도 정상 동작하게 해보자
MVCEx05.src.spring.ConfigController1
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController1 {
@RequestMapping("/list1.do")
public ModelAndView handleRequest1() {
System.out.println("handleRequest1() 호출");
return new ModelAndView("listview1");
}
}
MVCEx05.src.spring.ConfigController2
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController2 {
@RequestMapping("/list2.do")
public ModelAndView handleRequest2() {
System.out.println("handleRequest2() 호출");
return new ModelAndView("listview2");
}
}
MVCEx05.WebContent.WEB-INF.spring.appServlet.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<beans:bean class="spring.ConfigController1" />
<beans:bean class="spring.ConfigController2" />
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
근데 이러면 servlet-context.xml에서 ConfigController 클래스가 매번 추가될때마다 추가해줘야 하기 때문에 자동 지정해주는 방법으로 해보자
MVCEx05.WebContent.WEB-INF.spring.appServlet.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:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="spring" />
<!--
<beans:bean class="spring.ConfigController1" />
<beans:bean class="spring.ConfigController2" />
-->
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
위와 같이 <context>기능을 쓰기 위해서 bean configuration file 생성시 context 기능을 추가해 줬던 것이다.
이제 데이터를 넣어서 전송해 보자.
우선 GET과 POST 방식을 구별해서 전송해보자
MVCEx05.WebContent.index
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="./list1.do">list1.do</a><br />
<a href="./list2.do">list2.do</a><br />
<form action="./list1.do" method="get">
데이터 : <input type="text" name="data" />
<input type="submit" value="전송" />
</form>
<form action="./list1.do" method="post">
데이터 : <input type="text" name="data" />
<input type="submit" value="전송" />
</form>
</body>
MVCEx05.src.spring.ConfigController1
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController1 {
@RequestMapping(value="/list1.do", method=RequestMethod.GET)
public ModelAndView handleRequest11() {
System.out.println("handleRequest11() 호출");
return new ModelAndView("listview1");
}
@RequestMapping(value="/list1.do", method=RequestMethod.POST)
public ModelAndView handleRequest12() {
System.out.println("handleRequest12() 호출");
return new ModelAndView("listview1");
}
}
data1, data2를 지정하여 두개의 데이터를 입력받고 두개의 데이터를 출력해보자
MVCEx05.src.spring.ConfigController1
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController1 {
@RequestMapping(value="/list1.do", method=RequestMethod.GET)
public ModelAndView handleRequest11(
@RequestParam("data1") String data1,
@RequestParam("data2") String data2
) {
System.out.println("handleRequest11() 호출 :" + data1);
System.out.println("handleRequest11() 호출 :" + data2);
return new ModelAndView("listview1");
}
@RequestMapping(value="/list1.do", method=RequestMethod.POST)
public ModelAndView handleRequest12(
@RequestParam("data1") String data1,
@RequestParam("data2") String data2
) {
System.out.println("handleRequest12() 호출 :" + data1);
System.out.println("handleRequest12() 호출 :" + data2);
return new ModelAndView("listview1");
}
}
MVCEx05.WebContent.index
<%@ 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>
<a href="./list1.do">list1.do</a><br />
<a href="./list2.do">list2.do</a><br />
<form action="./list1.do" method="get">
데이터 : <input type="text" name="data1" />
데이터 : <input type="text" name="data2" />
<input type="submit" value="전송" />
</form>
<form action="./list1.do" method="post">
데이터 : <input type="text" name="data1" />
데이터 : <input type="text" name="data2" />
<input type="submit" value="전송" />
</form>
</body>
값 입력을 안하면 default값이 출력되게 해보자
MVCEx05.src.spring.ConfigController1
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController1 {
@RequestMapping(value="/list1.do", method=RequestMethod.GET)
public ModelAndView handleRequest11(
@RequestParam(value="data1", defaultValue="test1") String data1,
@RequestParam(value="data2", defaultValue="test2") String data2
) {
System.out.println("handleRequest11() 호출 :" + data1);
System.out.println("handleRequest11() 호출 :" + data2);
return new ModelAndView("listview1");
}
@RequestMapping(value="/list1.do", method=RequestMethod.POST)
public ModelAndView handleRequest12(
@RequestParam("data1") String data1,
@RequestParam("data2") String data2
) {
System.out.println("handleRequest12() 호출 :" + data1);
System.out.println("handleRequest12() 호출 :" + data2);
return new ModelAndView("listview1");
}
}
view를 통해서 실행해보자
MVCEx05.src.spring.ConfigController1
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ConfigController1 {
@RequestMapping(value="/list1.do", method=RequestMethod.GET)
public ModelAndView handleRequest11(
@RequestParam(value="data1", defaultValue="test1") String data1,
@RequestParam(value="data2", defaultValue="test2") String data2
) {
System.out.println("handleRequest11() 호출 :" + data1);
System.out.println("handleRequest11() 호출 :" + data2);
// view
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("listview1");
modelAndView.addObject("data1", data1);
modelAndView.addObject("data2", data2);
return modelAndView;
}
@RequestMapping(value="/list1.do", method=RequestMethod.POST)
public ModelAndView handleRequest12(
@RequestParam("data1") String data1,
@RequestParam("data2") String data2
) {
System.out.println("handleRequest12() 호출 :" + data1);
System.out.println("handleRequest12() 호출 :" + data2);
// view
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("listview1");
modelAndView.addObject("data1", data1);
modelAndView.addObject("data2", data2);
return modelAndView;
}
}
MVCEx05.WebContent.WEB-INF.view.listview1
<%@ 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>
listview1.jsp
<br /><hr /><br />
데이터1 : ${ data1 }<br />
데이터2 : ${ data2 }<br />
</body>
</html>
한 클래스 안에서 get방식 post방식 요청을 해보자
MVCEx05.src.spring.ConfigController
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/list1.do")
public class ConfigController {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView doGet() {
System.out.println("Get 요청");
return new ModelAndView("listview1");
}
@RequestMapping(method=RequestMethod.POST)
public ModelAndView doPost() {
System.out.println("Post 요청");
return new ModelAndView("listview1");
}
}
데이터도 전달받아보자
MVCEx05.src.spring.ConfigController
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/list1.do")
public class ConfigController {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView doGet(
@RequestParam("data1") String value1,
@RequestParam("data2") String value2
) {
System.out.println("Get 요청");
System.out.println("value1 : " + value1);
System.out.println("value2 : " + value2);
return new ModelAndView("listview1");
}
@RequestMapping(method=RequestMethod.POST)
public ModelAndView doPost(
@RequestParam("data1") String value1,
@RequestParam("data2") String value2
) {
System.out.println("Post 요청");
System.out.println("value1 : " + value1);
System.out.println("value2 : " + value2);
return new ModelAndView("listview1");
}
}
GET과 POST 방식을 동시에 표현하는 방법으로 해보자
MVCEx05.src.spring.ConfigController
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/list1.do")
public class ConfigController {
@RequestMapping(method= { RequestMethod.GET, RequestMethod.POST })
public ModelAndView doGet(
@RequestParam("data1") String value1,
@RequestParam("data2") String value2
) {
System.out.println("Get 요청");
System.out.println("value1 : " + value1);
System.out.println("value2 : " + value2);
return new ModelAndView("listview1");
}
}
MavenEx01.src.main.webapp.WEB-INF.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</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>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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
MavenEx01.src/main/java.com.naver.board/HomeController
package com.naver.board;
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 = "/index.do", 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";
}
}
MavenEx01.src.main.webapp.WEB-INF.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">
<welcome-file-list>
<welcome-file>index.do</welcome-file>
</welcome-file-list>
<!-- 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</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>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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
oracle 추가하는법
MavenEx01.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.naver</groupId>
<artifactId>board</artifactId>
<name>MavenEx01</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>5.0.8.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<repositories>
<repository>
<id>Datanucleus</id>
<url>http://www.datanucleus.org/downloads/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
MVCBoardEx01을 가지고 오늘 배운 내용으로 수정한 게시판을 만들어보자
MVCBoardEx02.pom - 오라클 설정
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvc</groupId>
<artifactId>board</artifactId>
<name>MVCBoardEx02</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.0.8.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<repositories>
<repository>
<id>Datanucleus</id>
<url>http://www.datanucleus.org/downloads/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
MVCBoardEx02.src.webapp.WEB-INF.web - .do로 읽기 설정
<?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">
<welcome-file-list>
<welcome-file>index.do</welcome-file>
</welcome-file-list>
<!-- 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</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>
<!-- 한글 인코딩 필터 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Processes application requests -->
<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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
context.xml을 target.m2e-wtp.web-resources.META-INF에 추가해준다.
WEB-INF.views에 home외에 jsp파일들을 추가한다.
src/main/java에 DAO, TO, ListTO 파일을 paging1이란 패키지에 추가
com.mvc.board 패키지 안 HomeController에 어노테이션으로 실행파일을 설정
package com.mvc.board;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
import org.springframework.web.servlet.ModelAndView;
import paging1.BoardDAO;
import paging1.BoardListTO;
import paging1.BoardTO;
/**
* 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 = "/index.do", 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";
}
@RequestMapping(value="/list.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView ListAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
int cpage = 1;
if(request.getParameter("cpage") != null
&& !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
BoardListTO listTO = new BoardListTO();
listTO.setCpage(cpage);
BoardDAO dao = new BoardDAO();
listTO = dao.boardList(listTO);
ModelAndView modelAndView = new ModelAndView("board_list1");
modelAndView.addObject("listTO", listTO);
return modelAndView;
}
@RequestMapping(value="/write.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView WriteAction(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
return new ModelAndView("board_write1");
}
@RequestMapping(value="/write_ok.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView WriteOkAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
BoardTO to = new BoardTO();
to.setSubject(request.getParameter("subject"));
to.setWriter(request.getParameter("writer"));
to.setMail(request.getParameter("mail1") + "@" + request.getParameter("mail2"));
to.setPassword(request.getParameter("password"));
to.setContent(request.getParameter("content"));
to.setWip(request.getRemoteAddr());
BoardDAO dao = new BoardDAO();
int flag = dao.boardWriteOk(to);
ModelAndView modelAndView = new ModelAndView("board_write1_ok");
modelAndView.addObject("flag",flag);
return modelAndView;
}
@RequestMapping(value="/view.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView ViewAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
BoardDAO dao = new BoardDAO();
to = dao.boardView(to);
ModelAndView modelAndView = new ModelAndView("board_view1");
modelAndView.addObject("cpage", cpage);
modelAndView.addObject("to", to);
return modelAndView;
}
@RequestMapping(value="/delete.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView DeleteAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
BoardDAO dao = new BoardDAO();
to = dao.boardDelete(to);
ModelAndView modelAndView = new ModelAndView("board_delete1");
modelAndView.addObject("to", to);
modelAndView.addObject("cpage", cpage);
return modelAndView;
}
@RequestMapping(value="/delete_ok.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView DeleteOkAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
to.setPassword(request.getParameter("password"));
BoardDAO dao = new BoardDAO();
int flag = dao.boardDeleteOk(to);
ModelAndView modelAndView = new ModelAndView("board_delete1_ok");
modelAndView.addObject("flag", flag);
return modelAndView;
}
@RequestMapping(value="/modify.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView ModifyAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
BoardDAO dao = new BoardDAO();
to = dao.boardModify(to);
ModelAndView modelAndView = new ModelAndView("board_modify1");
modelAndView.addObject("to", to);
modelAndView.addObject("cpage", cpage);
return modelAndView;
}
@RequestMapping(value="/modify_ok.do", method= {RequestMethod.GET, RequestMethod.POST})
public ModelAndView ModifyOkAction(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
String cpage = request.getParameter("cpage");
BoardTO to = new BoardTO();
to.setSeq(request.getParameter("seq"));
to.setPassword(request.getParameter("password"));
to.setSubject(request.getParameter("subject"));
to.setMail(request.getParameter("mail1") + "@" + request.getParameter("mail2"));
to.setContent(request.getParameter("content"));
BoardDAO dao = new BoardDAO();
int flag = dao.boardModifyOk(to);
ModelAndView modelAndView = new ModelAndView("board_modify1_ok");
modelAndView.addObject("flag", flag);
modelAndView.addObject("cpage", cpage);
modelAndView.addObject("seq", to.getSeq());
return modelAndView;
}
}
css, image 등 디자인 요소를 webapp에 추가
MVCBoardEx02.src.main.webapp.WEB-INF.views.home
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
<a href="./list.do">list.do</a><br>
</body>
</html>
'Web & Mobile > Spring' 카테고리의 다른 글
Lecture 76 - Spring(6) Model2 게시판을 Spring으로, 전자정부프레임워크 설정법, 전자정부프레임워크 모바일 (0) | 2019.07.05 |
---|---|
Lecture 74 - Spring(4) AOP, Spring MVC 게시판 (0) | 2019.07.02 |
Lecture 73 - Spring(3) 스프링 게시판 (0) | 2019.07.01 |
Lecture 72 - Spring(2) 스프링 프레임워크를 이용한 게시판 (0) | 2019.06.28 |
Lecture 71 - Spring(1) 스프링 프레임워크 설치법, (0) | 2019.06.27 |
댓글