SprintBoot支持jsp

( 1 )创建 Maven web project
使用 Eclipse 新建一个 Maven Web Project ,项目取名为:
spring-boot-jsp
( 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 ........ -->
http://412887952-qq-com.iteye.com 1.22 (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
第 83 / 266 页
<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规范的容器上,如tomcat 4.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>
http://412887952-qq-com.iteye.com 1.22 (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
第 84 / 266 页
<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/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application
( 4 )编写测试 Controller
http://412887952-qq-com.iteye.com 1.22 (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
第 85 / 266 页
编写类: com.kfit.jsp.controller.HelloController:
package com.kfit.jsp.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 测试
* @author Angel(QQ:412887952)
* @version v.0.1
*/
@Controller
public class HelloController {
// 从 application.properties 中读取配置,如取不到默认值为Hello Shanhy
@Value("${application.hello:Hello Angel}")
private String hello;
@RequestMapping("/helloJsp")
http://412887952-qq-com.iteye.com 1.22 (19)Spring Boot 添加JSP支持【从零开始学Spring Boot】
第 86 / 266 页
public String helloJsp(Map<String,Object> map){
System.out.println("HelloController.helloJsp().hello="+hello);
map.put("hello", hello);
return "helloJsp";
}
}
( 5 )编写 JSP 页面
在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面 : helloJsp.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>Insert title here</title>
</head>
<body>
helloJsp
<hr>
${hello}
</body>
</html>

猜你喜欢

转载自blog.csdn.net/douyunqian668/article/details/81100703