spring mvc + freemarker 整合
生活随笔
收集整理的這篇文章主要介紹了
spring mvc + freemarker 整合
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/templates/" /><property name="suffix" value=".ftl" /></bean>--><mvc:annotation-driven /><context:component-scan base-package="com.bingco" /><!--配置freemarker視圖解析器 --><bean id="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/templates/" /></bean><bean id="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="suffix" value=".ftl" /><property name="contentType" value="text/html; charset=UTF-8" /></bean></beans>
?
?
這是單獨(dú)使用的Demo:
@GetMapping("/getHtml")public String method2() throws IOException, TemplateException {Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);configuration.setDirectoryForTemplateLoading(Paths.get(templates_path + File.separator).toFile());configuration.setDefaultEncoding("UTF-8");configuration.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);String tempName = "test.ftl";Template template = configuration.getTemplate(tempName);Map<String, Object> map = new HashMap<>();map.put("message", "Hello World Java !");File file = new File("D:/temp/" + tempName.substring(0, tempName.indexOf(".") + 1) + "html");if (!file.exists()) {try (Writer out = new OutputStreamWriter(new FileOutputStream(file))) {template.process(map, out);out.flush();System.out.println("\t-- over --");}}return "redirect:/index.jsp";}?
轉(zhuǎn)載于:https://www.cnblogs.com/bingco/p/8716568.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的spring mvc + freemarker 整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Elasticsearch启动报错:Ex
- 下一篇: Python pandas中DataFr