You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Thymeleaf模板解析错误:跳转创建表单页面异常求助

问题:Thymeleaf模板解析错误导致创建表单页面无法正常显示

问题背景

我遇到了URL映射相关问题,无需过多关注方法内部代码。

Controller代码

@Autowired
ChannelService channelService;

@Autowired
RuleService ruleService;

@GetMapping("/config/{channelCode}/view/{ruleCode}")
public String viewTable(@PathVariable String channelCode, @PathVariable String ruleCode, Authentication  authentication, ModelMap model) {
    UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
    ChannelDto channelDto = channelService.findByClientIdAndChannelCode(userPrincipal.getUser().getClient().getId(), channelCode);
    List<RuleStoreDto> storeDtos = ruleService.findAllCustomRuleStoreByClientIdAndChannelCode(userPrincipal.getUser().getClient().getId(), channelCode);
    model.addAttribute("channel", channelDto);
    model.addAttribute("stores", storeDtos);
    return "configuration/sales_channel_view";
}

@GetMapping("/config/{channelCode}/create")
public String createForm(@PathVariable String channelCode, Authentication authentication, ModelMap model) {     
    UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
    ChannelDto channelDto = channelService.findByClientIdAndChannelCode(userPrincipal.getUser().getClient().getId(), channelCode);
    model.addAttribute("channel", channelDto);
    return "configuration/sales_channel_create";
}
    
@RequestMapping(value="/config/{channelCode}/create", method=RequestMethod.POST)
public String postForm(
    @Valid @ModelAttribute("createForm") RuleStoreDto form, @PathVariable String channelCode, BindingResult result, Authentication  authentication, ModelMap model) {

    UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
    ChannelDto channelDto = channelService.findByClientIdAndChannelCode(userPrincipal.getUser().getClient().getId(), form.getChannelCode());
    model.addAttribute("channel", channelDto);
    
    try {
        form.setClientId(userPrincipal.getUser().getClient().getId());
        if(result.hasErrors()) {
            model.addAttribute("createForm", form);
            return "configuration/sales_channel_create";
        }
        customRuleService.save(form);
    }catch(Exception e) {
        model.addAttribute("err", e.getClass().getSimpleName());
        model.addAttribute("createForm", form);
        return "configuration/sales_channel_create";
    }
    return "configuration/sales_channel_create";
}

列表页跳转按钮代码(sales_channel_view.html)

<div class="btn-group">
   <a class="btn btn-success"
      th:href="@{|/config/${channel.channelCode}/create|}">
      <i class="fa fa-plus">&nbsp; Create New</i>
   </a>
</div>

创建表单页面代码片段(sales_channel_create.html)

<form id="demo-form2" th:action="|@{/config/${channel.channelCode}/create}|" th:object="${createForm}" method="post" data-parsley-validate class="form-horizontal form-label-left">
   //stuff
</form>

错误信息

ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8080-exec-4] Exception processing template "configuration/sales_channel_create": An error happened during template parsing (template: "class path resource [templates/configuration/sales_channel_create.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/configuration/sales_channel_create.html]")

问题分析与修复方案

核心问题

模板解析失败的直接原因是:GET请求跳转到创建表单页面时,Model中没有初始化createForm对象。

表单代码里用了th:object="${createForm}"绑定对象,但@GetMapping("/config/{channelCode}/create")对应的方法只往Model里添加了channel,没有添加createForm,Thymeleaf找不到这个绑定对象就会抛出模板解析异常。

修复步骤

修改GET请求的createForm方法,初始化createForm对象并添加到Model:

@GetMapping("/config/{channelCode}/create")
public String createForm(@PathVariable String channelCode, Authentication authentication, ModelMap model) {     
    UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
    ChannelDto channelDto = channelService.findByClientIdAndChannelCode(userPrincipal.getUser().getClient().getId(), channelCode);
    model.addAttribute("channel", channelDto);
    // 初始化createForm对象并放入Model
    model.addAttribute("createForm", new RuleStoreDto());
    return "configuration/sales_channel_create";
}

额外检查点

  1. 确保RuleStoreDto类有无参构造函数,否则初始化对象时会报错;
  2. 表单内的字段名称要和RuleStoreDto的属性名完全匹配,避免后续提交时出现参数绑定错误;
  3. 若模板中还有其他依赖Model对象的代码,也要确认这些对象在GET请求时都已初始化。

内容的提问来源于stack exchange,提问作者Ernest Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:40:34