欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > SpringMVC环境搭建

SpringMVC环境搭建

2025/2/21 3:18:22 来源:https://blog.csdn.net/m0_64637029/article/details/145604986  浏览:    关键词:SpringMVC环境搭建

文章目录

    • 1.模块创建
        • 1.创建一个webapp的maven项目
        • 2.目录结构
    • 2.代码
        • 1.HomeController.java
        • 2.home.jsp
        • 3.applicationContext.xml Spring配置文件
        • 4.spring-mvc.xml SpringMVC配置文件
        • 5.web.xml 配置中央控制器以及Spring和SpringMVC配置文件的路径
        • 6.index.jsp
    • 3.配置Tomcat
        • 1.配置路径以及热加载
        • 2.配置war包以及上下文路径为/
        • 3.启动
          • 1.首页
          • 2.访问 /home
    • 4.初始化基本流程解析

1.模块创建

1.创建一个webapp的maven项目

CleanShot 2025-02-10 at 12.21.01@2x

2.目录结构

CleanShot 2025-02-10 at 12.39.37@2x

2.代码

1.HomeController.java
package com.example.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;/*** Description:** @Author sun* @Create 2025/2/10 12:23* @Version 1.0*/
@Controller
public class HomeController {@RequestMapping("/home")public String home(Model model) {System.out.println("SpringMVC 执行了 home() 方法");model.addAttribute("message", "Hello, SpringMVC!");return "home"; // 返回 home.jsp}
}
2.home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>SpringMVC Demo</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
3.applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 扫描 Service 组件 --><context:component-scan base-package="com.example.service"/></beans>
4.spring-mvc.xml SpringMVC配置文件
<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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 启用Spring MVC注解 --><mvc:annotation-driven/><!-- 扫描 Controller 层 --><context:component-scan base-package="com.example.controller"/><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean></beans>
5.web.xml 配置中央控制器以及Spring和SpringMVC配置文件的路径
<?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"><!-- 配置Spring MVC的DispatcherServlet --><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/spring-mvc.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><!-- 配置Spring的ContextLoaderListener(用于加载根上下文) --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param>
</web-app>
6.index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

3.配置Tomcat

1.配置路径以及热加载

CleanShot 2025-02-10 at 12.43.14@2x

2.配置war包以及上下文路径为/

CleanShot 2025-02-10 at 12.43.41@2x

3.启动
1.首页

CleanShot 2025-02-10 at 12.44.26@2x

2.访问 /home

CleanShot 2025-02-10 at 12.44.53@2x

CleanShot 2025-02-10 at 12.44.40@2x

4.初始化基本流程解析

  1. Tomcat启动,读取web.xml,装载中央控制器以及获取Spring以及SpringMVC的配置文件路径
  2. 由于中央控制器配置了load-on-startup所以会调用中央控制器的init方法完成Spring以及SpringMVC容器的初始化

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com