通过观看https://my.oschina.net/sdlvzg/blog/1154281创建项目,再执行以下操作
在pom.xml中引入依赖包
<!-- 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>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
在pom.xml中配置Jdk编译版本:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
在application.properties引入相关配置信息
########################################
###Spring MVC配置
########################################
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
编写代码
编写模版代码(存放到项目 src/main/webapp/WEB-INF/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>
<!-- 针对el表达式,类似${hello}这个对于servlet的版本是有限制的,2.4版本版本以下是不支持的,是无法进行识别的,请注意 -->
编写控制层代码
package org.lvgang.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* springboot jsp controller
* @author Administrator
*/
@Controller
public class HelloController {
@RequestMapping("/helloJsp")
public String helloJsp(Map<String, Object> map) {
System.out.println("HelloController.helloJsp().hello=lvgang");
map.put("hello", "lvgang");
return "helloJsp";
}
}
验证是否成功
通过Main方法启动项目,然后访问http://127.0.0.1:8080/helloJsp,如果页面出现以下信息,表示配置成功