【Spring boot 集成 JSP】

Spring boot 集成 JSP
这个部分比较复杂,所以单独创建一个工程来进行讲解;
大体步骤:
1 )创建 Maven web project
2 )在 pom.xml 文件添加依赖;
3 )配置 application.properties 支持 jsp
4 )编写测试 Controller
5 )编写 JSP 页面
6 )编写启动类 App.java
1,FreeMarker
2,Groovy
3,Thymeleaf (Spring 官网使用这个)
4,Velocity
5,JSP (貌似 Spring Boot 官方不推荐,STS 创建的项目会在 src/main/resources 下有个 templates 目录,这
里就是让我们放模版文件的,然后并没有生成诸如 SpringMVC 中的 webapp 目录)
不过本文还是选择大家都熟悉的 JSP 来举例,因为使用 JSP 与默认支持的模版需要特殊处理,所以拿来举例更
好。
(1)创建 Maven web project
使用 Eclipse 新建一个 Maven Web Project ,项目取名为:
springboot02
(2)在 pom.xml 文件添加依赖
<!-- spring boot parent 节点,引入这个之后,在下面和 spring boot 相关的就不需要引入版本了 ; -->
< parent >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-parent </ artifactId >
< version > 1.3.3.RELEASE </ version >
</ parent >
依赖包:
<!-- web 支持 : 1 web mvc; 2 restful; 3 jackjson 支持 ; 4 aop ........ -->
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-web </ artifactId >
</ dependency >
<!-- servlet 依赖 . -->
< dependency >
< groupId > javax.servlet </ groupId >
< artifactId > javax.servlet-api </ artifactId >
< scope > provided </ scope >
</ dependency >
<!--
JSTL JSP Standard Tag Library JSP 标准标签库 ) 是一个不断完善的开放源代码的 JSP
签库,是由 apache jakarta 小组来维护的。 JSTL 只能运行在支持 JSP1.2 Servlet2.3 规范的容器上,
tomcat4.x 。在 JSP 2.0 中也是作为标准支持的。
不然报异常信息:
javax.servlet.ServletException: Circular view path [/helloJsp]: would
dispatch back to the current handler URL [/helloJsp] again. Check your ViewResolver
setup! (Hint: This may be the result of an unspecified view, due to default view name
generation.)
-->
< dependency >
< groupId > javax.servlet </ groupId >
< artifactId > jstl </ artifactId >
</ dependency >
<!-- tomcat 的支持 .-->
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-tomcat </ artifactId >
< scope > provided </ scope >
</ dependency >
< dependency >
< groupId > org.apache.tomcat.embed </ groupId >
< artifactId > tomcat-embed-jasper </ artifactId >
< scope > provided </ scope >
</ dependency >
Jdk 编译版本:
< build >
< finalName > spring-boot-jsp </ finalName >
< plugins >
< plugin >
< artifactId > maven-compiler-plugin </ artifactId >
< configuration >
< source > 1.8 </ source >
< target > 1.8 </ target >
</ configuration >
</ plugin >
</ plugins >
</ build >
(3)application.properties 配置
上面说了 spring-boot 不推荐 JSP,想使用 JSP 需要配置 application.properties。
添加 src/main/resources/application.properties 内容:
# 页面默认前缀目录
spring.mvc.view.prefix = /WEB-INF/views/
# 响应页面默认后缀
spring.mvc.view.suffix = .jsp
# 自定义属性,可以在 Controller 中读取
application.hello = Hello Zjs From application
(4)编写测试 Controller
编写类:com.hpit.sb.controller. HelloJSPController:
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* TODO 开发控制器,该控制器将返回到JSP视图
*
*
*/
@Controller
public class HelloJSPController {
@RequestMapping ( "/index" )
public String hello(ModelMap map ) {
map .put( "message" , "this data is from the backing server" );
return "index" ;
}
}
(5)编写 JSP 页面
在 src/main 下面创建 webapp/WEB-INF/views 目录用来存放我们的 jsp 页面:index.jsp
<%@ page language = "java" contentType = "text/html; charset=UTF-8"
pageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title > hello jsp </ title >
</ head >
< body >
${message }
</ body >
(6)编写启动类
编写 App.java 启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args ) throws Exception {
SpringApplication. run (App. class , args );
}
}
运行程序,访问页面:

 

附注:关于集成 JSP 几个问题:
1 Spring Boot 使用 jsp 时,仍旧可以打成 jar 包的形式吗?
2 Spring Boot 使用 jsp 时,比如说 css image js 等三种静态资源文件,应该放在什么目录下?这些静态资源映
射,在 spring boot 中具体应该怎么做?
例如,下面是 spring 中做的静态资源映射,但是在 spring boot 中不知道怎么处理:
<!-- springmvc.xml 资源映射 -->
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
<mvc:resources location="/WEB-INF/image/" mapping="/image/**"/>
3 下面这个 tomcat 的包必须导入 吗, spring-boot-starter-web 中不是有一个内嵌的 tomcat 吗?
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-tomcat </ artifactId >
< scope > provided </ scope >
</ dependency >
<1>、针对第一个问题,答案是不可以的。
我们先看一段英文描述,如下:
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an
executable archive), there are some limitations in the JSP support.
With Tomcat it should work if you use war packaging, i.e. an executable war will work, and will also be
deployable to a standard container (not limited to, but including Tomcat). An executable jar will not work
because of a hard coded file pattern in Tomcat.
Jetty does not currently work as an embedded container with JSPs.
Undertow does not support JSPs.
原文的大体意思就是:Tomcat 支持 war 的打包方式,spring boot 支持 war 打包方式。Jetty 现在不支持
JSP 嵌入容器。Undertow 根本就不支持 JSP。
所以答案就是打包成 war,jsp 会自然按照 servlet 的标准部署。但也就意味着你不可以用嵌入式的方式运
行,而是 Tomcat Server + war 的部署方式。
看到这里有些网友肯定会有疑问那什么是嵌入式的 web 服务器?我们这边就拿 jetty 来说明下。
Jetty 可以非常容易的嵌入到应用程序当中而不需要程序为了使用 Jetty 做修改。
从某种程度上,你也可以把 Jetty 理解为一个嵌入式的 Web 服务器。所以我们经常会说嵌入式 jetty。
Jetty 有一个口号:不要把你的应用部署到 Jetty 中,把 Jetty 部署到你的应用中。Jetty 可以在 Java 应用
程序中向其他 POJO 一样被实例化,换句话说,以嵌入式的模式运行 Jetty 是指将 Http 模块放入你的应用程序
中,而非部署你的程序到一个 HTTP 服务器。这就是所谓的嵌入式 jetty。
另外在说明一点就是 JSP 解析是需要 JSP 引擎处理的,tomcat 就提供了 JSP 处理引擎。所以很显然 JSP
是依赖容器而存在的,不然就没法访问了。那么既然是依赖于 tomcat 的话。
有一网友找到一支持打成 jar 包运行的插件:
Using Spring Boot with JSPs in Executable Jars
https://github.com/ghillert/spring-boot-jsp-demo
经过 java -jar xxx.jar 运行后,可以正常访问网页。
这也可以说明原本是不支持的,但是如果非要支持的话,那么需要进行使用插件进行支持。
<2>针对第二个问题
对于第二个问题,如果看过之前的章节就很好解决了,只需要在 src/main/resouces 下新建一个 static 目
录,然后在 static 下新建子目录:css,images,js 目录,在 images 放入一张 test.jpg 图片,那么访问路径是:
http://127.0.0.1:8080/images/test.jpg
当前目录结构应该是这样子的:
(1)--src/java/resources
(2)-- static
(3)-- css
(3)-- images
(3)-- js
那么有人会有疑问这个,打包的时候能打上嘛,答案是可以的,请看实际打包解压图:

 

好的,我为你提供一个简单的Spring Boot集成JSP的示例项目,你可以参考一下: 1. 创建一个Maven项目,pom.xml文件中添加以下依赖: ```xml <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat Embed Jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> ``` 2. 在src/main目录下创建一个webapp目录,然后在webapp目录下创建一个jsp目录,用于存放JSP文件。 3. 在src/main/java目录下创建一个Spring Boot的配置类,例如: ```java @Configuration @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 在src/main/java目录下创建一个Controller类,例如: ```java @Controller public class HomeController { @RequestMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, World!"); return "home"; } } ``` 5. 在src/main/resources目录下创建一个application.properties文件,用于配置应用程序的属性,例如: ``` spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp ``` 6. 运行应用程序,访问http://localhost:8080/,会显示Hello, World!。 以上就是一个简单的Spring Boot集成JSP的示例项目,你可以在此基础上进行开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

God Zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值