SpringMVC---配置与使用的示例
#代码知识 发布时间: 2026-01-12
SpringMVC是Spring的一个组件,所以我们在使用SpringMVC的时候也会使用到Spring

使用环境
- JDK:1.8
- Tomcat:9.0.3
- spring:5.2.8
- 编译器:IDEA2019
1、导包
需要引入Spring-web和Spring-webmvc两个包,可以到maven仓库里面去下载或者使用maven依赖
2、ApplicationContext.xml配置(Spring的核心配置文件)
- ApplicationContext.xml文件需要放在WEB-INF下,并且需要把名字改为拦截的serlvet-name+ -Servlet,比如我这边的拦截名字为mvc,所以我需要把配置文件名改为mvc-Servlet.xml
- 如果不放在WEB-INF下,需要在web.xml文件中进行路径配置(如下web.xml文件中的init-param标签配置)
- 注意命名空间的问题
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启spring注解驱动--> <context:component-scan base-package="com.cjh"/> <!-- 开启mvc注解驱动--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
3、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 说明Spring核心配置文件的位置--> <param-value>classpath:ApplicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
4、java的实现
Controller类
@Controller
@RequestMapping("userController.do")
public class UserController {
public UserController(){
System.out.println("controller创建了");
}
@RequestMapping
public void test(){
System.out.println("controller:test方法执行了");
}
}
index.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>cai jin hong</title> <style> </style></head> <body> <a href="userController.do" rel="external nofollow" >测试</a> </body> </html>
请求和响应流程:
- 当点击测试超链接时,浏览器向服务器发送userController.do的资源请求
- 服务器接收到之后,找到类上面带有@RequestMapping("userController.do")注解的对象
- 找到了之后,查找方法上面带有@RequestMapping("xxx")注解的方法
如果只有一个方法,可以不用写名字,直接写RequestMapping
如果有多个方法,需要注明方法名
- 找到了之后,执行方法,并将处理信息响应回给浏览器(该代码中没有返回值)
本篇文章只讲了一下最基本的时候,下一篇文章会详细的说的SpringMVC请求和响应的处理!!!
以上就是SpringMVC---配置与使用的示例的详细内容,更多关于SpringMVC---配置与使用的资料请关注其它相关文章!
代码知识SEO上一篇 : 浅析spring定时器的使用
下一篇 : python实现AdaBoost算法的示例
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!