Springboot設(shè)置默認(rèn)訪問(wèn)路徑方法實(shí)現(xiàn)
當(dāng)使用springboot與其他框架結(jié)合編寫(xiě)web前后端時(shí),可能存在這樣的需求:我想在訪問(wèn)10.10.10.100時(shí),實(shí)際上需要訪問(wèn)10.10.10.100/hello頁(yè)面。(端口已省略,自行設(shè)置)
解決方案1 - 實(shí)現(xiàn)WebMvcConfigurer接口搜過(guò)很多博客,里面的內(nèi)容雖然可以用。但是基本上都是基于繼承WebMvcConfigurerAdapter類實(shí)現(xiàn)的,而官方的源碼里面已經(jīng)不推薦使用該類了。
下面給出我的解決方案,很簡(jiǎn)單:
import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class DefaultView implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController('/').setViewName('hello'); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); }}
補(bǔ)充&注意點(diǎn)
setViewName :將 / 指向 /hello
這里需要注意,因?yàn)槲业捻?xiàng)目里把url的訪問(wèn)路徑后綴'.html'全部都去掉了,所以可以這么用。如果你的不是,需要做對(duì)應(yīng)調(diào)整。
補(bǔ)充我的application.properties文件部分配置:
server.port=80 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.mvc.favicon.enabled=true
@Configuration 別忘了加,我自己就是忘了加,以為沒(méi)生效,折騰半天。
方案2 - @Controller路由設(shè)置@Controllerpublic class PageController { @GetMapping(value = '/') public String defaultPath(Model model) { return 'hello'; } }
properties文件配置同方案1一致
到此這篇關(guān)于Springboot設(shè)置默認(rèn)訪問(wèn)路徑方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot 默認(rèn)訪問(wèn)路徑內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML入門(mén)精解之結(jié)構(gòu)與語(yǔ)法2. HTML DOM setInterval和clearInterval方法案例詳解3. CSS3中Transition屬性詳解以及示例分享4. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. 輕松學(xué)習(xí)XML教程7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. UDDI FAQs9. 使用純HTML的通用數(shù)據(jù)管理和服務(wù)10. 解析原生JS getComputedStyle
