直接使用SpringMVC时配置访问jsp页面时很容易的事,但是由于Spring Boot使用内嵌的servlet容器,所以对jsp的支持不是很好,而且也不建议使用jsp,但是为了满足这种返回jsp页面的需求同时又想享受Spring Boot免去各种配置的麻烦,经本屌丝各种试验,有两种方法可以供参考,在说明以下两种方法前首先声明所用的依赖如下:
<!-- 增加对web的支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web
是用来支持web应用开发的,在导入之后会自动导入内嵌的servlet容器,比如Tomcat
,但是要注意的是此时并没有能解析jsp页面的jasper模块,由此可以看出内嵌的servlet容器
并不是全部导入的,而是分模块部分导入的,Jasper需要我们手动导入。如果还要使用jstl库,也可以继续导入:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
1、在工程中返回jsp页面
由于Spring Boot的目录结构和普通的Web应用的目录结构是不同的,为了返回jsp,首先要建立对应的web应用程序目录,在/src/main
下建立WEB-INF/jsp
目录,形成/src/main/WEB-INF/jsp
,然后将我们的jsp页面放在下面即可。
然后再继续配置application.properties
文件,如下:【注意等号右边不能有注释 在Springboot1.0.2的时候是没有mvc的 】
<code class="language-xml hljs has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="color:#ff6600;">spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp</span></code><code class="language-xml hljs has-numbering" style="color: inherit; display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">#<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;">Springboot1.0.2</span></code><code class="language-xml hljs has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="color:#cc0000;">spring.view.prefix=/WEB-INF/jsp/</span></span></span></code><code class="language-xml hljs has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;"><span style="font-family: 'Source Code Pro', monospace; font-size: 14px; line-height: 20.3px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);"><span style="color:#cc0000;">spring.view.suffix=.jsp</span></span><span style="color:#555555;">
</span></span></code><ul class="pre-numbering" style="color: rgb(51, 51, 51); box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="color: rgb(51, 51, 51); box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
这与只使用SpringMVC时的配置十分类似,而且可以看出使用的是InternalResourceViewResolver
视图解析器。使用代码测试一下:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Controller</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">SpringBootController</span> {</span>
<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@RequestMapping</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> String <span class="hljs-title" style="box-sizing: border-box;">index</span>(){
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"welcome"</span>;
}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
以上会返回/src/main/WEB-INF/jsp/welcome.jsp
视图。
但是,返回页面只是在eclipse这种IDE中直接运行Spring Boot程序时会起作用,如果打包成jar包后是不会包含我们创建的WEB-INF/jsp
目录的,也就没有我们定义的jsp页面,所以会返回404状态码
,所以这种方法实际上是没有什么意义的,个人感觉(没用你还说什么!!只是提出一种解决方式。。)
2、在打包后返回jsp页面
如果想在打包后使用java -jar ***.jar
来运行代码,就要使用如下这种方法,在/src/main/resources/
中建立如下的目录META-INF/resources/WEB-INF/jsp
,然后application.properties
的配置和第一种方法相同。但是这样在使用mvn package
打包后会包含上面建立的目录,如下:
这时使用java -jar SpringBootTemplate-0.0.1-SNAPSHOT.jar
就可以愉快地部署应用程序了,顺便说一句,此时在jsp页面也可以使用EL表达式获取Model中存储的数据了。