如何在Freemarker中使用SpringBoot Validator?遇FTL模板错误求助
Hey, let's get this sorted out. That error you're seeing means the Freemarker template can't access the Spring tag library utilities, which are required for @spring.bind and @spring.showErrors to work. Here's how to fix it step by step:
1. Add Spring Freemarker Dependency (if missing)
First, make sure your project has the necessary dependency to enable Spring's Freemarker integration. If you're using Maven, add this to your pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
For Gradle, add this to your build.gradle:
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
2. Ensure Spring Exposes Tag Helpers to Freemarker
Spring Boot usually auto-configures this, but if you're still hitting issues, you can explicitly expose the spring utility object with a config bean:
@Configuration public class FreemarkerConfig { @Bean public FreeMarkerConfigurer freeMarkerConfigurer() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPath("classpath:/static/"); // Match your template directory Map<String, Object> variables = new HashMap<>(); variables.put("spring", new SpringMacroRequestContext()); configurer.setFreemarkerVariables(variables); return configurer; } }
3. Import Spring Tag Library in Your FTL
At the very top of your cust_add.ftl file, add this line to import the Spring macro library:
<#assign spring=JspTaglibs["http://www.springframework.org/tags"] />
Here's your updated FTL with this fix plus a couple of critical tweaks (like adding the name attribute to the input and pre-filling values):
<#assign spring=JspTaglibs["http://www.springframework.org/tags"] /> <form action="/crm/static/cust_check.html" class="form-ajax-file"> <table width="100%" border="0" align="center" cellpadding="4" cellspacing="4" bordercolor="#666666"> <tr> <td colspan="2" bgcolor="#eeeeee">add customer info</td> </tr> <tr> <td width="150" align="right">customerName:</td> <td> <@spring.bind "cust.name"/> <input type="text" id="name" name="name" value="${spring.status.value}" class="tr-width"/> </td> <td><@spring.showErrors ""/></td> </tr> </table> </form>
4. Fix Your Controller to Handle Validation Results
Right now, your controller isn't checking if validation failed. You need to handle the BindingResult to show errors to the user:
@RequestMapping(value = "/static/cust_add.html",method = {RequestMethod.GET,RequestMethod.POST}) public ModelAndView add(@Valid CustomerEntity cust, BindingResult bindingResult, RedirectAttributes redirectAttributes){ ModelAndView mv = new ModelAndView(); if (bindingResult.hasErrors()) { // Keep the customer object in the model to display validation errors mv.addObject("cust", cust); mv.setViewName("/static/cust_add"); return mv; } // If validation passes, proceed with your logic (e.g. save to DB) redirectAttributes.addFlashAttribute("cust", cust); mv.setViewName("redirect:/crm/static/cust_success.html"); // Redirect to success page return mv; }
5. Set Up Validation Message Source
Since you're using {notBlank} as the error message, create a messages.properties file in src/main/resources with:
notBlank=This field cannot be left blank
This ensures the validation message is properly resolved.
That should fix the "spring is null" error and get your validation working end-to-end. Let me know if you hit any other snags!
内容的提问来源于stack exchange,提问作者red0range

