Spring Boot? 使用freemarker模板引擎渲染web视图

效果图

代码

package com.wls.integrateplugs.hello.controller;

/**
 * Created by wls on 2017/8/24.
 */
import java.util.Locale;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import com.sun.org.apache.regexp.internal.RE;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

@RestController
public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(Locale locale, Model model) {
        return "hello world";
    }

    @RequestMapping("/helloWorld")
    public String index() {
        return "Hello World";
    }

    /**
     * 使用@RestController时,则使用ModelAndView显示页面
     * @param map
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/helloThymeleaf",method = RequestMethod.GET)
    public ModelAndView indexThymeleaf(ModelMap map) {
        ModelAndView mv = new ModelAndView("indexThymeleaf");
        map.addAttribute("name","王老师");
        map.addAttribute("host", "http://blog.didispace.com");
        return mv;
    }

    @RequestMapping(value = "/helloFreeMarker",method = RequestMethod.GET)
    public ModelAndView indexFreeMarker(ModelMap map) {
        ModelAndView mv = new ModelAndView("indexFreeMarker");
        map.addAttribute("name","王老师");
        map.addAttribute("host", "http://blog.didispace.com");
        return mv;
    }

    /**
     * 共享session
     * @param session
     * @return
     */
    @RequestMapping(value = "/uid",method = RequestMethod.GET)
    String uid(HttpSession session) {
        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        session.setAttribute("uid", uid);
        return session.getId();
    }

}

  indexFreeMarker.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
FreeMarker模板引擎
<h1>${host}</h1>
</body>
</html>

  

<!--    freemarker      -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

  

时间: 2024-08-13 13:57:26

Spring Boot? 使用freemarker模板引擎渲染web视图的相关文章

Spring Boot? 使用Thymeleaf模板引擎渲染web视图

静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static /public /resources /META-INF/resources 举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件.启动程序后,尝试访问http://localhost:8080/D.jpg.如能显示图片,配置成功. 渲染

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: Thymeleaf FreeMarker Velocity Groovy Mustache Spring Boot 推荐使用这些模板引擎来代替 Jsp,Thymeleaf 只是其中一种,下面我们来简单聊聊Thymeleaf及实践一下如何整合Spring Boot和Thymeleaf. 1.Thyme

Spring Boot系列(3)——模板引擎thymeleaf

〇.thymeleaf是什么 1.在以往开发spring web项目时,若我们想在前端页面上显示一些服务端的数据(即动态显示),得借助JSP的内置对象和JSTL实现,或者通过JavaScript请求实现:其缺点在于,与后端联系太紧密,不利于前后端分离. 2.而使用模板引擎,可以大大克服这一缺点,模板引擎可以使得前端很自然地开发(即更接近原生html). 3.模板引擎优点有:业务逻辑代码与界面代码分离.动态数据与静态数据分离.代码重用.... 4.常用模板引擎:Thymeleaf.FreeMark

关于spring boot整合thymeleaf模板引擎

1.目录结构: 2.前端页面引用: 这里想说的是引用的默认出发点是在/static/下了  3.后端:  使用了mybatis分页插件 @RequestMapping("list") public String getList(Model model,HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam

Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎

文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thymeleaf.这篇,我们看下 Spring Boot 如何集成和使用 FreeMarker. Spring Boot 中使用 FreeMarker 模板非常简单方便.如果想要使用FreeMarker 模板引擎,首先,修改 POM 文件,添加依赖. FreeMaker 代替 JSP 作为页面渲染 <de

spring(6) 渲染web视图

[0]README 1)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在review  "spring(6) 渲染web视图" 的相关知识: [1] 理解视图解析 [1.1]视图解析的基础知识以及spring 提供的其他视图解析器 1)spring mvc 定义了一个名为 ViewResolver的接口,如下 public interface ViewResolver { View resolveViewName(String view

Java之利用Freemarker模板引擎实现代码生成器,提高效率

http://blog.csdn.net/huangwenyi1010/article/details/71249258 目录(?)[-] 开心一笑 视频教程 提出问题 解决问题 前言 技术选型 实现思路 首先假如在数据库中有一张表 ay_test 我们首先要获取数据库的连接这里我只贴出相关的代码 获取数据库表的元数据 最后根据元数据获取表字段注释等等生成相关的文件 代码实现 Java代码实现 FreeMarkerTemplateUtils工具类 实体类 ColumnClass freemark

Spring boot 通用配置文件模板

001 # ===================================================================002 # COMMON SPRING BOOT PROPERTIES003 #004 # This sample file is provided as a guideline. Do NOT copy it in its005 # entirety to your own application.               ^^^006 # ==

使用模板引擎渲染HTML界面

使用模板引擎渲染HTML界面 by 伍雪颖 模板引擎:GRMustache pod 'GRMustache', '~> 7.3.0' html模板: template.html <HTML> <HEAD> </HEAD> <BODY> <H1> {{ name }} </H1> <P> {{ content }} </P> </BODY> </HTML> 调用: - (void)v