springboot系列文章(七)springboot中使用jsp

先前我们先看下这个命令:Spring Boot Maven Plugin中的spring-boot:run看下官方的解释:

spring-boot:run

Full name:

org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run

Description:

Run an executable archive application.

Attributes:

  • Requires a Maven project to be executed.
  • Requires dependency resolution of artifacts in scope: test.
  • Since version: 1.1.
  • Binds by default to the lifecycle phase: validate.
  • Invokes the execution of the lifecycle phase test-compile prior to executing itself

他是一个运行可执行应用程序的命令,

当你使用这个命令时会输出以下信息:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< com.kayleoi:spring-boot-web-jsp >-------------------
[INFO] Building spring-boot-web-jsp 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) > test-compile @ spring-boot-web-jsp >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-web-jsp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-web-jsp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to F:\Java\spring-boot\spring-boot-web-jsp\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-boot-web-jsp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\Java\spring-boot\spring-boot-web-jsp\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-boot-web-jsp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\Java\spring-boot\spring-boot-web-jsp\target\test-classes
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) < test-compile @ spring-boot-web-jsp <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) @ spring-boot-web-jsp ---

这是把他先打成了一个jar了然后又执行了内置的tomcat。

接下来看目录结构:

pom.xml:

<?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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kayleoi</groupId>
    <artifactId>spring-boot-web-jsp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-web-jsp</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
        </dependency>
        <!--用于编译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>
    </build>

</project>

controller层:

package com.kayleoi.springbootwebjsp.controller;

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

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(value = "abc")
    public String hello(Model model){
        System.out.println("方法L");
        model.addAttribute("msg","你好");
        return "success";
    }
}

application.yml:配置view在哪个位置和访问的路径

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

server.servlet.context-path=/jsp

通过这样的配置直接启动即可:
访问:

这样所有的web的基本结束。接下来我们看springboot与数据访问和缓存的使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kay三石 [Alay Kay]

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值