Search code examples
javahtmlspring-bootspring-securitythymeleaf

Thymeleaf throws an error when outputting the object id to the template


I have a object format, when I try to output to the id value, thymeleaf throws an error. Spring Security dependency is also enabled.

Template code

<label th:each = "format : ${Format}" th:attr="for='label' + ${format.title}" class="btn_order">
    <sup th:text = "${format.title}"/>
    <sup th:text = "${format.price}"/>
    <input class = "radio_settings" name = "price_reload" th:value="${format.id}"   th:attr="id='label' + ${format.title}" type="radio" hidden>
</label>

Entity Format

@Entity
@Table(name = "Format")
public class Format {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 50)
    private String title;

    @Column(nullable = false)
    private Integer lenght;
    
    @Column(nullable = false)
    private Integer width;

    @Column(nullable = false)
    private Integer height;

    @Column(nullable = false)
    private Integer weight;

    @Column(nullable = false)
    private Float price;

Controller

@GetMapping()
    public String getWindowOrder(Model model){
        model.addAttribute("title","Create order");
        model.addAttribute("Format", formatService.getByAll());
        return "window_order";
    }

Service

@Service
public class FormatService {

@Autowired
private FormatInterface iFormat;

public List<Format> getByAll (){
        return iFormat.findByAll();
    }
}

WebConfigSecurity

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .csrf(AbstractHttpConfigurer::disable)
            .authorizeHttpRequests((requests) -> requests
                .requestMatchers("/", "/registration/**",  "/create/**", "/cart" ,"/image/**", "/css/**", "/js/**","/order","/img/**").permitAll()
                .anyRequest().authenticated()
            )
            .formLogin((form) -> form
                .loginPage("/login")
                .permitAll()
            )
            .logout((logout) -> logout.permitAll());

        return http.build();
    }

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder);
    }

Error

ERROR 10344 --- [io-8080-exec-10] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-10] Exception processing template "window_cart": An error happened during template parsing (template: "class path resource [templates/window_order.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/window_order.html]")

All other class data is displayed correctly. If you remove th:value = "${format.id}" , then the template is loaded.


Solution

  • I found the problem, the point is that you need to address the id with capital characters

    <div class="settings_format">
      <label th:each = "format : ${Format}"           th:attr="for=${format.ID}" class="btn_order">
        <sup th:text = "${format.title}"/>
        <input class = "radio_settings" name = "format_id" th:attr="value=${format.ID},id=${format.ID}" type="radio" hidden>
      </label>
    </div>