基于maven配置最简SpringMVC

一、我的目录结构

基于maven配置最简SpringMVC_第1张图片

二、maven的相关spring依赖

 <dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>4.3.18.RELEASEversion>
    dependency>

    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>1.1.2version>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
  dependencies>

二、配置Spring的DispatcherServlet

1、什么是DispatcherServlet?
  • DispatcherServlet实质也是一个HttpServlet,DispatcherSevlet负责将请求分发,所有的请求都有经过它来 统一分发到我们自己定义的Controller的方法中。
2、配置方法
  • 在WEB-INF目录下的web.xml文件中配置
 
  <servlet>
    <servlet-name>SpringMVCservlet-name>
    
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath*:spring-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    
    <url-pattern>/url-pattern>
  servlet-mapping>
  • 配置spring-mvc.xml文件,主要是要进行组件扫描,并使用注解,所以 两个标签很重要,具体可以自行百度。到这一步基本上就配置完成,我们去Controller中写一个方法测试一下

<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-4.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    
    <context:component-scan base-package="com.springmvc.controller"/>
    <context:component-scan base-package="com.springmvc.service"/>
    
    <mvc:annotation-driven />
    <mvc:resources mapping="/static/**" location="/static/"/>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    bean>

    
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600"/>
        <property name="defaultEncoding" value="UTF-8" />
    bean>

beans>
3、写测试的Controller
package com.springmvc.controller;

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

/**
 * @author LCX
 * @date 2020/7/1
 */

@Controller
public class InputProductController {
    @RequestMapping("/test")
    public void test1(){
        System.out.println("Hello Word!");
    }
}

  • 最后运行结果
    基于maven配置最简SpringMVC_第2张图片
    我们来看看控制台,发现控制台以及打印出来了。所以 SpringMVC配置成功
    基于maven配置最简SpringMVC_第3张图片
    项目demo下载点击这里

你可能感兴趣的:(SpringMVC)