SpringBoot集成Jsp出錯(404) 原

剛學習SpringBoot,第一步肯定是搭建一個helloWorld的程序.

在集成SpringMVC的過程,發現一個異常詭異的問題.寫了2個controller.

@RestController
public class MyRestController {
    @RequestMapping("/")
    String home() {
        return "hello";
    }
}
@Controller
public class MyViewController {

    @GetMapping("/hello")
    public String index(Map<String, Object> model){
        model.put("time", new Date());
        return "hello";
    }
}

MyRestController正常運行, MyViewController無論如何都報404. 網上查詢答案,反覆確認已經做了各種修改(打包jar該war;引入tomcat\jsp依賴;修改webapp路徑)仍然無效.

一直搜索關鍵字"SpringBoot Jsp 404",無法獲得突破. 後面偶然發現一個網友的啓動步驟是

mvn:spring-boot:run. 嘗試一下終於成功.

總是獲得突破,修改關鍵字"mvn spring-boot:run main() 區別"找到以下這個文章,

https://segmentfault.com/a/1190000009785247 .裏面提供的解決方法"(去掉將pom.xml中tomcat-embed-jasper依賴<scope>provided</scope>)"在我這裏仍不奇效. 轉戰StackOverflow,

搜索新的關鍵字,找到這個文章

https://stackoverflow.com/questions/30237768/run-spring-boots-main-using-ide 裏面的人和我遇到同樣的問題"Seems like mvn spring-boot:run does some more magic that does not happen when running the main directly."

相關答案帖子裏的1L,2L說的很詳細了. IntelliJ IDEA沒有將<scope>provided </scope>的依賴注入到類路徑中,用main()方法啓動的話,pom.xml裏添加的這個

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>

不會被加載 .用mvn spring-boot:run指令就可以了.

另分享個今天的收穫:

https://github.com/spring-projects  發現這個神奇的github賬號,Spring官方維護的所有模塊的demo,哪裏不會點哪裏,so easy!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章