Springboot2.x 集成jsp

本文详细介绍如何在SpringBoot项目中使用JSP作为视图层,包括添加Maven依赖、配置资源路径、创建JSP页面及控制器,并通过示例演示整个流程。
1、添加pom依赖
<!--引入springboot 内嵌tomcat对jsp的解析包-->
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--servlet依赖jar-->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
</dependency>

<!--jsp依赖jar-->
<dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>javax.servlet.jsp-api</artifactId>
   <version>2.3.1</version>
</dependency>

<!--Jstl标签依赖的jar包start-->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

build下添加

<resources>
   <resource>
      <directory>src/main/java</directory>
      <includes>
         <include>**/*.xml</include>
      </includes>
   </resource>
   <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>
2、在src/main 下创建webapp目录,与src和resources同一级目录

 

3、application.properties添加配置
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

默认在webapp下

4、在webapp目录下创建index.jsp
<%@ page 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>测试页面4</title>
</head>
<body>
${haha}
</body>
</html>
5、controller编写代码
@Controller
public class Test1Controller {
    
    @RequestMapping(value = "/test")
    public String test1(Model model){

        model.addAttribute("haha","我的天");
        return "index";
    }
}
6、测试

http://localhost:8080/test

 

 

转载于:https://www.cnblogs.com/netcorner/p/10494913.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值