温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springMVC路由跳转怎么实现

发布时间:2022-04-15 09:10:22 来源:亿速云 阅读:221 作者:iii 栏目:开发技术

本篇内容介绍了“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>

“springMVC路由跳转怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI