Spring Boot和FreeMarker

时间:2015-07-07 09:04:28

标签: spring freemarker

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

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

我有以下配置类,我定义了视图解析器和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文件存储在&#34; src / main / resource / templates&#34;

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

2 个答案:

答案 0 :(得分:13)

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

@RestController用于告知从控制器发送的响应应该发送到浏览器,通常是映射到json的对象。 它与添加@ResponseBody相同。

答案 1 :(得分:2)

虽然你得到了答案。但是,你的帖子有两点。

首先,在Spring Boot中配置Freemarker模板非常简单。无需使用WebMvcConfigurerAdapter。您只需将属性放在类路径上,其中包含以下内容

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

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

从帖子Spring Boot FreeMarker Hello World Example

中找到此信息