基于maven配置最简SpringMVC
程序员文章站
2024-02-10 14:59:16
一、我的目录结构二、maven的相关spring依赖 org.springframework spring-beans 4.3.18.RELEASE&....
一、我的目录结构
二、maven的相关spring依赖
<dependencies>
<!--添加springMVC的支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
二、配置Spring的DispatcherServlet
1、什么是DispatcherServlet?
- DispatcherServlet实质也是一个HttpServlet,DispatcherSevlet负责将请求分发,所有的请求都有经过它来 统一分发到我们自己定义的Controller的方法中。
2、配置方法
- 在WEB-INF目录下的web.xml文件中配置
<!--配置spring相关-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<!--配置DispatcherServlet-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化自定义springmvc配置文件地址,我当前是放在resource文件目录下-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 指的是所有URL都会映射到这个servlet -->
<url-pattern>/</url-pattern>
</servlet-mapping>
- 配置spring-mvc.xml文件,主要是要进行组件扫描,并使用注解,所以 <context:component-scan /> 和 <mvc:annotation-driven /> 两个标签很重要,具体可以自行百度。到这一步基本上就配置完成,我们去Controller中写一个方法测试一下
<?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: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-->
<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!");
}
}
- 最后运行结果
我们来看看控制台,发现控制台以及打印出来了。所以 SpringMVC配置成功
项目demo下载点击这里
本文地址:https://blog.csdn.net/u013831263/article/details/107080554
上一篇: 分享几个微博营销好方法