SpringBoot集成Jsp出错(404)

刚学习SpringBoot,第一步确定是搭建一个helloWorld的程序.git

在集成SpringMVC的过程,发现一个异常诡异的问题.写了2个controller.github

@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路径)仍然无效.web

一直搜索关键字"SpringBoot Jsp 404",没法得到突破. 后面偶然发现一个网友的启动步骤是spring

mvn:spring-boot:run. 尝试一下终于成功.apache

老是得到突破,修改关键字"mvn spring-boot:run main() 区别"找到如下这个文章,segmentfault

http://www.javashuo.com/article/p-fevzxhth-dd.html .里面提供的解决方法"(去掉将pom.xml中tomcat-embed-jasper依赖<scope>provided</scope>)"在我这里仍不奇效. 转战StackOverflow,tomcat

搜索新的关键字,找到这个文章app

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."webapp

相关答案帖子里的1L,2L说的很详细了. IntelliJ IDEA没有将<scope>provided </scope>的依赖注入到类路径中,用main()方法启动的话,pom.xml里添加的这个jsp

<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!

相关文章
相关标签/搜索