Spring 引导和 FreeMarker

Spring Boot and FreeMarker

我正在尝试完成 Spring Boot 和 FreeMarker 集成的一个简单示例(基于我在网上找到的教程)。出于某种原因,我的观点没有被解析为 FreeMarker 模板(我认为这就是问题所在)。

在浏览器中启动时的结果只是 return TFL 视图文件的名称,即 "index"。因此正在调用控制器并 returning 字符串 "index",但似乎没有触发器可以拉入 FTL 文件本身。任何帮助将不胜感激...

我有以下配置 class,我在其中定义了视图解析器和 Free Maker 配置。

@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(true);
        resolver.setPrefix("");
        resolver.setSuffix(".ftl");
        resolver.setContentType("text/html; charset=UTF-8");
        return resolver;
    }

    @Bean
    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setConfiguration(factory.createConfiguration());
        return result;
    }
}

然后我有以下控制器:

@RestController
public class HelloController {

    /**
     * Static list of users to simulate Database
     */
    private static List<User> userList = new ArrayList<User>();

    //Initialize the list with some data for index screen
    static {
        userList.add(new User("Bill", "Gates"));
        userList.add(new User("Steve", "Jobs"));
        userList.add(new User("Larry", "Page"));
        userList.add(new User("Sergey", "Brin"));
        userList.add(new User("Larry", "Ellison"));
    }

    /**
     * Saves the static list of users in model and renders it 
     * via freemarker template.
     * 
     * @param model 
     * @return The index view (FTL)
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(@ModelAttribute("model") ModelMap model) {

        model.addAttribute("userList", userList);

        return "index";
    }

    /**
     * Add a new user into static user lists and display the 
     * same into FTL via redirect 
     * 
     * @param user
     * @return Redirect to /index page to display user list
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@ModelAttribute("user") User user) {

        if (null != user && null != user.getFirstname()
                && null != user.getLastname() && !user.getFirstname().isEmpty()
                && !user.getLastname().isEmpty()) {

            synchronized (userList) {
                userList.add(user);
            }
        }
        return "redirect:index.html";
    }
}

最后我将以下 FTL 文件存储在 "src/main/resource/templates"

<html>
<head><title>ViralPatel.net - FreeMarker Spring MVC Hello World</title>
<body>
<div id="header">
<H2>
    <a href="http://viralpatel.net"><img height="37" width="236" border="0px" src="http://viralpatel.net/blogs/wp-content/themes/vp/images/logo.png" align="left"/></a>
    FreeMarker Spring MVC Hello World
</H2>
</div>

<div id="content">

  <fieldset>
    <legend>Add User</legend>
  <form name="user" action="add.html" method="post">
    Firstname: <input type="text" name="firstname" /> <br/>
    Lastname: <input type="text" name="lastname" />   <br/>
    <input type="submit" value="   Save   " />
  </form>
  </fieldset>
  <br/>
  <table class="datatable">
    <tr>
        <th>Firstname</th>  <th>Lastname</th>
    </tr>
    <#list model["userList"] as user>
    <tr>
        <td>${user.firstname}</td> <td>${user.lastname}</td>
    </tr>
    </#list>
  </table>

</div>  
</body>
</html> 

问题是你的控制器有错误的注解。 您应该使用 @Controller 而不是 @RestController

@RestController 用来告诉你控制器发送的响应应该发送给浏览器,通常是一个映射到json的对象。 它与添加@ResponseBody 相同。

虽然你刚刚得到了答案。但是,您的 post 有两点。

首先,在 Spring 引导中配置 Freemarker 模板非常简单。无需使用 WebMvcConfigurerAdapter。您只需要将您的属性放在 class 路径中,内容如下

spring.freemarker.template-loader-path: /templates
spring.freemarker.suffix: .ftl

其次,@Controller用于注解classes为Spring MVC Controller。 @RestController 注释 classes 与 @Controller 相同,但处理程序方法上的 @ResponseBody 是隐含的。所以你必须在你的情况下使用@Controller。

从 post Spring Boot FreeMarker Hello World Example

中找到