Spring MVC入门案例与讲解

创建项目

Spring MVC入门案例与讲解_第1张图片
Spring MVC入门案例与讲解_第2张图片
Spring MVC入门案例与讲解_第3张图片

关联java,resources目录

Spring MVC入门案例与讲解_第4张图片

引入依赖

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
    <spring.version>5.2.2.RELEASEspring.version>
  properties>

  <dependencies>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>

    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>2.5version>
      <scope>providedscope>
    dependency>

    <dependency>
      <groupId>org.thymeleafgroupId>
      <artifactId>thymeleaf-spring5artifactId>
      <version>3.0.9.RELEASEversion>
    dependency>

    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.2.3version>
    dependency>
  dependencies>

web.xml配置

  • 配置前端控制器DispatcherServlet
  • 服务启动时,自动加载springmvc.xml配置文件
DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
  
 
  <filter>
    <filter-name>characterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
	
  	
  <filter>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
  
  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    
    
    <load-on-startup>1load-on-startup>
  servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

springmvc.xml配置

  • 在resources目录下新建springmvc.xml并配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="mvc.demo"/>
    
 	
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    bean>
                property>
            bean>
        property>

    
    <mvc:annotation-driven/>

beans>

案例展示

  • 创建控制器类
    Spring MVC入门案例与讲解_第5张图片

  • 在templates目录下新建index.html和success页面
    Spring MVC入门案例与讲解_第6张图片
    Spring MVC入门案例与讲解_第7张图片

  • 配置Tomcat过程略

  • 访问效果: http://localhost:8080/

Spring MVC入门案例与讲解_第8张图片
Spring MVC入门案例与讲解_第9张图片

讲解

  • Spring MVC流程
    Spring MVC入门案例与讲解_第10张图片
  1. 浏览器发送http://localhost:8080/hello请求到前端控制器DispatcherServlet;
  2. 前端控制器DispatcherServlet请求处理映射器HandlerMapping查找对应的@RequestMapping("/hello")注解所在的类;
  3. 处理适配器HandlerAdapter去执行对应方法并返回ModelAndView前端控制器DispatcherServlet;
  4. 前端控制器DispatcherServlet请求视图解析器ViewResolver进行视图解析,再返回View;
  5. 最后响应请求;

@RequestMapping()

  • 可以放在类上,也可以放在方法上,如果在类上也有此注解,访问某个方法时必须把类设置的URL也带上,如/usr/add;
  • 常用有两个属性:
  1. value : 用于指定请求的URL,value = "/hello";
  2. method : 用于指定请求的方式,method = {RequestMethod.GET};

你可能感兴趣的:(Spring,MVC)