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

SSM 配置 index 页面的实现方式

一、Servlet 容器默认欢迎页

配置pom文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qcby</groupId> <artifactId>springmvcIndexOne</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring.version>5.3.20</spring.version> </properties> <!-- 仅需引入Servlet和SpringMVC基础依赖(保证项目能运行) --> <dependencies> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- SpringMVC核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>springmvcIndexOne</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.4.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.3.0</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>3.1.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.2</version> </plugin> </plugins> </pluginManagement> </build> </project>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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"> <display-name>Archetype Created Web Application</display-name> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc.xml配置文件,配置的是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> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置过滤器,解决中文乱码的问题 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 指定字符集 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置spring创建容器时要扫描的包 --> <context:component-scan base-package="com.qcby"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置spring开启注解mvc的支持 <mvc:annotation-driven></mvc:annotation-driven>--> <mvc:annotation-driven></mvc:annotation-driven> </beans>

配置完成Tomcat容器,直接启动项目。

二、使用welcome-file-list进行默认跳转页面设置

pom文件同上

springmvc.xml文件同上

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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"> <display-name>Archetype Created Web Application</display-name> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc.xml配置文件,配置的是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> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <!-- 优先级从上到下:先找pages/home.jsp,找不到再找index.jsp --> <welcome-file>pages/home.jsp</welcome-file> <welcome-file>home.jsp</welcome-file> </welcome-file-list> </web-app>

配置完成Tomcat容器,直接启动项目。

三、springmvc配置文件中mvc:view-controller设置

pom文件同上

web.xml配置文件同一

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置spring创建容器时要扫描的包 --> <context:component-scan base-package="com.qcby"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:view-controller path="/" view-name="home"/> <!-- 配置spring开启注解mvc的支持 <mvc:annotation-driven></mvc:annotation-driven>--> <mvc:annotation-driven></mvc:annotation-driven> </beans>

配置完成Tomcat容器,直接启动项目。
注意:需要将项目创建时默认加载的index.jsp删除

四、使用controller配置

pom文件同上

web.xml配置文件同一

springmvc.xml配置文件同一

设置controller接口

@Controller public class HelloController { @RequestMapping({"/", ""}) public String home(Model model){ model.addAttribute("username", "测试用户"); model.addAttribute("message", "这是Controller处理后的首页数据"); // 返回视图名,结合视图解析器指向 /WEB-INF/pages/index.jsp return "home"; } }

配置完成Tomcat容器,直接启动项目。

http://www.jsqmd.com/news/300032/

相关文章:

  • 三步搭建个人B站资源管理中心:DownKyi高效下载与系统化管理指南
  • 颠覆式英雄联盟智能助手:League Akari重构你的游戏体验
  • 突破B站视频下载三大瓶颈:DownKyi全方位解决方案
  • 探索League Akari:5个让你游戏体验升维的智能解决方案
  • YOLOE性能实测报告:LVIS数据集上提升3.5 AP真相
  • 3个技巧掌握云存储提速工具:文件解析技术助力创作者高速下载方案
  • 3款神器终结下载限速:从入门到精通的文件下载提速指南
  • 亲测推荐!科哥开发的lama修复工具让修图变得超简单
  • League Akari:英雄联盟智能辅助工具全解析
  • HsMod炉石传说插件完全使用手册:提升游戏体验的全方位指南
  • 3个终极技巧让你突破网盘限速
  • 科哥OCR镜像支持Ctrl多选上传,批量操作更高效
  • FSMN VAD RTF指标解读:0.030实时率的实际意义
  • 2026年RL+大模型趋势入门必看:verl开源部署实战
  • 7步精通虚拟设备驱动:Windows游戏控制多设备模拟解决方案
  • 颠覆认知:视频下载效率提升的终极指南——B站8K超清下载全攻略
  • Glyph上手不难!只需三步完成视觉推理任务
  • 树莓派开机黑屏没反应?用这个镜像让脚本可见可查
  • 三极管开关电路解析:高频工作状态监测指南
  • 基于深度学习YOLOv8的超市商品识别检测系统(YOLOv8+YOLO数据集+UI界面+Python项目源码+模型)
  • 吐血推荐!研究生必用AI论文软件TOP8:开题文献综述全测评
  • 基于深度学习YOLOv8的车辆行人检测系统(YOLOv8+YOLO数据集+UI界面+Python项目源码+模型)
  • 虚拟设备驱动解锁游戏控制新姿势:从问题到实践的完整指南
  • 如何简单管理空洞骑士模组:Scarab从入门到精通指南
  • 颠覆式效率提升:GHelper如何重构华硕笔记本性能控制体验
  • Emotion2Vec+ Large实战对比:帧级vs整句粒度情感分析性能评测
  • 5个维度重构B站视频收藏体系:DownKyi全功能深度解析
  • ALU控制信号解析:手把手教你理解功能选择机制
  • 小白友好型教程:YOLO11目标检测从0到1
  • 《把脉行业与技术趋势》-105-霍金以非凡智慧揭示:无机械动力的AI只是“缸中之脑”;真智能必具身——能感知物理世界、施加因果力、在现实中留下不可磨灭的行动印记。