spring MVC框架

spring MVC框架

一、 框架图


spring MVC框架

二、 代码

1. web.xml

<!-- 配置Spring MVC DispatcherServlet -->
    <servlet>
        <servlet-name>mengka</servlet-name>
        <servlet-class>com.mengka.common.DispatcherController</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置DispatcherServlet所需要拦截的 url -->
    <servlet-mapping>
        <servlet-name>mengka</servlet-name>
        <url-pattern>/mengka/*</url-pattern>
    </servlet-mapping>
/**
 * User: mengka
 * Date: 15-3-22-下午4:02
 */
public class DispatcherController extends DispatcherServlet {

    private static final Logger log = LoggerFactory.getLogger(DispatcherController.class);


    @Override
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String path = request.getRequestURI();
        log.info("------------, path = "+path);


        super.doDispatch(request, response);
        return;
    }

}
  

2. mengka-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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </bean>


</beans>

3. 设置拦截器

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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

       <context:component-scan base-package="com.mengka.app.*"/>
       <mvc:annotation-driven/>

       <!-- 拦截器 -->
       <mvc:interceptors>
              <bean class="com.mengka.interceptor.AllInterceptor"/>
              <mvc:interceptor>
                     <mvc:mapping path="/qingcai/*" />
                     <bean class="com.mengka.interceptor.TbbInterceptor"/>
              </mvc:interceptor>
              <mvc:interceptor>
                     <mvc:mapping path="/baicai/**"/>
                     <!--<mvc:mapping path="/mengka/baicai/list"/>-->
                     <!--<mvc:mapping path="/qingcai/**"/>-->
                     <!--<mvc:mapping path="/**" />-->
                     <bean id="souxianInterceptor" class="com.mengka.interceptor.TaaInterceptor"/>
              </mvc:interceptor>
       </mvc:interceptors>

</beans>
 
public class AllInterceptor implements WebRequestInterceptor {

    private static final Logger log = LoggerFactory.getLogger(AllInterceptor.class);

    @Override
    public void preHandle(WebRequest request) throws Exception {
        String url = request.getContextPath();
        log.info("---------------, AllInterceptor preHandle path = "+url);

        request.setAttribute("request", "request", WebRequest.SCOPE_REQUEST);//这个是放到request范围内的,所以只能在当前请求中的request中获取到
        request.setAttribute("session", "session", WebRequest.SCOPE_SESSION);

    }

    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {

    }

    @Override
    public void afterCompletion(WebRequest request, Exception ex) throws Exception {

    }
}
public class TaaInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(TaaInterceptor.class);

    /**
     *  Controller 方法调用之前执行
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURI();
        log.info("---------------, TaaInterceptor preHandle path = "+url);

        String cookie = "11111111111111";
        request.setAttribute("souxianji", cookie);

        return true;
    }

    /**
     *  Controller 方法调用之后执行:
     *  1.在这个方法中你可以对ModelAndView进行操作
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("---------------, TaaInterceptor postHandle。。。");

    }

    /**
     *  该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("---------------, TaaInterceptor afterCompletion。。。");

    }
}

4. MengkaController控制层

/**
 *  http://127.0.0.1:8080/mengka/get
 *
 * User: mengka
 * Date: 2015/3/20
 */
@Controller
public class MengkaController {

    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public ModelAndView addPushMsg(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> paraMap = new HashMap<String, String>();
        paraMap.put("para", "test aaa...");
        return new ModelAndView("mengka/pushSuccess", paraMap);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ModelAndView mengkaGet(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> paraMap = new HashMap<String, String>();
        paraMap.put("para", "test aaa...");
        return new ModelAndView("mengka/pushSuccess", paraMap);
    }
}

5. 视图层"WEB-INF/pages/mengka/pushSuccess.jsp"

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>test</title>
</head>
<body>
${para}
</body>
</html>


 

相关推荐