使用场景:前端写好HTML模版,后台使用Thymeleaf模版引擎技术为每一个用户渲染各自的成长报告
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置类:
package cn.bjsytx.phsh.evaluation.config;
import com.aliyun.oss.OSSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import java.nio.charset.StandardCharsets;
@Configuration
public class ThymeleafConfig {
@Value("${file.oss.access-key}")
private String accessKeyId;
@Value("${file.oss.secret-key}")
private String accessKeySecret;
@Value("${file.bucketName}")
private String bucket;
@Value("${file.oss.endpoint}")
private String endPoint;
@Bean
public OSSClient ossClient() {
return new OSSClient(endPoint, accessKeyId, accessKeySecret);
}
// Thymeleaf配置
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("https://" + bucket + ".oss-cn-shanghai.aliyuncs.com/"); // 设置模板文件的URL地址
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
return viewResolver;
}
}
yml文件配置
spring:
thymeleaf:
#缓冲的配置
cache: false
check-template: true
check-template-location: true
#开启MVC thymeleaf 视图解析
enabled: true
encoding: utf-8
mode: HTML5
接口代码
@RequestMapping("/")
@Inner(value = false)
public String generReport(Model model, Long tempId, Long userId, Long projectId, Long roundId) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Y年M月d日");
ProductReportTemp temp = productReportTempService.getById(tempId);
HashMap<String, Object> map = new HashMap<>();
//基本信息
ProjectUser projectUser = remoteProjectUserService.getByUserAndProjectForInner(userId, projectId, SecurityConstants.FROM_IN);
map.put("userName", projectUser.getName());
map.put("avtar", projectUser.getAvtar());
SysUser sysUser = remoteUserService.getUserById(userId, SecurityConstants.FROM_IN);
map.put("avtar", sysUser.getPhoto());
String format = LocalDate.now().format(formatter);
map.put("time", format);
Project project = remoteProjectService.innerGetById(projectId, SecurityConstants.FROM_IN);
BasicInformationVO vo = evaluationProductDataService.evaluationProductStatistics(project.getProjectProductId(), roundId, userId);
map.put("testTime", vo.getTestTime().format(formatter));
map.put("allModule", vo.getAllModule());
map.put("completionProduct", vo.getCompletionProduct());
map.put("productName", vo.getProductName());
map.put("takeTime", vo.getTakeTime());
List<PlateVO> plateVOList = evaluationProductDataService.evaluatingStatistics(project.getProjectProductId(), roundId, userId);
map.put("PlateVOList", plateVOList);
String platVoNames = plateVOList.stream().map(PlateVO::getPlateName).collect(Collectors.joining("、"));
map.put("platVoNames", platVoNames);
List<DataList> arrayList = new ArrayList<>();
for (PlateVO plateVO : plateVOList) {
DataList list = new DataList();
list.setMax(plateVO.getPlateMaxScore().toString());
list.setName(plateVO.getPlateName());
arrayList.add(list);
}
map.put("dataList", arrayList);
List<BigDecimal> collect = plateVOList.stream().map(PlateVO::getUserScoreValue).collect(Collectors.toList());
map.put("oneList", collect);
map.put("userName", projectUser.getName());
String attachment = temp.getAttachment();
String[] split = attachment.split("/");
model.addAllAttributes(map);
return split[split.length - 1];
}