SpringBoot設置首頁(默認頁)跳轉功能的實現方案


最近springboot開發需要設置個默認頁面,要直接跳轉到登錄頁面。

方案1:controller里添加一個"/"的映射路徑

@RequestMapping("/")
public String index(Model model, HttpServletResponse response) {
  model.addAttribute("name", "simonsfan");
  return "/index";
}

方案二:設置默認的View跳轉頁面

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    super.addViewControllers(registry);
  }
}

  

@Configuration
public class DefaultView extends WebMvcConfigurerAdapter{
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    super.addViewControllers(registry);
    //主頁
    registry.addViewController("/").setViewName("forward:/index");
  } 
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM