SpringBoot中Jsp的使用

一、POM

<?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>

    <groupId>com.yale</groupId>
    <artifactId>SpringBootJsp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </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>

            <!--springboot使用的web资源要编译到META-INF/resources-->
            <resource>
                <directory>src/main/webapps</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

        </resources>
    </build>
</project>

二、application.yml

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

三、Controller

package com.yale.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
public class IndexController {

    @RequestMapping({"/","/index"})
    public String index(Map<String,Object> map){
        map.put("name","SpringBoot");
        map.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "index";
    }

}

四、Jsp

<%@page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Spring整合Jsp</title>
    </head>
    <body>
        Hello ${name},当前时间:${date}.
    </body>
</html>
五、附目录
D:\idea_workspace\springboot_test\SpringBootJsp>tree /f
文件夹 PATH 列表
卷序列号为 0000-8AEC
D:.
│  pom.xml
│  SpringBootJsp.iml
│
├─.idea
│
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─yale
│  │  │          │  Application.java
│  │  │          │
│  │  │          └─controller
│  │  │                  IndexController.java
│  │  │
│  │  ├─resources
│  │  │      application.yml
│  │  │
│  │  └─webapps
│  │      └─WEB-INF
│  │          └─jsp
│  │                  index.jsp
│  │
│  └─test
│      └─java
└─target
    ├─classes
    │  │  application.yml
    │  │
    │  ├─com
    │  │  └─yale
    │  │      │  Application.class
    │  │      │
    │  │      └─controller
    │  │              IndexController.class
    │  │
    │  └─META-INF
    │      └─resources
    │          └─WEB-INF
    │              └─jsp
    │                      index.jsp
    │...


D:\idea_workspace\springboot_test\SpringBootJsp>

六、补上Application

package com.yale;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

七、测试

1. Run -> Debug "spring-boot:run"
2. 浏览器访问:http://localhost:8080/
3. 返回:Hello SpringBoot,当前时间:2019-03-05 20:16:43.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值