码迷,mamicode.com
首页 > 编程语言 > 详细

springboot2.0整合freemarker springboot日期转换器

时间:2018-10-29 16:58:13      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:port   springmvc   color   word   charset   import   mod   public   spring   

pom.xml

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

application.properties

#viewResovler
#spring.mvc.view.prefix=/WEB-INF/pages/
#spring.mvc.view.suffix=.jsp

#dataSource
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=scott
spring.datasource.password=tiger

#springMVC applicationContext-dao.xml SqlSessionFactory mapperLocations
mybatis.mapper-locations=classpath:mapper/*.xml

#freemarker
spring.freemarker.content-type=text/html
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=/WEB-INF/ftl/

 

日期转换器类,springboot不需要在写配置文件,springmvc需要写配置文件

package com.springboot.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class DateConverterConfig implements Converter<String, Date> {

@Override
public Date convert(String source) {
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}

}

 

controller

@Autowired
private EmpService empService;

@RequestMapping("/empIndex.html")
public String empIndex(Model model) throws Exception {
List<Emp> empList = empService.queryAll();
model.addAttribute("empList",empList);
return "empIndex";
}

 

empIndex.html (freemarker模板)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" cellspacing="0" bgcolor="#f2f2f2">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员职位</td>
<td>雇佣日期</td>
<td>雇员工资</td>
<td>雇员佣金</td>
</tr>
<#list empList as emp>
<tr>
<!-- 去逗号 -->
<td>${emp.empno?c}</td>
<td>${emp.ename!}</td>
<td>${emp.job!}</td>
<!-- 接收日期,格式化 -->
<td>${emp.hiredate?string(‘yyyy-MM-dd HH:mm:ss‘)}</td>
<td>${emp.sal!}</td>
<!-- 可以为null,为null时显示空串 -->
<td>${emp.comm!}</td>
</tr>
</#list>
</table>
</body>
</html>

springboot2.0整合freemarker springboot日期转换器

标签:port   springmvc   color   word   charset   import   mod   public   spring   

原文地址:https://www.cnblogs.com/jinlin-2018/p/9871389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!