当前位置: 首页 > article >正文

Spring MVC 入门案例:开启 Web 开发之旅

在 Web 开发领域,Spring MVC 是一个强大且广泛使用的框架。今天,我们就通过一个简单的入门案例,来一窥 Spring MVC 的神奇之处,帮助大家迈出掌握这个优秀框架的第一步。

一、前期准备

开发环境

我们需要以下工具和环境:

  • JDK:确保已经安装了合适版本的 JDK,本案例使用 JDK 8 及以上版本。
  • IDE:这里推荐使用 IntelliJ IDEA 或者 Eclipse,它们都能很好地支持 Java Web 开发。
  • Maven:用于项目的依赖管理和构建。

创建 Maven 项目

打开 IDE,创建一个新的 Maven 项目。在创建过程中,需要填写项目的基本信息,如groupIdartifactIdversion。这里我们以一个简单的示例为例,groupIdcom.exampleartifactIdspringmvc - demoversion1.0 - SNAPSHOT

添加 Spring MVC 依赖

在项目的pom.xml文件中,添加 Spring MVC 的核心依赖:

<dependencies>
    <!-- Spring MVC 依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring - webmvc</artifactId>
        <version>5.3.23(你可以根据需要更新版本)</version>
    </dependency>
    <!-- Servlet API 依赖 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet - api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

二、配置 Spring MVC

创建 Spring 配置文件

src/main/resources目录下,创建spring - mvc.xml配置文件。这个文件是 Spring MVC 的核心配置文件,用于配置各种组件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2010/XMLSchema - in - xmlSchema - Instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring - beans - xsd.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring - mvc - xsd.xsd">

    <!-- 启用 Spring MVC 注解驱动,自动注册各种必要的组件 -->
    <mvc:annotation - driven />

    <!-- 配置视图解析器,这里我们使用 InternalResourceViewResolver 来解析 JSP 视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB - INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

配置 web.xml(传统方式,若使用 Servlet 3.0 + 可使用注解配置)

src/main/webapp/WEB - INF目录下,创建web.xml文件,用于配置 Servlet 和相关的初始化参数。

<?xml version="1.0" encoding="UTF-8"?>
<web - app xmlns:xsi="http://www.w3.org/2010/XMLSchema - in - xmlSchema - Instance"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web - app_3_0.xsd"
           version="3.0">

    <!-- 配置 Spring MVC 的 DispatcherServlet -->
    <servlet>
        <servlet - name>dispatcherServlet</servlet - name>
        <servlet - class>org.springframework.web.servlet.DispatcherServlet</servlet - class>
        <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>dispatcherServlet</servlet - name>
        <url - pattern>/</url - pattern>
    </servlet - mapping>
</web - app>

三、创建控制器(Controller)

src/main/java目录下,创建com.example.controller包,并在其中创建一个简单的控制器类HelloController

package com.example.controller;

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

@Controller
public class HelloController {

    // 使用 @RequestMapping 注解来映射 HTTP 请求路径和方法
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String sayHello() {
        return "hello"; // 返回一个视图名,这个视图名会由视图解析器解析
    }
}

四、创建视图(View)

src/main/webapp/WEB - INF/views目录下,创建hello.jsp文件。这个文件就是我们的视图,用于向用户展示内容。

<%@ page contentType="text/html;charset=UTF - 8" language="java" %>
<html>
<head>
    <title>Hello Spring MVC</title>
</head>
<body>
    <h1>Hello, World! This is Spring MVC in action.</h1>
</body>
</html>

五、运行项目

将项目部署到一个支持 Servlet 的 Web 服务器上,比如 Tomcat。启动服务器后,在浏览器中输入http://localhost:(你的 Tomcat 端口)/springmvc - demo/hello,你就会看到hello.jsp页面的内容显示出来。

通过这个简单的 Spring MVC 入门案例,我们创建了一个基本的 Web 应用程序。从配置 Spring MVC 环境,到创建控制器处理请求,再到通过视图展示内容,希望你已经对 Spring MVC 的工作流程有了初步的理解。在后续的学习中,你可以进一步探索 Spring MVC 的更多高级特性,如数据绑定、表单处理、拦截器等,让你的 Web 开发之旅更加精彩。


http://www.kler.cn/a/382093.html

相关文章:

  • http-server:Node.js下的轻量级静态服务器工具
  • SpringBoot配置Rabbit中的MessageConverter对象
  • 【spark面试题】RDD和DataFrame以及DataSet有什么异同
  • 链表-单链表
  • [MySQL]DCL语句
  • 【UML】- 用例图(结合银行案例解释其中的奥义)
  • 蓝桥杯专项---一维前缀/差分巩固题目
  • 【5.9】指针算法-双指针解验证回文字符串 Ⅱ
  • PostgreSQL 学习笔记:PostgreSQL 主从复制
  • 【自用】fastapi教程第三节--响应与响应体
  • 智能化在线考试及数据可视化系统
  • C++ 之类和对象
  • 集智书童 | UniMatch V2 推进半监督语义分割极限,以更低训练成本实现更优的语义分割结果-建议收藏!
  • 【网络】数据链路层
  • 基于Qt的独立线程创建与多线程执行实验Demo
  • JAVA读取doc,docx转PDF通过vue展示
  • 基于Multisim拔河比赛游戏+计分电路(含仿真和报告)
  • 华为 HarmonyOS NEXT 原生应用开发:【封装正则API】在原生鸿蒙中使用正则表达式校验登录注册模块(邮箱、密码、手机号)校验
  • 微积分复习笔记 Calculus Volume 1 - 4.7 Applied Optimization Problems
  • WordPress 中最佳的维护服务:入门级用户指南