springboot集成jsp的Demo实现

本文详细介绍如何在SpringBoot项目中使用JSP页面,包括创建Maven Web项目、添加依赖、配置视图解析器、创建控制器及JSP页面等步骤。

1.创建一个maven web项目

2.添加springboot和jsp相关依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>3.8.1</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
		
	<!-- jsp依赖 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
	</dependency>
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
	</dependency>
		
</dependencies>

3.在application.properties配置文件中添加配置

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

4.创建一个controller

@EnableAutoConfiguration
@ComponentScan(basePackages="com.vhukze.controller")
@RestController
public class JspDemoController {

	@RequestMapping("/toJsp")
	public ModelAndView toJsp() {
		return new ModelAndView("index");
	}
	
	public static void main(String[] args) {
		SpringApplication.run(JspDemoController.class, args);
	}
}

5.在src/main下新建webapp目录,在webapp下面创建配置的路径

 

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

6.启动项目访问controller映射路径

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿演

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

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

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

打赏作者

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

抵扣说明:

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

余额充值