springboot极简教程006-使用JSP

本文详细介绍如何在SpringBoot项目中集成JSP技术,包括添加依赖、配置JSP加载路径及后缀、创建JSP文件及Controller等步骤,帮助读者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSP技术并不是springboot官方推荐的,官方推荐thymeleaf,但是可以简单了解一下。

添加依赖

注意:使用JSP时要在pom中关闭thymeleaf的引用。

<!-- servlet依赖. -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

<!-- tomcat的支持.-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

配置JSP加载路径及后缀

在application.yml中添加以下配置:

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

创建JSP文件

在src\main\下创建webapp\WEB-INF\jsp目录,然后在该目录下新建一个hello.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
Hi JSP 当前时间:${now}

添加Controller

注意: HelloController类上面的@Controller注解不能替换为@RestController,原因参见:springboot极简教程005-URL映射

@Controller
public class HelloController {
    @RequestMapping("/hello")
    private String hello(Model model) {
        model.addAttribute("now", DateFormat.getDateTimeInstance().format(new Date()));
        return "hello";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

asmcvc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值