Springboot集成使用jsp

版权声明:本文为博主原创文章,未经博主允许不得转载

简介:

官方不推荐使用jsp作为页面,我们可以使用其他的模板引擎,比如 Thymeleaf 和 freemarker,官方主推的是Thymeleaf。但是目前来说,很多项目的页面还是用的jsp.而且很多现成的项目用的jsp页面,扒过来就能用,当然如果时间允许的情况,还是不建议大家使用jsp,而是采用官方推荐的模板。

搭建项目

1.新建一个基础的 springboot WEB项目

2.pom.xml文件jar包

在这里插入代码片
```<?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 http://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.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.mr</groupId>
   <artifactId>springboot_jsp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboot_jsp</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>
   <packaging>war</packaging>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>


      <!--jsp支持-->
      <!-- 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.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
      </dependency>
      <!--引入Spring boot内嵌的Tomcat对jsp的解析包-->
      <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
         <scope>provided</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
      <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>
   </build>

</project>

3. 新建webapp文件夹,与resources同级

在这里插入图片描述

4. 新建JSP页面,此时发现New里面没有JSP页面。需要设置一下才会出现

在这里插入图片描述

添加web

在这里插入图片描述

选择已经添加的webapp

在这里插入图片描述

此时webapp上有蓝色圆点,表示设置成功

在这里插入图片描述
在这里插入图片描述

5.application.properties

```server.port=8082

#页面默认前缀目录 默认在webapp下有别的文件夹可以,以文件夹/往下加
spring.mvc.view.prefix=/
#页面默认后缀目录
spring.mvc.view.suffix=.jsp``

简单Demo

6.JspController.java


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

/**
 * 
 */
@Controller
public class JspController {
    @GetMapping("/boot/index")
    public String index(Model model){
        model.addAttribute("msg","Spring boot集成jsp");
        return "index";
    }
    @GetMapping("/boot/aaa")    
    public String aaa(Model model){
        model.addAttribute("sss","boot 整合jsp");
        return "aaa";
    }
}

7.index.jsp

         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>Insert title here</title>
</head>
<body>
    <h1>Hello word</h1>
    ${msg}
</body>
</html>

8.aaa.jsp

  Created by IntelliJ IDEA.
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${sss}
</body>
</html>

在这里插入图片描述

右键选择spring-boot:run启动项目即可。

浏览器效果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44914784/article/details/89300238