Spring MVC Thymeleaf错误:模板解析期间出错

ih99xse1  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(123)

我在urlMap上遇到了一个问题,所以不要太关注我的方法内部的代码。这是我的控制器:

@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>

因此,当我试图从表(viewTable)访问表单(createForm)时,我的表单没有完全显示&我得到了以下错误:

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]")

我的代码有什么问题?

zzoitvuj

zzoitvuj1#

没有完整的例外,我只能推测。
此处的管道符号(|)似乎错误:

th:href="@{|/config/${channel.channelCode}/create|}">

th:action="|@{/config/${channel.channelCode}/create}|"

因为它们不是an valid URL的一部分,需要转义为%7C

相关问题