thymeleaf / thymeleaf

Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
http://www.thymeleaf.org
Apache License 2.0
2.79k stars 502 forks source link

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

Open ozzi- opened 8 months ago

ozzi- commented 8 months ago

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'reportVuln' available as request attribute at org.springframework.web.servlet.support.BindStatus.(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:
```java
    @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;
    }
ozzi- commented 5 months ago

:'(