Spring Thymeleaf 对于两个不同的请求返回相同的主体
我有 2 个用户,有 2 个请求,它们具有相同的响应模板,但具有不同的数据。
如果我尝试同时处理两个请求,它会为两个请求返回相同的数据。我已禁用所有缓存并使用调试标头。标头不同,但主体相同。所有日志显示 2 个请求已成功处理,并且选择了正确的内容。
但它仍然在浏览器中显示相同的数据。
Thymeleaf 配置:
@Configuration
public class ThymeleafConfiguration{
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(1);
templateResolver.setCheckExistence(true);
templateEngine.addTemplateResolver(templateResolver);
templateEngine.addDialect(new Java8TimeDialect());
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
}
控制器:
@GetMapping
public String index(
HttpServletRequest request, Model model,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "status", required = false) Integer status,
@RequestParam(name = "fileName", required = false) String fileName
) {
try {
loadUser(request);
Pageable pageable = PageRequest.of(page, size,
Sort.by(Sort.Direction.DESC, "updatedAt"));
RequestFile.Status rStatus = RequestFile.Status.ofValue(status);
Page<RequestFile> requestFiles = requestFileService.findAll(fileName,
rStatus, null, null, currentUser.getBranch(), null, pageable);
model.addAttribute("requestFiles", requestFiles);
model.addAttribute("currentUser", currentUser);
model.addAttribute("status", status);
model.addAttribute("fileName", fileName);
model.addAttribute("curr", page);
return "index";
} catch (Exception e) {
log.info("Error: " + e.getMessage());
e.printStackTrace();
throw e;
}
}
I have 2 users with 2 requests which have the same template for the response, but which have different data.
If I try to process both requests at the same time, it returns the same data for both requests. I have disabled all caches and used a debug header. The headers are different but the bodies are the same. All the logs show that 2 requests are processed successfully, with the right content selected.
But it still displays identical data in the browser.
The Thymeleaf configuration:
@Configuration
public class ThymeleafConfiguration{
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(1);
templateResolver.setCheckExistence(true);
templateEngine.addTemplateResolver(templateResolver);
templateEngine.addDialect(new Java8TimeDialect());
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
}
The controller:
@GetMapping
public String index(
HttpServletRequest request, Model model,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "status", required = false) Integer status,
@RequestParam(name = "fileName", required = false) String fileName
) {
try {
loadUser(request);
Pageable pageable = PageRequest.of(page, size,
Sort.by(Sort.Direction.DESC, "updatedAt"));
RequestFile.Status rStatus = RequestFile.Status.ofValue(status);
Page<RequestFile> requestFiles = requestFileService.findAll(fileName,
rStatus, null, null, currentUser.getBranch(), null, pageable);
model.addAttribute("requestFiles", requestFiles);
model.addAttribute("currentUser", currentUser);
model.addAttribute("status", status);
model.addAttribute("fileName", fileName);
model.addAttribute("curr", page);
return "index";
} catch (Exception e) {
log.info("Error: " + e.getMessage());
e.printStackTrace();
throw e;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论