springboot2.6.4 集成freemark(404问题,只返回模板名称问题)

maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

test.ftl (在目录resources/templates/ 文件夹下创建该文件)

<!doctype html>
<html lang="en">
<head>

</head>
<body>
<h1>${name}</h1>
<h1>${stu.name}</h1>
</body>
</html>

编写controller

@Controller
public class HelloController {

    @GetMapping("/test")
    public String test(Model model) {


        //1.纯文本形式的参数
        model.addAttribute("name", "freemarker");
        //2.实体类相关的参数

        Student student = new Student();
        student.setName("小明");
        model.addAttribute("stu", student);

        return "test";
    }

对于freemark,理论上可以不用配置,默认即可,源码配置信息

@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {

	public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";

	public static final String DEFAULT_PREFIX = "";

	public static final String DEFAULT_SUFFIX = ".ftlh";




代码块二

public abstract class AbstractTemplateViewResolverProperties extends AbstractViewResolverProperties {

	/**
	 * Prefix that gets prepended to view names when building a URL.
	 */
	private String prefix;

	/**
	 * Suffix that gets appended to view names when building a URL.
	 */
	private String suffix;

	/**
	 * Name of the RequestContext attribute for all views.
	 */
	private String requestContextAttribute;

	/**
	 * Whether all request attributes should be added to the model prior to merging with
	 * the template.
	 */
	private boolean exposeRequestAttributes = false;

	/**
	 * Whether all HttpSession attributes should be added to the model prior to merging
	 * with the template.
	 */
	private boolean exposeSessionAttributes = false;

	/**
	 * Whether HttpServletRequest attributes are allowed to override (hide) controller
	 * generated model attributes of the same name.
	 */
	private boolean allowRequestOverride = false;

	/**
	 * Whether to expose a RequestContext for use by Spring's macro library, under the
	 * name "springMacroRequestContext".
	 */
	private boolean exposeSpringMacroHelpers = true;

	/**
	 * Whether HttpSession attributes are allowed to override (hide) controller generated
	 * model attributes of the same name.
	 */
	private boolean allowSessionOverride = false;





代码块三
public abstract class AbstractViewResolverProperties {

	private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

	private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

	/**
	 * Whether to enable MVC view resolution for this technology.
	 */
	private boolean enabled = true;

	/**
	 * Whether to enable template caching.
	 */
	private boolean cache;

	/**
	 * Content-Type value.
	 */
	private MimeType contentType = DEFAULT_CONTENT_TYPE;

	/**
	 * Template encoding.
	 */
	private Charset charset = DEFAULT_CHARSET;

	/**
	 * View names that can be resolved.
	 */
	private String[] viewNames;

	/**
	 * Whether to check that the templates location exists.
	 */
	private boolean checkTemplateLocation = true;

如上,即可正常启动。

问题1、调试过程中,报404问题

查阅了其他文章,说是springboot自2.2以后,模板的后缀改为.ftlh了。看源码也确实是默认.ftlh。 但是我在命名模板为test.ftlh时,仍然报404,后修改为test.ftl 正常了

问题2:get请求过程中,返回结果类型为 test(模板名称),而非test.ftl的模板内容

原因:最开始controller的注解用的是@RestController 注解.  改为@Controller 即可。同时需要特别注意,如果test.ftl 文件在目录resources/templates/的下一级目标,比如在目录resources/templates/ftl/目录下,则controller的返回模板要携带该路径:为 

return "ftl/test"

//@RestController 该注解会直接返回 return的 字符串
@Controller //返回模板对应的内容
public class HelloController {

    @GetMapping("/test")
    public String test(Model model) {


        //1.纯文本形式的参数
        model.addAttribute("name", "freemarker");
        //2.实体类相关的参数

        Student student = new Student();
        student.setName("小明");
        model.addAttribute("stu", student);

        return "test"; //test若有相对目录,则需把相对目录加上
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值