[英]Set the welcome page for Spring MVC 4 with Java configuration
我試圖在Spring MVC 4中從XML遷移到完全基於 java 類的配置。 到目前為止我所做的是創建一個簡單的WebAppInitializer類和一個WebConfig類。
但是,我找不到配置我的歡迎頁面的方法,這里是我舊的Web.xml
的摘錄:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
任何幫助,將不勝感激。
你不需要做任何事情,spring會自動在src/main/webapp
下查找index.html
文件,你需要做的就是創建一個index.html
文件並把它放在這個root下面。
您可以通過覆蓋WebMvcConfigurerAdapter
類的addViewControllers
方法來完成此操作。
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.myapp.controllers" })
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
}
請參閱我的回答以獲取更多信息
使用此配置,您可以將任何文件名設置為歡迎/主頁。
在根控制器中,您可以將路徑重定向到要顯示為welcomefile的路徑,
@Controller
public class WelcomePageController {
@RequestMapping("/")
public String redirectPage() {
return "redirect:Welcome";
}
@RequestMapping("/Welcome")
public String showHomePage() {
return "index";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.