记忆系列–Spring Boot之addviewController
本文最后更新于 2539 天前,其中的信息可能已经有所发展或是发生改变。

今天又是没找到工作的一天呀~


今天要记录的addviewController,我觉得挺有意义的吧~

因为在项目开发过程中,时常会涉及页面跳转问题,而这个页面跳转不需要或者没有任何业务逻辑处理的过程,只是单纯的路由跳转过程或者是点击一个按钮跳转到另一个页面。

例如下面例子:

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }

但是呢,你不可能每个Controller都写这样的代码去跳转吧~这样的话不就很多类似的代码咩?

所以Spring Boot(spring mvc)中的提供了:

①implements WebMvcConfigurer(官方推荐)

②extends WebMvcConfigurationSupport

ps:有一些比较早出的书籍会说是继承WebMvcConfigurerAdapter,但是在spring 5或者spring boot 2.0之后已经给上面两种方法取代。

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
  //省略
}

@Configuration
public class MyWebMvcConfig extends WebMvcConfigurationSupport {
  //省略
}

如果用idea ,直接快捷键alt+insert,出现有 Override methods(快捷键为:ctrl+o)/implement methods(快捷键为:ctrl+i)就有所有方法了~


下面方法同等第一个例子~

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/hello").setViewName("/hello");
        //可以添加更多
    }
}

Image
Image

 

评论

  1. Image
    hamburger
    Android Chrome 62.0.3202.84
    5 年前
    2021-7-01 14:40:28

    可是这样设置之后我是只能在浏览器手动输入的时候跳转,在html页面写这个/hello跳转失败 会出错

    • Image
      博主
      hamburger
      Windows Edge 91.0.864.64
      5 年前
      2021-7-03 18:09:05

      可能太久了~现在我已经不咋用后台判断了

  2. Image
    吲哚乙酸
    Windows Edge 99.0.1150.39
    4 年前
    2022-3-18 18:54:56

    谢谢答主

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇