《穿越SpringBoot 》 第二章-SpringBoot 的 Web开发 | 第2节- SpringBoot 整合 Jsp模板引擎

15 篇文章 1 订阅
1 篇文章 0 订阅

SpringBoot 集成 JSP

前提:

借助:Maven构建工具IntelliJ IDEA,以及基于SpringBoot 2.3.4

可参考SpringBoot官方文档:https://docs.spring.io/spring-boot/docs/

更多干货:

请阅读:《穿越SpringBoot》系列文章
请参考:Java学习资料

背景:

SpringBoot 官方大人对jsp是不建议使用的哦。

在这里插入图片描述

按照在下的办法,官人您可以按照下面的步骤执行就可以搞定哟!

目录结构:

来瞧一下,下面这张图:

在这里插入图片描述

官人如果不会创建没关系,可以参考:第一章-SpringBoot 入门 | 第1节- 构建第一个Spring Boot工程创建一个入门先玩玩吧!

使用:

配置pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>boot-web-traditional</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-web-traditional</name>
	<description>Demo project for Spring Boot</description>
	<packaging>war</packaging>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
<!--	tomcat	-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>
<!--	tomcat处理jsp 内置tocat对Jsp支持的依赖,用于编译Jsp-->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
<!--	javax.servlet-api	-->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
<!--    JstL    -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
<!--	web	-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
<!--	lombok	-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
<!--	test	-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
<!--   如果想修改打包的名称,需在build中加入   -->
	  	<finalName>springBootJsp</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>

<!--	下面配置idea主程序不能直接运行主程序的问题	  -->

		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/webapp</directory>
				<targetPath>META-INF/resources/</targetPath>
				<includes>
					<include>**/*.*</include>
				</includes>
			</resource>
		</resources>
	</build>

</project>

配置application.properties

下图的配置:spring.mvc.view.prefix和spring.mvc.view.suffix是SpringBoot与我们约定的视图前缀后后缀配置,意思是找到/WEB-INF/jsp/目录下的.jsp文件,那么前缀后后缀之间少了文件的名称,这个名称将由控制器(Controller)给出,在控制器中指定返回的路径和名称,就能访问到具体的jsp页面。
#视图解析器的配置
#文件存放的路径
spring.mvc.view.prefix=/WEB-INF/jsp/
#文件后缀
spring.mvc.view.suffix=.jsp

编写BootWebTraditionalApplication 启动类

Spring Boot项目需要部署在外部容器中的时候,Spring Boot导出的war包如果直接在Tomcat的部署会报错,如需运行需进行如下 修改:

1.启动类继承SpringBootServletInitializer

外部容器部署的话,就不能依赖于Application的main函数了,而是要以类似于web.xml文件配置的方式来启动Spring应用上下文,此时我们需要在启动类中继承SpringBootServletInitializer并实现configure方法:

2.pom.xml添加spring-boot-starter-tomcat ,上面的pom文件已配置

3.打包方式修改为< packaging>war< /packaging >,上面的pom文件已配置

4.如果想修改打包的名称,需在build节点中加入< finalName>springBootJsp< /finalName>,上面的pom文件已配置

package com.example.bootwebtraditional;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.core.SpringVersion;
@SpringBootApplication
public class BootWebTraditionalApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(BootWebTraditionalApplication.class, args);
	}
	//BootWebTraditionalApplication 需要继承 SpringBootServletInitializer 重写 configure 方法 进行初始化。
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(BootWebTraditionalApplication.class);
	}
}

编写DemoController 类

package com.example.bootwebtraditional.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DemoController {
    @RequestMapping("/demo1")
    public String demo1(Model model){
        model.addAttribute("data","测试jsp页面的controller");
        return "test";
    }
}

编写jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
    <h1>hello spring boot 中的jsp</h1>
    <h2>${data}</h2>
</body>
</html>

测试:

启动项目后直接访问http://localhost:8080/demo1进行测试,执行结果如下:在这里插入图片描述

总结:

待完善…
本教程基于最新的spring-boot-starter-parent:2.3.4RELEASE编写,目前很多大佬都写过关于SpringBoot的教程了,如有雷同,请多多包涵.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值