SpringBoot配置使用JSP

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhou6282610/article/details/88383914

项目结构

jsp页面放在webapp目录下。

spring-boot-jsp
 +-src
    +- main
         +- java
         +- resources
         +- webapp
              +- WEB-INF
                 +- jsp
                    +- welcome.jsp
    +- test
 +-pom.xml

配置文件

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
  • prefix:指明jsp页面的位置;
  • suffix:指明jsp页面的后缀;

在pom中引入依赖包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
  • jstl 是一个 JSP 标签集合,它封装了 JSP 应用的通用核心功能;
  • tomcat-embed-jasper 主要用来支持 JSP 的解析和运行;

写一个简单的jsp页面

<!DOCTYPE html>
<html lang="en">

<body>
    Time:  ${time}
    <br>
    Message: ${message}
</body>

</html>

springboot后端程序

@Controller
public class WelcomeController {

    @GetMapping("/")
    public String welcome(Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", "hello world");
        return "welcome";
    }

}

启动springboot程序进行页面访问

Time: Sun Mar 10 21:04:55 CST 2019 
Message: Hello World!

猜你喜欢

转载自blog.csdn.net/zhou6282610/article/details/88383914
今日推荐