Spring mvc 模式小结

http://www.taobaotesting.com/blogs/2375

1、spring mvc简介

Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离。从这样一个角度来说,Spring MVC和Struts、Struts2非常类似。Spring MVC的设计是围绕DispatcherServlet展开的,DispatcherServlet负责将请求派发到特定的handler。通过可配置的handler mappings、view resolution、locale以及theme resolution来处理请求并且转到对应的视图。Spring MVC请求处理的整体流程如图:

 

Spring mvc框架在spring2.5版本之前是基于XML配置实现的,在spring2.5版本之后引入了注解方式,大大减少了之前通过XML配置的繁杂配置,下面我们就分别来看下基于XML配置和注解配置是如何实现spring mvc模式的。

 

2、基于XML配置的spring mvc模式

依赖的jar包:spring-core、spring-web、spring-webmvc、spring-context、spring-aop、spring-orm。

2.1、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        ...

        <servlet>

                <servlet-name>springmvc</servlet-name>

                <servlet-class>

                        org.springframework.web.servlet.DispatcherServlet

                </servlet-class>

                <load-on-startup>1</load-on-startup>

        </servlet>

 

        <servlet-mapping>

                <servlet-name>springmvc</servlet-name>

                <url-pattern>*.do</url-pattern>

        </servlet-mapping>

        ...

</web-app>

 

2.2、springmvc-servlet.xml配置

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

 

<beans>

        <bean id="viewResolver"         class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                <property name="prefix" value="/WEB-INF/jsp/" />

                <property name="suffix" value=".jsp" />

        </bean>

 

        <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

                <property name="mappings">

                        <props>

                                <prop key="/hello.do">helloController</prop>

                        </props>

                </property>

        </bean>

 

        <bean id="helloController" class="com.ideawu.HelloController"></bean>

</beans>

2.3、HelloController.java文件

package com.ideawu;

 

import org.springframework.web.servlet.mvc.Controller;

import org.springframework.web.servlet.ModelAndView;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class HelloController implements Controller {

        public ModelAndView handleRequest(HttpServletRequest request,

                        HttpServletResponse response) throws Exception {

 

                request.setAttribute("hello_1", "你好啊, Spring!");

                request.setAttribute("hello_2", "Hello World!");

 

                return new ModelAndView("hello");

        }

}

2.4、hello.jsp文件

<%@ page contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

        <head>

                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

                <title>Hello World!</title>

        </head>

        <body>

                <h2>${hello_1}</h2>

                <h2>${hello_2}</h2>

        </body>

</html>

                接下来部署应用,我们访问http://localhost:8080/springmvc2-demo/hello.do,会显示视图的内容,并且取出了msg的内容:

 

 

3、基于注解的spring mvc模式

依赖的jar包:spring-core、spring-web、spring-webmvc、spring-context、spring-aop、spring-orm。

3.1、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="WebApp_ID"

    version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >

        ...

 

    <!--  Spring MVC 的Servlet,它将加载WEB-INF/springmvc-servlet.xml的 配置文件,以启动Spring MVC模块-->

    <servlet>

        <servlet-name>springmvc</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

</web-app>

3.2、springmvc-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:p="http://www.springframework.org/schema/p"

        xmlns:context="http://www.springframework.org/schema/context"

        xsi:schemaLocation="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">

 

        <!-- ①:自动搜索@Controller、@Service以及@Repository和@Component 标注的类,对springmvc包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->

        <context:component-scan base-package="com.googlecode.springmvc" />

 

        <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

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

       

        <!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 -->

        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

                <property name="prefix" value="/jsp/" />

                <property name="suffix" value=".jsp"></property>

        </bean>

       

</beans>

3.3、ViewController.java文件

package com.googlecode.springmvc.controller;

 

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

@Controller

public class ViewController {

                        //默认将helloworld.do的请求都转到这个函数来进行处理

                                                @RequestMapping(value="/helloworld.do")

                                                public ModelAndView helloWorld(HttpServletRequest request) {

                                                //这个函数执行后会转到helloWorld.jsp来显示结果,并将"result"属性的值置为map

                                                //这样在jsp显示页面中可以这样调用:Map map = (Map)request.getAttribute("result")

                                                                        ModelMap map = new ModelMap();

                                                                        map.put("one", "one");

                                                                        map.put("two", "two");

                                                                        map.put("three", "two");

                                                                       

                                                                        ModelAndView mAndView = new ModelAndView("helloWorld");

                                                                        mAndView.addObject("result", map);

                                                                        mAndView.addObject("answer","weichao");

                                                                        return mAndView;

                                                }

}

3.4、helloWorld.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page language="java" import="java.util.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<%!

        Map map;

        String answer;

%>

 

  <body>

  my name:

  <%

        //获取属性result的值 -->

        map = (Map)request.getAttribute("result");

 

        //<!—获取属性answer的值 -->

        answer = (String)request.getAttribute("answer");

  %>

  <%=request.getParameterMap().size()%>

 

  <%

        out.print("<br>" + map.get("two"));

        out.print("<br>" + answer);

  %>

  </body>

 

</html>

接下来部署应用,我们访问http://localhost:8080/springmvc3-demo/helloworld.do,会显示视图的内容,并且取出了msg的内容:

 

 

4、基于XML配置与注解的spring mvc模式对比

通过上面的两个例子可以看出,使用注解方式比起XML配置方式,主要有以下几个优点:

1)、Controller 更加灵活 
传统方式:当创建一个 Controller 时,我们需要直接或间接地实现org.springframework.web.servlet.mvc.Controller 接口。一般情况下,我们是通过继承 SimpleFormController 或 MultiActionController 来定义自己的 Controller 的。 
注解方式: Controller 不必继承任何接口,它仅是一个简单的 POJO,开发人员对Controller的代码实现变得更加灵活。 

2)、方便请求和控制器的映射简化配置 
传统方式:需通过Spring MVC的配置文件,在XML 配置文件中定义请求和 Controller 的映射关系,以便将两者关联起来。 
注解方式:可以通过@Controller注解声明将该类的实例添加到Spring 容器中管理,而无需通过Spring MVC的配置文件来配置,大大简化了Spring MVC相关的配置量。 

3)、更加丰富的参数绑定机制 
传统方式:我们都使用HttpServletRequest绑定,对于一个对象绑定需要编写bind帮助类来实现。 
注解方式:通过注解将某个或者某些参数直接绑定到Controller方法的参数上,从而在方法体内,你可以完全对HttpServletRequest视而不见,直接使用已经绑定好的参数。 

除了上面三种优点外,还有第四种优点:
4)、细粒度处理各种request请求 
传统方式:必须通过HttpServletRequest进行判断。 
注解方式:针对最基本的统一请求的GET/POST方式进行不同处理自然不在话下,还可以对拥有不同请求参数的同一request请求分别用不同的方法处理。

如上面3.3中的java类,还可以作如下修改:

@Controller // <——①  

@RequestMapping("/helloworld.do") //<——②  

public class ViewController {

                        @Resource // <——  

private HelloWorldManager helloWorldManger; // 引入需要用到的其他类(上面类中没有,这里试试说明一下可以使用这种方式引入其他类,可以去掉) 

 

                        //默认将helloworld.do的请求都转到这个函数来进行处理

                                                @RequestMapping(params = "method=list") //<——④  

                                                public ModelAndView helloWorld(HttpServletRequest request) {

                                                //这个函数执行后会转到helloWorld.jsp来显示结果,并将"result"属性的值置为map

                                                //这样在jsp显示页面中可以这样调用:Map map = (Map)request.getAttribute("result")

                                                                        ModelMap map = new ModelMap();

                                                                        map.put("one", "one");

                                                                        map.put("two", "two");

                                                                        map.put("three", "two");

                                                                       

                                                                        ModelAndView mAndView = new ModelAndView("helloWorld");

                                                                        mAndView.addObject("result", map);

                                                                        mAndView.addObject("answer","andy");

                                                                        return mAndView;

                                                }

}


①:@Controller注解标明该类需要Spring容器自动加载,将一个类成为 Spring 容器的 Bean。 

②: @RequestMapping 这个注解使得该类具有了Spring MVC Controller 的功能。 
@RequestMapping注解标识ViewController处理来自/helloworld.do的请求。 

@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;还可以标注在方法签名处(注解4后面详细介绍)。所以在类声明处标注的 @RequestMapping 相当于让 POJO 实现了 Controller 接口,而在方法定义处的 @RequestMapping 相当于让 POJO 扩展 Spring 预定义的 Controller(如 SimpleFormController 等)。 

③:注解方式注入 HelloWorldManager 

④: 此处又用了一个@RequestMapping注解,这里的这个注解是为了细粒度区分各个Controller方法,也就是说 helloworld这个方法来处理 /helloworld.do?method=list的请求。 

此外,我们注意到此处返回值是一个String类型,Spring MVC会认为这是你告诉他返回的视图名称,当然此处你也可以返回一个ModelAndView类型,假若你什么也不返回-void,那么Spring会试图查找和你的请求URL同名的视图进行匹配(与配置文件相关)。 

 

另外,还可以通过表单提交的方式来决定使用哪个方法,如:

 

@RequestMapping("/helloworld.do") //<——②  

public class ViewController {

                        //默认将helloworld.do的请求都转到这个函数来进行处理

                        @RequestMapping(method = RequestMethod.POST) 

       //@RequestMapping(value=”/helloworld.do”, method = RequestMethod.POST) 

       public ModelAndView helloWorld(HttpServletRequest request) {

                                                ……

                        }

}     

上面的方式是表示以post方式提交的表单会调用helloWorld方法。

 

还可以通过注解的方式获取请求中、cookies中的参数,具体方法如下:

获取request中的参数,使用@RequestParam

@RequestMapping(value = "/helloworld.do", method = RequestMethod.POST)

public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {

  String username = request.getParameter("username");

  System.out.println(username);

  return “helloworld”;

}

 

获取Cookie的值:使用@CookieValue :(具体使用参照@RequestParam)

获取PrintWriter

可以直接在Controller的方法中传入PrintWriter对象,就可以在方法中使用:

@RequestMapping(value = "helloworld.do", method = RequestMethod.POST)

public String testParam(PrintWriter out, @RequestParam("username") String username) {

  out.println(username);

  return null;

}

Spring mvc 模式小结,布布扣,bubuko.com

Spring mvc 模式小结

上一篇:大厂经验(二):多端可视化埋点解决方案


下一篇:大数据助力酒企精准营销