spring-boot集成freemarker

阅读更多

 

1、pom.xml文件中添加freemarker依赖

		
        
            org.springframework.boot
            spring-boot-starter-freemarker
          

 

2、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

   到此配置完成,以下为示例代码。

 

3、新建实体类User及控制器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;
	
	// 日期类型输出到页面格式  
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")    
    // 页面字符串格式化为日期  
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	private Date createTime;
    @Length(min = 2, max = 10, message = "用户名长度在{min}到{max}之间")
	private String username;
    @Email(message = "邮箱格式错误")
	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 errors = bindingResult.getAllErrors();
			for (ObjectError objectError : errors) {
				sb.append(objectError.getDefaultMessage()).append("
"); } return sb.toString(); } userService.insert(user); return user; } }

 

4、在/WEB-INF/ftl/user下新建addPage.html





Insert title here


	

新增用户信息

姓名
邮箱
添加时间
备注

用户详情

主键${user.id}
姓名${user.username}
邮箱${user.email}
添加时间${user.createTime?string('yyyy-MM-dd HH:mm:ss')}

 

 

你可能感兴趣的:(spring-boot,freemarker)