Spring Boot 和FreeMarker

a7qyws3x  于 2022-12-21  发布在  Spring
关注(0)|答案(2)|浏览(111)

我尝试通过一个简单的Sping Boot 和FreeMarker集成的例子(基于我在网上找到的教程),由于某种原因,我的视图没有被解析到FreeMarker模板(我认为这是问题所在)。
在浏览器中启动时的结果只是返回TFL视图文件的名称iIndieE.“Index”。因此控制器正在被调用并返回字符串“Index”,但似乎没有触发器拉入FTL文件本身。任何帮助都将不胜感激...
我有下面的配置类,我在其中定义了视图解析器和免费制造商配置。

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

avwztpqn1#

问题是你的控制器有错误的注解,你应该使用@Controller代替@RestController
@RestController用来告诉你的控制器发来的响应应该发到浏览器,通常是一个Map到json的对象,和添加@ResponseBody是一样的。

cbwuti44

cbwuti442#

虽然你刚刚得到了答案。但是,你的帖子有两点。
首先,在Sping Boot 中配置Freemarker模板非常简单。不需要使用WebMvcConfigurerAdapter。您只需要将您的属性放在您的类路径中,内容如下

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

第二,@Controller被用来标注类作为Spring MVC控制器。@RestController标注的类与@Controller相同,但是处理程序方法上的@ResponseBody是隐含的。所以在您的情况下必须使用@Controller。
从帖子Spring Boot FreeMarker Hello World Example找到这个

相关问题