web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>spring3</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>
<!-- 스프링 환경 설정 시작 -->
<!-- ContextLoaderListener : 서로 다른 DispatcherServlet이 공통으로 사용될 빈 설정 -->
<!-- context-param 태그로 설정파일을 지정하지 않으면 applicationContext.xml이 설정 파일이 된다. -->
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/service.xml,
/WEB-INF/common.xml
</param-value>
</context-param>
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 스프링 컨트롤러 -->
<servlet>
<servlet-name>dispatcher</servlet-name><!-- 환경설정 xml 이름, aa이면 aa-servlet.xml -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 환경설정 파일 명시하는 다른방법
<init-param>
<param-name>contextConfigLocation</param-name> 여기 이름 꼭 이렇게 해야한다.
<param-value>
/WEB-INF/dispatcher-servlet.xml, 여기 이름은 자기 맘대로
/WEB-INF/front.xml
</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 인코딩 필터 -->
<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>/*</url-pattern>
</filter-mapping>
</web-app>
=============================================================================================================================================
dispatcher-servlet.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- 컨트롤러 찾아가는 역할 -->
<bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
<property name="order" value="1"/>
</bean>
<!-- 뷰를 결정하는 역할 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" /> <!-- UserController.java의 mav.setViewName("user/result");의 user -->
<property name="suffix" value=".jsp" /> <!-- UserController.java의 mav.setViewName("user/result");의 result -->
</bean>
<bean id="user.userService" class="com.sp.user.UserServiceImpl"/>
<!-- BeanNameUrlHandlerMapping 예가 주소의 /hello.action 을보고 여기로 진입 -->
<bean name="/hello.action" class="com.sp.user.UserController">
<property name="userService" ref="user.userService"></property>
</bean>
</beans>
=============================================================================================================================================
applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
</beans>
=============================================================================================================================================
UserController.java
package com.sp.user;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
/*
* ModelAndView : 처리결과를 보여줄 뷰와 뷰에 전달할 값을 저장하는 용도
*/
public class UserController extends AbstractController {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req,HttpServletResponse resp) throws Exception {
String s=userService.getMessage();
ModelAndView mav=new ModelAndView();
mav.setViewName("user/result"); // <bean id="viewResolver" 어쩌고 에서 사용됨, user는 폴더이름 result는 jsp
mav.addObject("msg",s); //jsp에서 ${msg}로 사용
return mav;
}
}
=============================================================================================================================================
UserService.java
package com.sp.user;
public interface UserService {
public String getMessage();
}
=============================================================================================================================================
UserServiceImpl.java
package com.sp.user;
import java.util.Calendar;
public class UserServiceImpl implements UserService{
@Override
public String getMessage() {
String s="";
int h=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if(h>=6&&h<=8)
s="아침이다! 일어나기 싫어 시밤 ㅠㅠ";
else if(h>8&&h<=12)
s="열공 ㄴㄴ해";
else if(h>12&&h<=13)
s="점심..";
else if(h>=13&&h<18)
s="오후 열공....";
else
s="끝......";
return s;
}
}
=============================================================================================================================================
결과
'spring > MVC' 카테고리의 다른 글
Spring MVC (0) | 2013.07.28 |
---|