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 @ 2022-06-29 20:37  ShineLe  阅读(874)  评论(0)    收藏  举报