Spring Thymeleaf 对于两个不同的请求返回相同的主体

发布于 2025-01-20 21:56:53 字数 2307 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文