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

maven依赖

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
            org.springframework.boot
            spring-boot-starter-web
        

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







${name}

${stu.name}

编写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若有相对目录,则需把相对目录加上
    }

你可能感兴趣的:(spring,boot,java,后端)