SpringMVC QuickStart

SpringMVC:QuickStart

  1. 创建一个Maven工程,可采用模板:Create from archetype—>maven-archetype-webapp

  2. 在pom.xml描述文件中添加Spring MVC依赖:https://search.maven.org/

spring-webmvc

坐标:org.springframework:spring-webmvc:5.2.15.RELEASE

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.2.15.RELEASE</version>
</dependency>

完整pom.xml如下:对于后续添加的依赖,会在使用的时候会在新的地方更新pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>

  <name>ywxspringmvc</name>
  <groupId>com.ywx</groupId>
  <artifactId>ywxspringmvc</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.15.RELEASE</version>
    </dependency>
  </dependencies>

</project>

  1. 配置WEB-INF/web.xml

    • 配置DispatcherServlet,控制整个SpringMVC,称为前端控制器

      • 需要初始化的参数使用<init-param>配置,如在生成DispatcherServlet的同时,让他加载配置文件

      • web.xml的约束头

        <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">
        
    • 可自定义DispatcherServlet,此处我们使用org.springframework.web.servlet.DispatcherServlet,同时再src/resources下创建一个空的配置文件springmvc-servlet.xml(文件名自定义)作为springmvc的配置文件。

    • 并且设置为拦截所有的请求

    • 完整web.xml内容

      <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>DispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
          </init-param>
        </servlet>
        <servlet-mapping>
          <servlet-name>DispatcherServlet</servlet-name>
          <url-pattern>/</url-pattern>
        </servlet-mapping>
      
      </web-app>
      
  2. springmvc-servlet.xml,文件名可自定义。

    似乎固定为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:mvc="http://www.springframework.org/schema/mvc"
             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.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">
      
    • 因为要使用到注解,则需要去声明去扫描的路径

    • 再配置一个视图解析器,用来解析逻辑视图,将其转为物理视图(下节会详解,暂照做即可)

    • 完整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:mvc="http://www.springframework.org/schema/mvc"
             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.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="com.ywx"></context:component-scan>
      
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/" />
              <property name="suffix" value=".jsp" />
          </bean>
      
      </beans>
      
  3. 创建Handler

    • Controller注解类型声明某类的实例是一个控制器

    • 在基于注解的控制器类中可以为每个请求编写对应的处理方法。

    • RequestMapping 注解类型将请求与处理方法一一对应

    package com.ywx.handler;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class TestHandler {
        @RequestMapping("/test")
        public String test() {
            System.out.println("该请求被接执行了");
            return "index";
        }
    }
    
    
  4. 若不存在,则在webapp下建立一个index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        Hello,Spring MVC!
    </body>
    </html>
    
  5. 启动服务器,并访问artifacts下/test路径

    如:http://localhost:8888/test

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值