【spring boot】5.spring boot 创建web项目并使用jsp作前台页面【idea新创建spring boot服务,pom完善即可启动】
贼烦的是,使用 spring boot 创建 web 项目,然后我再 idea 下创建的,but 仅仅启动 spring boot 的启动类,就算整个项目都是好着的,就算是能够进入 controller 中,也不能成功访问到 jsp 页面。很尴尬!也很无语!
好吧,开头一句抱怨,下面全靠编 ~~~
=======================================================================================================
1.idea 下 spring boot 创建 web 项目
刚创建完是下面这样
当然,最后创建完的使用 JSP 的 web 项目可不是这个结构的。往下看。
2.pom.xml 添加下面
<!--web 支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency><span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)">jsp页面使用jstl标签</span><span style="color: rgba(0, 128, 0, 1)">--></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>javax.servlet<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>jstl<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)">用于编译jsp</span><span style="color: rgba(0, 128, 0, 1)">--></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span>org.apache.tomcat.embed<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span>tomcat-embed-jasper<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span>provided<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
上面这三个是 spring boot 项目创建使用 jsp 页面的 web 项目必须的三个依赖
这里介绍下 maven 中 scope 依赖范围的概念,因为后续涉及到这个会有问题。
依赖范围就是用来控制依赖和三种 classpath(编译 classpath,测试 classpath、运行 classpath) 的关系,Maven 有如下几种依赖范围:
- compile:编译依赖范围。如果没有指定,就会默认使用该依赖范围。使用此依赖范围的 Maven 依赖,对于编译、测试、运行三种 classpath 都有效。典型的例子是 spring-code, 在编译、测试和运行的时候都需要使用该依赖。
- test: 测试依赖范围。使用次依赖范围的 Maven 依赖,只对于测试 classpath 有效,在编译主代码或者运行项目的使用时将无法使用此依赖。典型的例子是 Jnuit, 它只有在编译测试代码及运行测试的时候才需要。
- provided:已提供依赖范围。使用此依赖范围的 Maven 依赖,对于编译和测试 classpath 有效,但在运行时候无效。典型的例子是 servlet-api, 编译和测试项目的时候需要该依赖,但在运行项目的时候,由于容器以及提供,就不需要 Maven 重复地引入一遍。
3. 创建 webapp 目录,并创建 jsp 页面在目录下
注意创建的目录层级关系
如果再创建 jsp 页面的时候,不能右键直接创建的话,可以 http://www.cnblogs.com/sxdcgaq8080/p/7676294.html
4.application.properties 配置文件的完善
注意前缀 的值是 /WEB-INF/views/ 而不是 /WEB-INF/views
少一个 / 整个访问的路径就是不完整的。
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
因为 webapp 在刚刚上面创建 jsp 页面的时候,已经被指定为 classpath 了,也就是根路径了。
所以现在,这里配置了 mvc 的【view】视图层前缀后缀,指定 controller 中寻找页面就去 /WEB-INF/views/ 下面去找.jsp 文件了。
5. 写一个跳转页面的 controller 层
6. 所以,最终项目的目录结构是这个样子的
哦哦,这里有一个 web 目录,是可以删掉的,不过删不删也无所谓,没有多大的关系
7. 最后一步,启动并且能访问
这里不是通过启动类去启动,而是需要通过 spring-boot:run 去启动
右键 run 或者 debug 都可以
然后 启动起来之后在浏览器访问
是可以访问到的。
8. 还有就是,如果把用于编译 jsp 的 这个插件的 scope 注解,也就是使用默认的 scope
<!--用于编译 jsp--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!--<scope>provided</scope>--> </dependency>
然后 import 整个 pom.xml 文件之后。
再通过 启动 启动类的方式来启动 spring boot 项目的话,也是可以访问的到的【我把 web 目录删掉了】
通过启动 spring-boot:run 的方式也可以访问到。
=====================================================================================================
因为确实碰到很多小的点注意不到的地方,导致整个就是访问不到,反正上面该罗列的都罗列出来了。