Thymeleaf输出Format对象id时抛出模板解析异常求助
问题描述
项目引入Spring Security依赖,使用Thymeleaf模板渲染Format实体对象时,title、price等属性均可正常输出,但只要添加th:value="${format.id}"就会抛出模板解析异常,移除该代码后模板可正常加载。
模板代码
<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>
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; // 原代码未提供id及其他属性的getter/setter方法 }
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(); } }
Spring Security配置代码
@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 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]")
解决方案
核心原因
Thymeleaf默认通过getter方法访问对象属性,而非直接读取类的字段。你的Format实体类未提供id属性的getter方法,导致模板无法解析该属性。
修复步骤
为Format实体添加getter/setter方法
给id及所有需要在模板中访问的属性添加getter方法,推荐用Lombok的@Data注解自动生成,也可以手动编写:@Entity @Table(name = "Format") @Data // 自动生成所有属性的getter/setter、toString等方法 public class Format { // 原有字段... }手动编写的话,至少添加id的getter:
public Long getId() { return id; } public void setId(Long id) { this.id = id; }可选:显式类型转换(若仍有问题)
若添加getter后仍存在Long转String的类型问题,可在模板中显式转换:<input th:value="${#strings.toString(format.id)}" ... />或者直接调用对象的toString方法:
<input th:value="${format.id.toString()}" ... />
验证
添加getter方法后重启项目,重新访问页面,模板应能正常渲染format.id的值。
内容的提问来源于stack exchange,提问作者FIDSTONE
相关产品推荐
相关产品推荐

