软件编程
位置:首页>> 软件编程>> java编程>> 简单的一次springMVC路由跳转实现

简单的一次springMVC路由跳转实现

作者:上升的蜗牛  发布时间:2023-01-09 10:05:11 

标签:springMVC,路由跳转

实现目标:使用springMVC前端控制器,跳转到WEB-INF的templates下面的前端页面

图示

简单的一次springMVC路由跳转实现

1.目录结构

简单的一次springMVC路由跳转实现

2.创建一个maven的webapp项目,创建好之后记得把index.jsp文件删除,否i则会首先跳到这个文件,我们要用前端控制器转发所有请求(如果有大佬知道怎么让他存在,又不影响,希望可以学习一下)

简单的一次springMVC路由跳转实现

3.在xml里面,配置springMVC前端控制器,

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>

<!--  配置springMVC的前端控制器,对浏览器发送的请求进行统一处理-->
 <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!--  配置SpringMVC配置文件的位置和名称-->
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springMVC.xml</param-value>
   </init-param>
   <!--将前端控制器DispatcherServlet的初始化时间提前到服务器启动时-->
   <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <!--匹配除了.jsp请求路径的请求-->
   <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

4.创建并配置springMVC.xml,记得配置一下context(开启扫描需要)

<?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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    扫描组件-->
   <context:component-scan base-package="com.atguigu.mvc.controller"></context:component-scan>
   <!-- 配置Thymeleaf视图解析器 -->
   <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
       <!--设置视图解析器优先级,可以设置多个-->
       <property name="order" value="1"/>
       <property name="characterEncoding" value="UTF-8"/>
       <property name="templateEngine">
           <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
               <property name="templateResolver">
                   <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

<!-- 视图前缀 -->
                       <property name="prefix" value="/WEB-INF/templates/"/>

<!-- 视图后缀 -->
                       <property name="suffix" value=".html"/>
                       <property name="templateMode" value="HTML5"/>
                       <property name="characterEncoding" value="UTF-8" />
                   </bean>
               </property>
           </bean>
       </property>
   </bean>
</beans>

5.匹配路径

@Controller
public class HelloController {
   //"/" -->/web-inf/templates/index.html
   //RequestMapping请求映射
   //value可不写
   @RequestMapping(value="/")
   public String tindex(){
       //返回视图名称,因为我们在视图解析器里面,配置了后缀,所以这里不用写了
       return "index";
   }

@RequestMapping(value="/target")
   public String toTarget(){
       return "target";
   }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>/</title>
</head>
<body>
<!--/浏览器从哪个localhost:8080开始-->
<!--th:被thymeleaf解析,可从localhost:8080:项目名字开始-->
<!--@{}检测到绝对路径,自动添加上下文路径-->
<a th:href="@{/target}" rel="external nofollow" >访问目标</a>
ahhahahahah
</body>
</html>

target.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
HELLOWORLD
</body>
</html>

来源:https://blog.csdn.net/qq_51014805/article/details/124137012

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com