SpringBoot实战之8 整合jsp模版引擎

历史文章

SpringBoot实战之入门

springboot实战之文章汇总

springboot实战之读取配置文件

一、简介

springboot支持多种模版引擎包括:
1. FreeMarker
2. Groovy
3. Thymeleaf (Spring 官网使用这个)
4. Velocity
5. JSP (貌似Spring Boot官方不推荐)

不过本文还是选择大家都熟悉的JSP来作为研究案例。

二、导包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

三、controller层

@Controller
public class ViewController {

    /**
     * @description <p></p>
     * @return
     * @author heshiyuan
     * @date 2017/12/2 20:56
     */
    @RequestMapping(value = {"/","/index"})
    public String index(HttpServletRequest request){
        request.setAttribute("content","this is index");
        request.setAttribute("time", Calendar.getInstance().getTime());
        // 自动映射到"/WEB-INF/jsp/"下
        return "index" ;
    }

    @RequestMapping("/toPage1View")
    public String page1(Model model){
        model.addAttribute("time",Calendar.getInstance().getTime()) ;
        model.addAttribute("content","this is page1");
        return "page/page1" ;
    }

    @RequestMapping("/toPage2View")
    public ModelAndView page1(ModelAndView modelAndView){
        modelAndView.setViewName("page/page2");
        modelAndView.addObject("time",Calendar.getInstance().getTime()) ;
        modelAndView.addObject("content","this is page1") ;
        return modelAndView;
    }
}

四、配置文件

spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

五、配置页面

index.html

<%@page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
    <title>主页</title>
</head>
<body>
    ${content}
    ${time}
</body>
</html>

page/page1.jsp page/page2.jsp均是类似的页面。

六、启动

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

七、项目结构图

这里写图片描述

最后

此demo最后没有运行运行成功,提示如下错误,正在解决当中,有线索的道友感谢提供帮助。
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值