案例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. centos7,jdk8,tomcat8镜像推送到腾讯云

    目录 centos7 jdk tomcat centos7 创建一个mycentos7的文件 vim mycentos7 FROM centos:7 MAINTAINER qyp_mail@sohu. ...

  2. Docker2 docker commit方法镜像制作

    一.前期准备 1.下载一个centos镜像,进入容器,安装wget docker pull centos docker run -it centos bash [root@web1 ~]# docke ...

  3. 转:Java接口和抽象类

    转:http://www.cnblogs.com/dolphin0520/p/3811437.html 一.抽象类 在了解抽象类之前,先来了解一下抽象方法.抽象方法是一种特殊的方法:它只有声明,而没有 ...

  4. java之hibernate之基于主键的双向一对一关联映射

    这篇 基于主键的双向一对一关联映射 1.依然考察人和身份证的一对一关系,如果采用主键关联,那么其表结构为: 2.类结构 Person.java public class Person implemen ...

  5. C盘清理、C盘瘦身、省出30G

    三招C盘瘦身30G,清理win10系统中虚占C盘空间的三大祸害 1.对C盘进行“磁盘清理” C盘右键->属性->磁盘清理->清理系统文件->勾选“windows更新清理”-&g ...

  6. PHP导出XML格式的EXCEL

    <?php function Export(){ set_time_limit(0); ob_start(); $biz = new ZaikuBiz(); $biz->setSearch ...

  7. Linux系统内核正式进入5.0版本时代

    知名Linux内核开发人员兼维护人员Greg Kroah-Hartman今天宣布,Linux Kernel 4.20内核分支已经结束并督促用户尽快升级至更新的内核分支. Linux Kernel 4. ...

  8. jenkins配置Webhook-gitlab

    1.Jenkins 安装完成以后,首先我们在Jenkins中需要安装一下,Gitlab Hook Plugin,GitLab Plugin,Gitlab Authentication plugin插件 ...

  9. 试用一款网荐的 iOS 快速布局UI库

      NerdyUI github: https://github.com/nerdycat/NerdyUI Cupcake (Swift 版本) github: https://github.com/ ...

  10. Hibernate hql getHibernateTemplate()常用方法汇总

    转自:https://www.iteye.com/blog/zwdsmileface-2191943 getHibernateTemplate()常用方法 一.find(String queryStr ...