SpringBoot整合JSP

SpringBoot整合JSP

文章素材来自动力节点小郭老师的视频课
视频链接点这里

在开始创建项目前添加阿里云的镜像仓库,可以提高下载速度
如何在idea中查看集成的maven路径:

Setting >> Build >> Build Tools >> Maven
在这里插入图片描述
以我的为例打开maven的配置文件 D:\apache-maven-3.3.9\conf\settings.xml
在<mirrors>标签中添加镜像仓库

<mirror>
	<id>nexus-aliyun</id>
	<mirrorOf>*</mirrorOf>
	<name>Nexus aliyun</name>
	<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

1、创建一个空项目

2、新建模块 选项Spring Initializr

在这里插入图片描述

3、点击Next,然后填写模块的GAV

在这里插入图片描述

4、web项目就选Web–>SpringWeb

在这里插入图片描述
以上便完成了SpringBoot工程的创建

5、在src/main目录下创建webapp文件夹并指定为webapp的资源文件夹

model >> web >> web resource directories >> + >>选中刚创建webapp文件夹 >>ok>>Create Artifact
在这里插入图片描述

此时如果文件夹图标发生变化说明已经成功

在这里插入图片描述

6、引入SpringBoot内嵌Tomcat对jsp的解析依赖

在pom.xml文件的<dependencies>标签的添加依赖
这里仅仅只是展示jsp页面所以只添加了一个依赖(需要用jstl需添加jstl的依赖)

<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

7、指定jsp最后的编译路径

在<build>标签中添加

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

8、在SpringBoot核心配置文件中配置视图解析器

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

到这springBoot集成jsp就完成了

接下来的测试:

在Application类(有@SpringBootApplication注解的类)所在的同级或下级目录下创建控制器类

@Controller
public class MyController {
    @RequestMapping(value="/hello")
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message","helloSpringBoot");
        mv.setViewName("hello");
        return mv;
    }
}

然后在webapp目录下创建一个hello.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    ${message}
</body>
</html>

启动项目 >> 浏览器访问
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值