Developing and Configuring the Views and the Controller
Configure JSTL and add JSP header file
required library
- jstl.jar, standard.jar (spring.jar, spring-webmvc.jar, commons-logging.jar)
WebContent/include/include.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
WebContent/index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ include file="/include/include.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm"/>
WebContent/spring/hello.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ include file="/include/include.jsp" %>
<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}"/></p>
</body>
</html>
springapp.HelloController.java
package springapp;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.util.Date;
public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("/spring/hello.jsp", "now", now);
}
}
위에서 /spring/hello.jsp 는 Root Context 아래의 경로인 spring안에 저장된 hello.jsp 으로 forward 한다는 의미이다.
Test : http://localhost/springapp/
위와같이 요청하면 index.jsp --> HelloController --> hello.jsp 순서로 실행되어 결국 웹브라우저 화면에 hello.jsp의 실행결과가 출력된다.
Decouple the view from the controller
위에서 정의한 HelloController 처럼 뒤이어 실행될 View 의 완전한 경로를 사용하기도 하지만, 논리적인 이름만으로도 해당 페이지로 포워드할 수도 있다. 이런 경우에 기대할 수 있는 장점은 콘트롤러 코드를 변경하지 않고도 뷰의 실제경로나 확장자명을 설정파일 상에서 변경하여 사용할 수 있게 된다.
WEB-INF/springapp-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean name="/hello.htm" class="springapp.HelloController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/spring/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
springapp.HelloController.java
package springapp;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import java.io.IOException;
import java.util.Date;public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {String now = (new Date()).toString();
logger.info("Returning hello view with " + now);return new ModelAndView("hello", "now", now);
}}