spring-boot integrated freemarker

 

1. Add freemarker dependency to pom.xml file

		<!-- Spring Boot Freemarker dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>  

 

2, placed in application.yml freemarker

spring:
#  mvc:
#    view:
#      prefix: /WEB-INF/jsp/
#      suffix: .jsp
  freemarker:
    template-loader-path:
    - /WEB-INF/ftl/
    suffix: .html
    charset: utf-8
    content-type: text/html

   At this point, the configuration is complete, and the following is the sample code.

 

3. New entity class User and controller UserController

package com.huatech.domain;

import java.util.Date;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

public class User {
	
	private Long id;
	
	// Date type output to page format  
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")    
    // page string formatted as date  
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	private Date createTime;
    @Length(min = 2, max = 10, message = "Username length is between {min} and {max}")
	private String username;
    @Email(message = "Email format error")
	private String email;
	private String remark;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", createTime=" + createTime + ", username=" + username + ", email=" + email
				+ ", remark=" + remark + "]";
	}
	
}

 

package com.huatech.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huatech.domain.User;
import com.huatech.service.UserService;

@Controller
public class UserController {
	
	@Autowired private UserService userService;

	@GetMapping(value="/user/addPage")
	public String addPage(Model model){
		model.addAttribute("user", userService.get(21L));
		return "user/addPage";
	}
	
	@PostMapping(value="/user/doAdd")
	@ResponseBody
	public Object doAdd(@Valid User user, BindingResult bindingResult){
		
		if(bindingResult.hasErrors()){
			StringBuffer sb = new StringBuffer();
			List<ObjectError> errors = bindingResult.getAllErrors();
			for (ObjectError objectError : errors) {
				sb.append(objectError.getDefaultMessage()).append("<br>");
			}
			return sb.toString();			
		}
		userService.insert(user);
		return user;
	}
	
	
	
	
}

 

4. Create a new addPage.html under /WEB-INF/ftl/user

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>Add user information</h3>
	<form action="/user/doAdd" method="post">
		<span>姓名</span><input name="username"/><br/>
		<span>邮箱</span><input name="email"/><br/>
		<span>Add time</span><input name="createTime" value="2017-12-26 19:24:00"/><br/>
		<span>备注</span><input name="remark"/><br/>		
		<input type="submit"/>
	</form>
	
	
	<h3>User Details</h3>
	<table>
		<tr><td>主键</td><td>${user.id}</td></tr>
		<tr><td>姓名</td><td>${user.username}</td></tr>
		<tr><td>邮箱</td><td>${user.email}</td></tr>
		<tr><td>添加时间</td><td>${user.createTime?string('yyyy-MM-dd HH:mm:ss')}</td></tr>
	</table>
	 

</body>
</html>

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081613&siteId=291194637