Spring MVC框架二:创建第一个MVC程序

news/2025/2/26 13:29:05

精心整理了最新的面试资料,有需要的可以自行获取

点击前往百度网盘获取
点击前往夸克网盘获取
有两种方式

利用配置

1、利用IDEA新建一个Maven项目,添加一个web支持

在这里插入图片描述
在这里插入图片描述
2、导入常用的依赖

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

3、配置web.xml,注册DispatcherServlet

<?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">
<!--配置DispatchServlet:Spring的核心,请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--绑定配置Spring文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
<!--启动级别:设置为1-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
<!--SpringMVC中 / 和 /*的区别
/:不会匹配jsp页面,/*会匹配jsp页面-->


</web-app>

4、编写SpringMVC的配置文件,文件名为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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
<!--后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

5、编写要操作的业务Controller,要么实现接口,要么增加注解,需要返回一个ModelAndView,装数据,封视图

java">package com.www.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //ModelAndView 模型和视图
        ModelAndView mv = new ModelAndView();
        String result = "HelloSpringMVC";
        //封装对象,放在ModelAndView中
        mv.addObject("msg",result);
        //封装要跳转的视图,放在ModelAndView中
        mv.setViewName("test");//WEB-INF/jsp/test.jsp
        return mv;
    }
}

6、将类交给SpringIOC容器,注册bean

<!--BeanNameUrlHandlerMapping:bean-->
    <bean id="/hello" class="com.www.controller.HelloController"></bean>
</beans>

7、写要跳转的jsp页面,显示ModelAndView存放的数据

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

8、配置Tomcat启动测试
在这里插入图片描述

可能遇到的问题:访问出现404,排查步骤:

1.查看控制台输出,看一下是不是缺少了什么jar包。
2.如果jar包存在,显示无法输出,就在IDEA的项目发布中,添加lib依赖!
在这里插入图片描述
在这里插入图片描述

3.重启Tomcat 即可解决!

利用注解

1、新建一个Maven项目,添加web支持,配置好依赖,子项目的话就不用了
2、由于Maven可能存在资源过滤的问题,所以在之前的xml文件中添加如下内容

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

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-->
    <!--配置DispatchServlet:Spring的核心,请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--绑定配置Spring文件,进行关联-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--        启动级别:设置为1,数字越小,启动越早-->
        <load-on-startup>1</load-on-startup>
    </servlet>
<!--    所有的请求都会被springmvc拦截-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--SpringMVC中 / 和 /*的区别
    /:不会匹配jsp页面,/*会匹配jsp页面-->
</web-app>

4、添加Spring MVC配置文件
在resource目录下添加springmvc-servlet.xml配置文件,配置的形式与Spring容器配置基本类似,为了支持基于注解的IOC,设置了自动扫描包的功能

<?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.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">
    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.www.controller"/>
    <!-- 让Spring MVC不处理静态资源 -->
    <mvc:default-servlet-handler />
    <!--   支持mvc注解驱动
    在spring中一般采用@RequestMapping注解来完成映射关系
    要想使@RequestMapping注解生效
    必须向上下文中注册DefaultAnnotationHandlerMapping
    和一个AnnotationMethodHandlerAdapter实例
    这两个实例分别在类级别和方法级别处理。
    而annotation-driven配置帮助我们自动完成上述两个实例的注入。
    -->
    <mvc:annotation-driven />
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

在视图解析器中我们把所有的视图都存放在/WEB-INF/目录下,这样可以保证视图安全,因为这个目录下的文件,客户端不能直接访问

5、创建一个控制类

java">package com.www.controller;

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

@Controller
public class HelloController {
    @RequestMapping("/h1")
    public String hello(Model model){
        model.addAttribute("msg","Hello,SpringMVC6666666");
        return "hello";
    }
}

结语

创作不易,希望能对大家给予帮助

想要获取更多资源?

点击链接获取


http://www.niftyadmin.cn/n/5868766.html

相关文章

鸿蒙ArkTs如何实现pdf预览功能?

鸿蒙ArkTs如何实现pdf预览功能&#xff1f; 前言PDFKit运行示例代码报错真机运行先看效果一、预览本地pdf文件二、预览线上的pdf文件三、预览沙箱目录中pdf的文件(重点)效果中的整体代码总结 Harmony OS NEXT版本&#xff08;接口及解决方案兼容API12版本或以上版本) 前言 在开…

Project Reactor中 map、flatMap、concatMap 和 flatMapSequential 的区别

在 Project Reactor&#xff08;Reactive Streams 的实现库&#xff0c;常用于 Spring WebFlux&#xff09;中&#xff0c;map、flatMap、concatMap 和 flatMapSequential 是常用的操作符&#xff0c;但它们的功能和行为有显著区别。以下是它们的详细对比&#xff1a; 1. 功能对…

SOME/IP-SD -- 协议英文原文讲解2

前言 SOME/IP协议越来越多的用于汽车电子行业中&#xff0c;关于协议详细完全的中文资料却没有&#xff0c;所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块&#xff1a; 1. SOME/IP协议讲解 2. SOME/IP-SD协议讲解 3. python/C举例调试讲解 5.1.2.2 S…

Para-Lane: 首个真实世界多车道数据集,目的评估自动驾驶系统中的新型视角合成能力。

2025-02-22&#xff0c;阿里巴巴集团菜鸟自动驾驶实验室和百度研究院共同创建了一个名为 Para-Lane 的真实世界多车道数据集。该数据集目的评估自动驾驶系统中的新型视角合成&#xff08;NVS&#xff09;能力&#xff0c;通过提供大量真实世界的数据&#xff0c;弥补了现有合成…

【大模型】Ubuntu下 fastgpt 的部署和使用

前言 本次安装的版本为 fastgpt:v4.8.8-fix2。 最新版本fastgpt:v4.8.20-fix2 问答时报错&#xff0c;本着跑通先使用起来&#xff0c;就没有死磕下去&#xff0c;后面bug解了再进行记录。   github连接&#xff1a;https://github.com/labring/FastGPT fastgpt 安装说明&…

jdk21下载、安装(Windows、Linux、macOS)

Windows 系统 1. 下载安装 访问 Oracle 官方 JDK 下载页面 或 OpenJDK 下载页面&#xff0c;根据自己的系统选择合适的 Windows 版本进行下载&#xff08;通常选择 .msi 安装包&#xff09;。 2. 配置环境变量 右键点击 “此电脑”&#xff0c;选择 “属性”。 在左侧导航栏…

vLLM专题(十四)-自动前缀缓存

一、介绍 自动前缀缓存(Automatic Prefix Caching,简称 APC)缓存现有查询的 KV 缓存,以便新查询如果与现有查询共享相同的前缀,可以直接重用 KV 缓存,从而跳过共享部分的计算。 注意 有关 vLLM 如何实现 APC 的技术细节,请参阅此处。 二、在 vLLM 中启用 APC 在 vLLM …

防火墙双机热备---VRRP,VGMP,HRP(超详细)

双机热备技术-----VRRP&#xff0c;VGMP&#xff0c;HRP三个组成 注&#xff1a;与路由器VRRP有所不同&#xff0c;路由器是通过控制开销值控制数据包流通方向 防火墙双机热备&#xff1a; 1.主备备份模式 双机热备最大的特点就是防火墙提供了一条专门的备份通道&#xff08;心…