springboot2.0整合freemarker springboot日期轉換器


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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM