{"id":218254,"date":"2021-05-22T09:57:52","date_gmt":"2021-05-22T01:57:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=218254"},"modified":"2021-05-05T15:58:46","modified_gmt":"2021-05-05T07:58:46","slug":"springboot-integrates-freemarker","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/springboot-integrates-freemarker.html","title":{"rendered":"Spring Boot \u6574\u5408 Freemarker"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u662f\u4e00\u4e2a\u76f8\u5f53\u8001\u724c\u7684\u5f00\u6e90\u7684\u514d\u8d39\u7684\u6a21\u7248\u5f15\u64ce\u3002\u901a\u8fc7 Freemarker \u6a21\u7248\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u6e32\u67d3\u6210 HTML \u7f51\u9875\u3001\u7535\u5b50\u90ae\u4ef6\u3001\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u6e90\u4ee3\u7801\u7b49\u3002Freemarker \u4e0d\u662f\u9762\u5411\u6700\u7ec8\u7528\u6237\u7684\uff0c\u800c\u662f\u4e00\u4e2a Java \u7c7b\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e4b\u4f5c\u4e3a\u4e00\u4e2a\u666e\u901a\u7684\u7ec4\u4ef6\u5d4c\u5165\u5230\u6211\u4eec\u7684\u4ea7\u54c1\u4e2d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6765\u770b\u4e00\u5f20\u6765\u81ea Freemarker \u5b98\u7f51\u7684\u56fe\u7247\uff1a<\/p>\n

\"\"
\n\u53ef\u4ee5\u770b\u5230\uff0cFreemarker \u53ef\u4ee5\u5c06\u6a21\u7248\u548c\u6570\u636e\u6e32\u67d3\u6210 HTML \u3002
\nFreemarker \u6a21\u7248\u540e\u7f00\u4e3a .ftl(FreeMarker Template Language)\u3002FTL \u662f\u4e00\u79cd\u7b80\u5355\u7684\u3001\u4e13\u7528\u7684\u8bed\u8a00\uff0c\u5b83\u4e0d\u662f\u50cf Java \u90a3\u6837\u6210\u719f\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u5728\u6a21\u677f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u5982\u4f55\u5c55\u73b0\u6570\u636e\uff0c \u800c\u5728\u6a21\u677f\u4e4b\u5916\u53ef\u4ee5\u4e13\u6ce8\u4e8e\u8981\u5c55\u793a\u4ec0\u4e48\u6570\u636e\u3002<\/p>\n

\u597d\u4e86\uff0c\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ecb\u7ecd\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u770b Freemarker \u548c Spring Boot \u7684\u4e00\u4e2a\u6574\u5408\u64cd\u4f5c\u3002<\/p>\n

\u5b9e\u8df5<\/strong><\/div>\n

\u5728 SSM \u4e2d\u6574\u5408 Freemarker \uff0c\u6240\u6709\u7684\u914d\u7f6e\u6587\u4ef6\u52a0\u8d77\u6765\uff0c\u524d\u524d\u540e\u540e\u5927\u7ea6\u5728 50 \u884c\u5de6\u53f3\uff0cSpring Boot \u4e2d\u8981\u51e0\u884c\u914d\u7f6e\u5462\uff1f 0 \u884c\uff01<\/p>\n

1.\u521b\u5efa\u5de5\u7a0b<\/strong><\/span><\/div>\n

\u9996\u5148\u521b\u5efa\u4e00\u4e2a Spring Boot \u5de5\u7a0b\uff0c\u5f15\u5165 Freemarker \u4f9d\u8d56\uff1a<\/p>\n

\r\n<dependency>\r\n    <groupid>org.springframework.boot<\/groupid>\r\n    <artifactid>spring-boot-starter-freemarker<\/artifactid>\r\n<\/dependency>\r\n<dependency>\r\n    <groupid>org.springframework.boot<\/groupid>\r\n    <artifactid>spring-boot-starter-web<\/artifactid>\r\n<\/dependency>\r\n<\/pre>\n

\u5de5\u7a0b\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u5728 org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration \u7c7b\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u5173\u4e8e Freemarker \u7684\u81ea\u52a8\u5316\u914d\u7f6e\uff1a<\/p>\n

\r\n@Configuration\r\n@ConditionalOnClass({ freemarker.template.Configuration.class, FreeMarkerConfigurationFactory.class })\r\n@EnableConfigurationProperties(FreeMarkerProperties.class)\r\n@Import({ FreeMarkerServletWebConfiguration.class, FreeMarkerReactiveWebConfiguration.class,\r\n                FreeMarkerNonWebConfiguration.class })\r\npublic class FreeMarkerAutoConfiguration {\r\n}\r\n<\/pre>\n

\u4ece\u8fd9\u91cc\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53 classpath \u4e0b\u5b58\u5728 freemarker.template.Configuration \u4ee5\u53ca FreeMarkerConfigurationFactory \u65f6\uff0c\u914d\u7f6e\u624d\u4f1a\u751f\u6548\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u6211\u4eec\u5f15\u5165\u4e86 Freemarker \u4e4b\u540e\uff0c\u914d\u7f6e\u5c31\u4f1a\u751f\u6548\u3002\u4f46\u662f\u8fd9\u91cc\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u53ea\u505a\u4e86\u6a21\u677f\u4f4d\u7f6e\u68c0\u67e5\uff0c\u5176\u4ed6\u914d\u7f6e\u5219\u662f\u5728\u5bfc\u5165\u7684 FreeMarkerServletWebConfiguration \u914d\u7f6e\u4e2d\u5b8c\u6210\u7684\u3002\u90a3\u4e48\u6211\u4eec\u518d\u6765\u770b\u770b FreeMarkerServletWebConfiguration \u7c7b\uff0c\u90e8\u5206\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\n@Configuration\r\n@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)\r\n@ConditionalOnClass({ Servlet.class, FreeMarkerConfigurer.class })\r\n@AutoConfigureAfter(WebMvcAutoConfiguration.class)\r\nclass FreeMarkerServletWebConfiguration extends AbstractFreeMarkerConfiguration {\r\n        protected FreeMarkerServletWebConfiguration(FreeMarkerProperties properties) {\r\n                super(properties);\r\n        }\r\n        @Bean\r\n        @ConditionalOnMissingBean(FreeMarkerConfig.class)\r\n        public FreeMarkerConfigurer freeMarkerConfigurer() {\r\n                FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();\r\n                applyProperties(configurer);\r\n                return configurer;\r\n        }\r\n        @Bean\r\n        @ConditionalOnMissingBean(name = \"freeMarkerViewResolver\")\r\n        @ConditionalOnProperty(name = \"spring.freemarker.enabled\", matchIfMissing = true)\r\n        public FreeMarkerViewResolver freeMarkerViewResolver() {\r\n                FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();\r\n                getProperties().applyToMvcViewResolver(resolver);\r\n                return resolver;\r\n        }\r\n}\r\n<\/pre>\n

\u5728\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\uff0c\u6ce8\u5165\u4e86 FreeMarkerProperties\uff1a<\/p>\n

\r\n@ConfigurationProperties(prefix = \"spring.freemarker\")\r\npublic class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {\r\n        public static final String DEFAULT_TEMPLATE_LOADER_PATH = \"classpath:\/templates\/\";\r\n        public static final String DEFAULT_PREFIX = \"\";\r\n        public static final String DEFAULT_SUFFIX = \".ftl\";\r\n        \/**\r\n         * Well-known FreeMarker keys which are passed to FreeMarker's Configuration.\r\n         *\/\r\n        private Map settings = new HashMap<>();\r\n}\r\n<\/pre>\n

FreeMarkerProperties \u4e2d\u5219\u914d\u7f6e\u4e86 Freemarker \u7684\u57fa\u672c\u4fe1\u606f\uff0c\u4f8b\u5982\u6a21\u677f\u4f4d\u7f6e\u5728 classpath:\/templates\/ \uff0c\u518d\u4f8b\u5982\u6a21\u677f\u540e\u7f00\u4e3a .ftl\uff0c\u90a3\u4e48\u8fd9\u4e9b\u914d\u7f6e\u6211\u4eec\u4ee5\u540e\u90fd\u53ef\u4ee5\u5728 application.properties \u4e2d\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n

\u5982\u679c\u6211\u4eec\u5728 SSM \u7684 XML \u6587\u4ef6\u4e2d\u81ea\u5df1\u914d\u7f6e Freemarker \uff0c\u4e5f\u4e0d\u8fc7\u5c31\u662f\u914d\u7f6e\u8fd9\u4e9b\u4e1c\u897f\u3002\u73b0\u5728\uff0c\u8fd9\u4e9b\u914d\u7f6e\u7531 FreeMarkerServletWebConfiguration\u200b \u5e2e\u6211\u4eec\u5b8c\u6210\u4e86\u3002<\/p>\n

2.\u521b\u5efa\u7c7b<\/strong><\/span><\/div>\n

\u9996\u5148\u6211\u4eec\u6765\u521b\u5efa\u4e00\u4e2a User \u7c7b\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\npublic class User {\r\n    private Long id;\r\n    private String username;\r\n    private String address;\r\n    \/\/\u7701\u7565 getter\/setter\r\n}\r\n<\/pre>\n

\u518d\u6765\u521b\u5efa UserController\uff1a<\/p>\n

\r\n@Controller\r\npublic class UserController {\r\n    @GetMapping(\"\/index\")\r\n    public String index(Model model) {\r\n        List users = new ArrayList<>();\r\n        for (int i = 0; i < 10; i++) {\r\n            User user = new User();\r\n            user.setId((long) i);\r\n            user.setUsername(\"javaboy>>>>\" + i);\r\n            user.setAddress(\"www.javaboy.org>>>>\" + i);\r\n            users.add(user);\r\n        }\r\n        model.addAttribute(\"users\", users);\r\n        return \"index\";\r\n    }\r\n}\r\n<\/user><\/pre>\n

\u6700\u540e\u5728 freemarker \u4e2d\u6e32\u67d3\u6570\u636e\uff1a<\/p>\n

\r\n<!DOCTYPE html>\r\n<html lang="en">\r\n<head>\r\n    <meta charset="UTF-8">\r\n    <title>Title<\/title>\r\n<\/head>\r\n<body>\r\n<table border="1">\r\n    <tr>\r\n        <td>\u7528\u6237\u7f16\u53f7<\/td>\r\n        <td>\u7528\u6237\u540d\u79f0<\/td>\r\n        <td>\u7528\u6237\u5730\u5740<\/td>\r\n    <\/tr>\r\n    <#list users as user>\r\n        <tr>\r\n            <td>${user.id}<\/td>\r\n            <td>${user.username}<\/td>\r\n            <td>${user.address}<\/td>\r\n        <\/tr>\r\n    <\/#list>\r\n<\/table>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n

\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a
\n\"\"<\/p>\n

\u5176\u4ed6\u914d\u7f6e<\/strong><\/span><\/div>\n

\u5982\u679c\u6211\u4eec\u8981\u4fee\u6539\u6a21\u7248\u6587\u4ef6\u4f4d\u7f6e\u7b49\uff0c\u53ef\u4ee5\u5728 application.properties \u4e2d\u8fdb\u884c\u914d\u7f6e\uff1a<\/p>\n

\r\nspring.freemarker.allow-request-override=false\r\nspring.freemarker.allow-session-override=false\r\nspring.freemarker.cache=false\r\nspring.freemarker.charset=UTF-8\r\nspring.freemarker.check-template-location=true\r\nspring.freemarker.content-type=text\/html\r\nspring.freemarker.expose-request-attributes=false\r\nspring.freemarker.expose-session-attributes=false\r\nspring.freemarker.suffix=.ftl\r\nspring.freemarker.template-loader-path=classpath:\/templates\/\r\n<\/pre>\n

\u597d\u4e86\uff0c\u6574\u5408\u5b8c\u6210\u4e4b\u540e\uff0cFreemarker \u7684\u66f4\u591a\u7528\u6cd5\uff0c\u5c31\u548c\u5728 SSM \u4e2d\u4f7f\u7528 Freemarker \u4e00\u6837\u4e86\uff0c\u8fd9\u91cc\u6211\u5c31\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6765\u770b\u4e00\u5f20\u6765\u81ea Freemarker \u5b98\u7f51\u7684\u56fe\u7247\uff1a \u53ef\u4ee5\u770b\u5230\uff0cFreemarker \u53ef\u4ee5\u5c06\u6a21\u7248\u548c\u6570\u636e\u6e32\u67d3\u6210 HT […]<\/p>\n","protected":false},"author":1903,"featured_media":218260,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=218254"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218254\/revisions"}],"predecessor-version":[{"id":218262,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218254\/revisions\/218262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/218260"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=218254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=218254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=218254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}