controller
@RequestMapping(value = "/ex01", method = RequestMethod.GET)
public String ex01(Locale locale, Model model) {
logger.info("Ex01 Welcome home! the client locale is "+ locale.toString());
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
model.addAttribute("mulder", "youngsung yu");
return "ex01";
}
interceptor
package mulder.spring.mvc.interceptors;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class SampleInterceptor extends HandlerInterceptorAdapter
{
private static Logger log = Logger.getLogger(SampleInterceptor.class);
/**
* This implementation always returns <code>true</code>.
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response
, Object handler)
throws Exception
{
log.info("preHandle");
return true;
}
/**
* This implementation is empty.
*/
public void postHandle(
HttpServletRequest request, HttpServletResponse response
, Object handler, ModelAndView modelAndView)
throws Exception
{
log.info("postHandle");
if(modelAndView != null)
{
log.info("modelAndView:" + modelAndView);
Map<String, Object> p = modelAndView.getModel();
if(p.get("mulder") != null)
{
log.info("mulder exists:" + (String)p.get("mulder"));
response.setStatus(999);
}
}
}
/**
* This implementation is empty.
*/
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response
, Object handler, Exception ex)
throws Exception
{
log.info("afterCompletion");
}
}
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.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="mulder.spring.mvc" />
<!-- all controller interceptor -->
<!--
<mvc:interceptors>
<beans:bean class="mulder.spring.mvc.interceptors.SampleInterceptor"/>
</mvc:interceptors>
-->
<!-- part interceptor -->
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/ex01"/>
<beans:bean class="mulder.spring.mvc.interceptors.SampleInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans:beans>