spring-boot集成freemarker
程序员文章站
2022-07-13 09:03:12
...
1、pom.xml文件中添加freemarker依赖
<!-- Spring Boot Freemarker 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
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<ObjectError> errors = bindingResult.getAllErrors(); for (ObjectError objectError : errors) { sb.append(objectError.getDefaultMessage()).append("<br>"); } return sb.toString(); } userService.insert(user); return user; } }
4、在/WEB-INF/ftl/user下新建addPage.html
<!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>新增用户信息</h3> <form action="/user/doAdd" method="post"> <span>姓名</span><input name="username"/><br/> <span>邮箱</span><input name="email"/><br/> <span>添加时间</span><input name="createTime" value="2017-12-26 19:24:00"/><br/> <span>备注</span><input name="remark"/><br/> <input type="submit"/> </form> <h3>用户详情</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>
上一篇: 基于httpclient4.5.3的httpsUtil
下一篇: java进程CPU过高问题定位
推荐阅读
-
华为官宣麒麟990:9月6日德国IFA发布、集成达芬奇架构新NPU
-
ABP 结合 MongoDB 集成依赖注入
-
三星发布Exynos 980:旗下首款集成5G基带手机SoC、8nm A77架构
-
高通官宣!骁龙7系处理器将集成5G基带 12家厂商齐发
-
对标高通Adreno!三星Exynos 9925曝光:集成AMD GPU
-
台积电代工 高通骁龙855命名确认:7nm工艺/集成NPU
-
spring boot 2 集成JWT实现api接口认证
-
Spring Cloud系列-Zuul网关集成JWT身份验证
-
Jenkins + Docker + dockerfile-maven-plugin + Harbor CI/CD spring-boot项目的最轻量级配置
-
在小程序中集成redux/immutable/thunk第三方库的方法