Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] using th:field does not work in th:each #986

Open
ozzi- opened this issue Dec 27, 2023 · 1 comment
Open

[BUG] using th:field does not work in th:each #986

ozzi- opened this issue Dec 27, 2023 · 1 comment

Comments

@ozzi-
Copy link

ozzi- commented Dec 27, 2023

  • Version of Thymeleaf (and any involved extensions) you are using.
    spring-boot-starter-thymeleaf - 3.2.0
    spring-boot-starter-thymeleaf - 3.2.0

  • Detailed steps to reproduce your issue.
    Using technicalDescription which is populated by th:each results in an exception (see below) if used with th:field

<div th:each="reportVuln,repIterStat: ${report.vulnerabilities}">
    <textarea name="tinyMDE" th:id="${'vulnTechnicalDescription'+(repIterStat.index+1)}" th:field="${reportVuln.technicalDescription}"></textarea>
</div>

Exception:

2023-12-27T13:51:46.378+01:00 ERROR 8140 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringTextareaFieldTagProcessor' (template: "editReport" - line 35, col 91)] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'reportVuln' available as request attribute
	at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-6.1.1.jar:6.1.1]
	at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:926) ~[spring-webmvc-6.1.1.jar:6.1.1]
	at org.thymeleaf.spring6.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:232) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.spring6.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.spring6.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.spring6.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.spring6.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring6-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE]
	at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE]

Interestingly enough, if one doesn't use th:field but th:value, the exception is not thrown and the textarea will have an attribute called "value" (however to set the value of the textarea, I need to use th:field).

Here the according java part:

    @GetMapping("/editReport")
    public ModelAndView editReport(@RequestParam(value = "id") int id) {
        ModelAndView edit = new ModelAndView();
        edit.setViewName("editReport");
        Report report= db.getReports().stream().filter(r -> r.getId()==id).findFirst().orElseThrow();
        edit.addObject("report", report);
        edit.addObject("vulnerabilities",db.getVulns());
        return edit;
    }
  • Any possible workarounds you may have found.
    See th:field using the index to access the object, this works fine.
<div th:each="reportVuln,repIterStat: ${report.vulnerabilities}">
    <textarea name="tinyMDE" th:id="${'vulnTechnicalDescription'+(repIterStat.index+1)}" th:field="${report.vulnerabilities[__${repIterStat.index}__].technicalDescription}"></textarea>
</div>
@ozzi-
Copy link
Author

ozzi- commented Mar 26, 2024

:'(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant