WebMvcConfigure:addViewController方法

重写WebMvcConfigurer接口实现addViewControllers页面跳转(学习笔记)_Jerry_cheese的博客-CSDN博客_addviewcontrolle

使用addviewController()实现无业务逻辑跳转 - 走看看

WebMvcConfigurer讲解_老爸是程序员的博客-CSDN博客

addViewController的使用_JerryWu2018的博客-CSDN博客

 重写WebMvcConfigurer接口实现addViewControllers页面跳转(学习笔记)_Jerry_cheese的博客

方法

public void addViewControllers( ViewControllerRegistry registry ).setViewName(String pageName)

参数

  • registry:Controller中的URL,格式为"/neturl",会自动拼成 http://localhost/neturl 的形式
  • pageName:return的页面文件,格式为 "dir/pagename"(不用写后缀),会自动返回dir目录下的pagename.html

说明

这是一个覆写方法,作用是将一个请求转到某个页面无业务逻辑,无业务逻辑的含义是我们不需要为页面跳转写额外的逻辑代码。

依赖

复制代码
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
复制代码

又是可能因为忘记引入thymeleaf导致识别不出资源路径,且不会报错。

例子

复制代码
@Configuration
public  class WebConfig implements WebMvcConfigurer{

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("test").setViewName("emp/test");
        
    }
    
}
复制代码

上文的含义是,在访问http://localhost/test时,会直接返回test.html网页。

正常Spring下的页面跳转的业务逻辑代码为:

复制代码
@Controller
public class EmpController {
    
    @RequestMapping("test")
    public String test() {
        
        return "emp/test";
    }
}
复制代码

 

posted @   ShineLe  阅读(850)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
历史上的今天:
2021-06-29 Qt:QWebChannel
2021-06-29 Qt:QWebEngineView
2021-06-29 IntelliJ:JUnit单元测试
点击右上角即可分享
微信分享提示