配置web工程

第一步安装tomcat

配置环境变量

运行结果

在idea中创建网络工程

目录结构完善

导入maven依赖

第一步,配置dispatcherservlet

DispatcherServlet 是一个 Servlet 类,它负责接收并处理 HTTP 请求。为了让 Web 应用程序能够识别并正确地处理这些请求,我们需要将 DispatcherServlet 注册到 web.xml 文件中,并为其配置一个请求映射路径。例如,以下配置:

xml 复制代码
xmlCopy Code
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

在这个例子中:

  • <servlet> 标签定义了 DispatcherServlet,指定了其类路径和初始化参数(例如 Spring 配置文件的位置)。
  • <servlet-mapping> 标签将 DispatcherServlet 映射到 /* 路径,这意味着所有请求都会交由 DispatcherServlet 处理。

第二步,创建处理器

kotlin 复制代码
package com.xyu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//控制器类的方法会通过注解如@RequestMapping(或其他更具体的注解,如@GetMapping, @PostMapping等)与特定的请求URL进行绑定,从而处理HTTP的响应
@Controller
//标记该类是mvc的控制器
public class my_controller {
    //创建一个处理器
    @RequestMapping("hello")
    
    @ResponseBody
    public String login() {
        return "hello";
    }
// @RequestMapping("/hello") 表示当请求 URL 为 /hello 时,调用 hello 方法进行处理。
// @ResponseBody 注解表示方法的返回值直接作为响应体返回给客户端,而不是去解析为视图。
}

控制器类的方法会通过注解如@RequestMapping(或其他更具体的注解,如@GetMapping, @PostMapping等)与特定的请求URL进行绑定,从而处理HTTP响应

第三步 创建springmvc.xml配置文件
ini 复制代码
<?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
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <context:component-scan base-package="com.xyu.controller"></context:component-scan>
</beans>

第四步,在spring.xml中开启注解开发

<context:component-scan base-package="com.xyu.controller"></context:component-scan>

第五步,创建视图文件及配置视图解析器

  1. prefix : 设置视图名称的前缀,Spring 会将返回的视图名称与此前缀拼接,查找实际的视图文件。例如,如果返回视图名称是 hello,Spring 会在 /WEB-INF/views/hello.jsp 路径下查找对应的 JSP 文件。
  2. suffix : 设置视图名称的后缀,通常是 .jsp,这样就可以让 Spring 自动为视图名称添加后缀。例如,视图名称 hello 会被解析为 /WEB-INF/views/hello.jsp

配置web.xml文件

xml 复制代码
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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的DispatcherServlet -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置Spring容器的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup> <!-- 确保在服务器启动时加载 -->
    </servlet>

    <!-- 配置DispatcherServlet的映射 -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern> <!-- 让Spring MVC处理所有请求 -->
    </servlet-mapping>

    <!-- 配置ContextLoaderListener(可选) -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

家人们注意一定要注意

给选对了,不然就会报错

相关推荐
橘子师兄6 分钟前
C++AI大模型接入SDK—ChatSDK封装
开发语言·c++·人工智能·后端
RFCEO7 分钟前
前端编程 课程十三、:CSS核心基础1:CSS选择器
前端·css·css基础选择器详细教程·css类选择器使用方法·css类选择器命名规范·css后代选择器·精准选中嵌套元素
@ chen14 分钟前
Spring事务 核心知识
java·后端·spring
Amumu1213831 分钟前
Vuex介绍
前端·javascript·vue.js
We་ct32 分钟前
LeetCode 54. 螺旋矩阵:两种解法吃透顺时针遍历逻辑
前端·算法·leetcode·矩阵·typescript
2601_949480061 小时前
【无标题】
开发语言·前端·javascript
css趣多多1 小时前
Vue过滤器
前端·javascript·vue.js
一点技术2 小时前
基于SpringBoot的选课调查系统
java·spring boot·后端·选课调查系统
理人综艺好会2 小时前
Web学习之用户认证
前端·学习
We་ct2 小时前
LeetCode 36. 有效的数独:Set实现哈希表最优解
前端·算法·leetcode·typescript·散列表