SpringBoot基础及FreeMarker模板
案例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模板的更多相关文章
- (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- 使用 FreeMarker模板 Springboot 发送邮件
四.使用 FreeMarker模板 HTML 标签的字符串拼接是一件很棘手的事.因为在你的大脑中解析HTML标签并想象它在渲染时会是什么样子是挺困难的.而将HTML混合在Java代码中又会使得这个问题 ...
- springboot 使用FreeMarker模板(转)
在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行: 1.引入依赖: <dependency> <groupId>org.springfra ...
- SpringBoot获取Freemarker模板引擎,生成HTML代码
今天用Ajax异步添加评论,加载Freemarker模板引擎,生成模板模块 1.新建Freemarker模板 <li id="${comment.oId}"> < ...
- Springboot模板(thymeleaf、freemarker模板)
目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...
- SpringBoot基础实战系列(一)整合视图
SpringBoot整合freemarker 1.添加依赖:springboot基本上是无缝衔接,基本上只需要添加对应的依赖,不需要或者做很少量的配置即可 注:对于springboot项目的创建此处不 ...
- Spring 4 使用Freemarker模板发送邮件&添加附件
前言 Spring对Java的邮件发送提供了很好的支持,提供了超级简单的API,大大简化了Java邮件发送功能的开发. Spring对Email的支持是基于JavaMail API开发的,所以,我们在 ...
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...
- SpringBoot下配置FreeMarker配置远程模版
需求产生原因 要求在同一个接口中,根据不同的参数,返回不同的视图结果 所有的视图中的数据基本一致 要求页面能静态化,优化SEO 例如:A接口返回客户的信息 客户A在调用接口时,返回其个性化定制的页面A ...
随机推荐
- 2019-10-11 ubuntu ssh远程免密登录配置及配置别名
在客户端能正常远程访问服务端的前提下. 客户端: 1)配置免密 执行 ssh-keygen 即可生成 SSH 钥匙,回车三次. 执行 ssh-copy-id user@remote,可以让远程服务器记 ...
- Java开发环境的搭建02——IntelliJ IDEA篇(Windows)
1.IntelliJ IDEA的下载与安装 IntelliJ IDEA简称IDEA,由JetBrains公司开发,是java语言开发的集成环境,也是目前业界被公认的最好的java开发工具之一.尤其在智 ...
- 面试题:在一个文件中有 10G 个整数,乱序排列,要求找出中位数(内存限制为2G)
假设整数为32bit,4个字节存储 这种题目,首先想到的是分而治之.将文件中数字分组.然后遍历文件中的数字,按分组进行计数.最后找到中位数所在的分组区间 1.如果10G个整数都为同一个,那么10G整数 ...
- Java中 StringBuffer StringBuilder String 区别
String 字符串常量 不可变 使用字符串拼接时是不同的2个空间 StringBuffer 字符串变量 可变 线程安全 字符串拼接直接在字符串后追加 StringBui ...
- 生物网络,RNA 与疾病关联分析
题目: 大数据时代下基于网络算法和机器学习的非编码RNA 相关预测研究摘要:最近越来越多的生物实验表明非编码RNA 具有非常重要的生物学功能,参与细胞中的多项重要生命活动,调控许多基本且重要的生物过程 ...
- 转 让NET C# 程序独立运行(脱离 .NET Framework运行,绿色运行) 未验证
但是.net版本众多.而且.NET Framework框架很大.拖着一个大大的.net Framework总是让人很郁闷. 在网上找呀找呀.找到另一个.NET Framework 替代方案.Mono. ...
- 使用jQuery开发tab选项卡插件(可以右键关闭多个标签)
在前一篇“使用jQuery开发tab选项卡插件”的基础上添加了tab标签右键关闭菜单功能,菜单主要包括:关闭当前标签.关闭左侧标签.关闭右侧标签.关闭其他.关闭全部. 一.插件效果 二.实现思路 为w ...
- SWD下载k60
转:JTAG各类接口针脚定义,含义及SWD接线方式 IAR设置如下
- kubernetes第八章--NFS PersistentVolume
- HTML学习摘要2
DAY 2 HTML 标签可以拥有属性.属性提供了有关 HTML 元素的更多的信息. 属性总是以名称/值对的形式出现,比如:name="value". 属性总是在 HTML 元素的 ...