SpringMvc的具体操作,如何配置springMvc(完整教程)

server/2024/10/5 22:27:37/

第一步:引入依赖

<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency>

第二步:配置tomcat(这个简单,一笔带过)

第三步:配置DispatcherServlet

官网:DispatcherServlet :: Spring Framework

点击官网,咱们会看到可以用配置类进行配置,也可以用xml的形式配置在tomcat的web.xml文件中

以代码形式配置如下:

java">public class MyWebApplicationInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext servletContext) {// Load Spring web application configurationAnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();context.register(AppConfig.class);// Create and register the DispatcherServletDispatcherServlet servlet = new DispatcherServlet(context);ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);registration.setLoadOnStartup(1);registration.addMapping("/app/*");}
}

以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"><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:app-context.xml</param-value></context-param><servlet><servlet-name>app</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>app</servlet-name><url-pattern>/*</url-pattern></servlet-mapping></web-app>

咱们直接用xml配置

咱们可以看到,有一个配置文件爆红,这个我们还没写,我们先写一个app-context.xml的配置文件

其实,就是spring的配置文件,以下就是一个包扫描的配置文件

<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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.example.demo.controller"/></beans>

注意还要写个app-servlet.xml,因为servlet-name是app,所以名字要搞一个app-servlet.xml,其实这个文件也是个spring的配置文件而已

<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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.example.demo.controller"/></beans>

第四步:编写controller层

package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello")public String sayHello(){return "Hello";}
}

第五步:演示结果

访问如下路径:localhost:8080/hello

第六步:其他功能

1、配置页面视图解析器

我们可以看到配置这个页面视图解析器是可以解析jsp文件的

官网关于配置视图解析器的示例:View Resolvers :: Spring Framework

咱们可以在app-servlet.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.example.demo.controller"/><mvc:view-resolvers><mvc:jsp/></mvc:view-resolvers>
</beans>

点击这个mvc:jsp进去可以看到就是我们之前展示的那个视图解析器

结果展示:

2、咱们可以配置如下功能


http://www.ppmy.cn/server/113865.html

相关文章

在前端中Proj4.js使用简单介绍

Proj4 是一个用于处理 JavaScript 里的几何图形和坐标计算的库。虽然这个库的知名度不如其他几何处理库如 Turf.js&#xff0c;但它提供了用于多边形、点、线等几何对象的计算功能&#xff0c;类似于 GIS&#xff08;地理信息系统&#xff09;的某些功能。 一、基础知识 在使…

【软件设计】常用设计模式--概述

学习设计模式是提高软件开发技能的重要步骤。下面是一些最常用的设计模式&#xff0c;以及它们的基本概念和使用场景&#xff1a; 1. 单例模式&#xff08;Singleton Pattern&#xff09; 概念: 确保一个类只有一个实例&#xff0c;并提供全局访问点。 使用场景: 适用于需要全…

Windows 11的新游戏手柄键盘现在可让玩家使用Xbox手柄打字

微软已开始测试 Windows 11 中专为 Xbox 控制器设计的全新游戏手柄键盘布局。游戏手柄键盘布局改进了 Windows 11 中的屏幕键盘&#xff0c;因此玩家可以使用 Xbox 控制器在操作系统中打字或导航。 作为 Windows 11 测试版的一部分&#xff0c;游戏手柄键盘目前正在测试中&…

大零售时代:开源 AI 智能名片、2+1 链动与 O2O 商城小程序引领融合新趋势

摘要&#xff1a;本文深入探讨了当今零售业态的发展趋势&#xff0c;指出在数据匹配的时代&#xff0c;人依然在零售中发挥着重要作用。通过对大零售理念的阐述&#xff0c;分析了跨行业跨业态融合的必然性&#xff0c;强调了业态融合的指导思想以及实现方式。同时&#xff0c;…

HarmonyOS开发实战( Beta5.0)Native Drawing自绘制能力替代Canvas提升性能

简介 Canvas 画布组件是用来显示自绘内容的组件&#xff0c;它具有保留历史绘制内容、增量绘制的特点。Canvas 有 CanvasRenderingContext2D/OffscreenCanvasRenderingContext2D 和 DrawingRenderingContext 两套API&#xff0c;应用使用两套API绘制的内容都可以在绑定的 Canv…

Java项目——苍穹外卖(一)

Entity、DTO、VO Entity&#xff08;实体&#xff09; Entity 是表示数据库表的对象&#xff0c;通常对应数据库中的一行数据。它通常包含与数据库表对应的字段&#xff0c;并可能包含一些业务逻辑。 DTO&#xff08;数据传输对象&#xff09; 作用&#xff1a;DTO 是用于在…

【docker】通过云服务器安转Docker

一、前言 这里关于Docker的安转&#xff0c;大家可以采用本地使用虚拟机来运行和安转Docker,我这里呢就采用云服务器来安装Docker,之所以用云服务器安转docker是因为比较简单&#xff0c;只是需要花一点money,而且自己没有用过云服务器所以这里就用这种方式来安转Docker了&…

SQL进阶技巧:截止当前批次前的批次量与订单量 | 移动窗口问题

目录 0 场景描述 1 数据准备 2 问题分析 3 小结 0 场景描述 表A有如下字段,user id(用户ID),batch id(批次ID),order id(订单ID),create time(创建时间),同一个用户ID下有多个批次,同一个批次下有多个订单ID,相同批次ID的创建时间是相同的,创建时间精确到了秒。 统计,截…