案例springboot_freemarker

application.properties配置文件

###FreeMarker配置
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.request-context-attribute=request

文件夹中创建HelloFreeMarker.ftl文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringBoot整合FreeMarker</title>
</head>
<body>
欢迎:<#--${name}-->
<#list stuList as stu>
${stu.stu_name}
</#list> <#if 1==1>
呵呵,相等
</#if>
</body>
</html>

FreeController

@Controller
@RequestMapping("/free")
public class FreeController { @RequestMapping("/freeFirst")
public String freeFirst(ModelMap map){
map.put("name","没穿裤子");
} @RequestMapping("/freeSecond")
public String freeSecond(ModelMap map){
List<String> list=new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
map.put("userList",list);
return "helloFreeMarker";
} @RequestMapping("/freeThread")
public String freeThread(ModelMap map){
List<Student> list=new ArrayList<>();
Student stu=new Student();
stu.setStu_id();
stu.setStu_name("张三");
list.add(stu);
map.put("stuList",list);
return "helloFreeMarker";
}
}

Student实体类

public class Student {
private Integer stu_id;
private String stu_name; public Integer getStu_id() {
return stu_id;
} public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
} public String getStu_name() {
return stu_name;
} public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
}

案例springboot_hellow

FirstController

@RestController
/**
* 如果说在Controller类上加RestController注解代表该controller当中的所有方法都返回Json串
*/
@RequestMapping("/first")
public class FirstController {
@RequestMapping("/firstRequest")
public String firstRequest(){
int result=/;
System.out.println("第一个请求到达Controller");
return "Hello SpringBoot";
}
}

MyExceptionHandler

@ControllerAdvice
public class MyExceptionHandler {
//捕获运行时异常
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String,Object> exceHandler(){
Map<String,Object> map=new HashMap<>();
map.put("error","");
map.put("msg","您好,服务器暂时出现异常,请稍后重试");
return map;
}

StartSpringBoot

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

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
静态资源HTML页面
</body>
</html>

SpringBoot基础及FreeMarker模板的更多相关文章

  1. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  2. 使用 FreeMarker模板 Springboot 发送邮件

    四.使用 FreeMarker模板 HTML 标签的字符串拼接是一件很棘手的事.因为在你的大脑中解析HTML标签并想象它在渲染时会是什么样子是挺困难的.而将HTML混合在Java代码中又会使得这个问题 ...

  3. springboot 使用FreeMarker模板(转)

    在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行: 1.引入依赖: <dependency> <groupId>org.springfra ...

  4. SpringBoot获取Freemarker模板引擎,生成HTML代码

    今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...

  5. Springboot模板(thymeleaf、freemarker模板)

    目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...

  6. SpringBoot基础实战系列(一)整合视图

    SpringBoot整合freemarker 1.添加依赖:springboot基本上是无缝衔接,基本上只需要添加对应的依赖,不需要或者做很少量的配置即可 注:对于springboot项目的创建此处不 ...

  7. Spring 4 使用Freemarker模板发送邮件&添加附件

    前言 Spring对Java的邮件发送提供了很好的支持,提供了超级简单的API,大大简化了Java邮件发送功能的开发. Spring对Email的支持是基于JavaMail API开发的,所以,我们在 ...

  8. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  9. SpringBoot下配置FreeMarker配置远程模版

    需求产生原因 要求在同一个接口中,根据不同的参数,返回不同的视图结果 所有的视图中的数据基本一致 要求页面能静态化,优化SEO 例如:A接口返回客户的信息 客户A在调用接口时,返回其个性化定制的页面A ...

随机推荐

  1. C++ 读写 Excel 文件

    //Microsoft Visual Studio 2015 Enterprise #include <fstream> #include <string> #include ...

  2. Inno Setup 检测已安装的.NET Framework 版本

    翻译自:http://kynosarges.org/DotNetVersion.html 由 Jordan Russell 写的 Inno Setup 是一个伟大的安装脚本程序,但缺乏一个内置的函数来 ...

  3. python大道——博客目录

      python基础 第一章 计算机基础 计算机基础 第二章 python基础语法 python入门 第三章 基础数据类型和文件操作 整型.布尔.字符串 列表.字典.集合 公共功能.小数据池 hash ...

  4. pandas再次学习

    numpy.scipy官方文档  pandas官方网站  matplotlib官方文档 一.数据结构 二.数据处理 1.数据获取(excel文件数据基本信息) #coding=utf-8 import ...

  5. MySQL练手小试题

    创建表和数据 创建class表 create table class ( cid int(11) primary key auto_increment, caption varchar(32) not ...

  6. L2R 一:基础知识介绍

    一.背景 l2r可以说是搜索推荐里面很常用的知识了,一直处于一知半解的地步,今天开个博客准备把这些零散的东西系统性整理好,一版就粗糙点了. 二.粗概 前段时间的项目主要和搜索引擎相关,记录下搜索引擎的 ...

  7. Flutter Image(图片)

    Image是一个用于展示图片的组件.支持 JPEG.PNG.GIF.Animated GIF.WebP.Animated WebP.BMP 和 WBMP 等格式. Image 有许多的静态函数: ne ...

  8. UML系列——OO Unit4分析和学期总结

    一.本单元的架构设计 1.类图 第一次 第二次 2.关键方法和架构简述 总体而言是读取图的时候就完成大部分计算(完成缓存),调用查询方法时只是展示计算的结果,少部分直接计算.主要是设计了各种自己定义的 ...

  9. fatal: 不是一个有效的对象名:'master'。

    听说git比svn的branch功能好,所以装了个msysgit玩.执行完了git init操作后想建branch,用git branch develop命令,结果报错,说 fatal: Not a ...

  10. 【亲测可行,图片宽度高度自适应】c# Graphics MeasureString精确测量字体宽度

    , , ) { int count = number.Length; //需要配置的字段 //Font f = new Font("Microsoft Sans Serif", f ...