Spring MVC

Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离。SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,Handler处理以后再返回相应的模型(Model)和视图(View),返回的视图和模型都可以不指定,即可以只返回Model或只返回View或都不返回。通过可配置的handler mappings、view resolution、locale以及theme resolution来处理请求并且转到对应的视图。Spring MVC请求处理的整体流程如图:

mvc

通过一个小例子来讲解Spring MVC,关于架包:最好不用用户库,而是将架包拷贝到lib目录下

项目结构如下:

image

Spring3.x中定义一个控制器类,必须以@Controller注解标记。当控制器类接收到一个请求(/first)时,它会在自己内部寻找一个合适的处理方法来处理请求。使用@RequestMapping注解将方法映射到一些请求上,以便让该方法处理那些请求。
控制器在选择好适合处理请求的方法时,传入收到的请求(根据方法参数类型,可能以不同的类型传入),并且调用该方法中的逻辑来进行处理(也可以是调用Service来真正处理)。方法逻辑可能也会在参数中添加或者删除数据。处理方法处理完之后,会委派给一个视图(return "firstspringmvc"),由该视图来处理方法的返回值。处理程序的返回值并不代表视图的具体实现,可以只是String类型,代表视图名,甚至是void(这时候Spring MVC可以根据方法名或者控制器名找默认视图)。也不需要担心返回值只是视图名称的话,视图拿不到要显示的数据。因为方法参数对于视图来说也是可以拿到的。比如说,如果处理方法以Map为参数,那么这个Map对于视图也是可以拿到的。
返回的视图名称会返回给DispatcherServlet,它会根据一个视图解析器将视图名称解析为一个具体的视图实现。这里说到的视图解析器是一个实现了ViewResolver接口的Bean,它的任务就是返回一个视图的具体实现(HTML、JSP、PDF等等)。

下面是具体的代码实现:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>springmvc</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>
<servlet>
     <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如下面的servlet-name为first,所以servlet.xml配置文件的名称为:first-servlet.xml
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/first-servlet.xml</param-value>
    </init-param>
    -->
    <!-- 可任意,会决定servlet.xml配置文件,所以设置时根据业务需求进行设置 -->
    <servlet-name>first</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>first</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring配置 -->
<!-- ====================================== -->
<!-- <listener>
   <listener-class>
     org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param> -->
</web-app>

first-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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-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">
 
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />
    <!-- 启用Spring Mvc 注解 -->
    <context:annotation-config />
    <!--启用自动扫描 ,设置使用注解的类所在的包-->
    <context:component-scan base-package="springmvc.controller" />
    <!-- 完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

FirstController.java

package springmvc.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class FirstController {
//     在浏览器中的输入,可任意
    @RequestMapping(value="/first",method=RequestMethod.GET)
    public String sayFirst(Model model) {
        model.addAttribute("msg", "This is my first springmvc page!");
//        return的字符串要跟jsp文件名相同
        return "firstspringmvc";
    }
}

firstspringmvc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>first.jsp</title>
</head>
<body>
    ${msg}
</body>
</html>

http://localhost:8888/springmvc/first

image

其中8888为自己修改的tomcat服务器的端口号,默认为8080

原文地址:https://www.cnblogs.com/tufujie/p/5659984.html