案例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. R学习笔记2 因子

    特殊值: 缺失值 NA(Not Avaiable),是不可用的缩写: NaN为“不是一个数”,意味着计算没有数学意义: NULL值,空值,表示一个空的变量,不会占用任何空间,通过is.null(x)来 ...

  2. MVC比WebForm的优势,为什么使用MVC

    前言 如果你看了最近微软的议程,你会发现他们现在的焦点除了MVC,还是MVC.问题在于为什么微软如此热衷于丢弃传统的APS.NET Webform而转向ASP.NET MVC?本文就主要来讨论这个问题 ...

  3. angular 8 配置路由

    一.生成路由文件 按照惯例,有一个独立模块来配置相关路由,这个模块类的名字叫做AppRoutingModule,位于src/app下的app-routing.module.ts文件中. 使用CLI生成 ...

  4. linux基础命令之1

    1.创建文件夹:mkdir 文件夹名称 2.创建文件:touch  文件名称 3.编辑文件:vi 文件名称 4.保存文件::wq

  5. robot framework笔记(三):扩展SeleniumLibrary库 (自定义关键字)

    (一)自定义和浏览器相关的关键字 以下代码GitHub 版本库地址: https://github.com/blairwind/blog_rf SeleniumLibrary的扩展文档中提供了3种增加 ...

  6. 小米cc9和vivo z5 对比

    (一)大致对比 1.小米 cc9(6GB+128GB.  1899元. 白色恋人(白色)) https://item.mi.com/product/10000163.html 2.vivo Z5 (6 ...

  7. 0001-代码仓库-git 命令

    参考 https://www.cnblogs.com/NTWang/p/6213408.html https://www.cnblogs.com/Sungeek/p/6905102.html

  8. vi / vim 基本操作

    进入vi的命令         vi filename :打开或新建文件,并将光标置于第一行首    vi n filename :打开文件,并将光标置于第n行首    vi filename :打开 ...

  9. 通过标签名获得td和tr

    <tr node="123445"> <td> <input type=button name="dak"> </td ...

  10. Js获取url问号(View_Detail?data='+data.zjb_ID+'&'+data.D_Name)传值

    Js逻辑 View_Detail?data='+data.zjb_ID+'&'+data.D_Name <script> $(function () { var url = dec ...