标签:response prefix div value 路径问题 load macro resource 2.x
准备好的环境:Maven工程整合好了ssm,即spring+springMVC+mybatis。接下来准备将springMVC与freemarker整合,以html文件为模板。
一,加入freemarker依赖
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
二,在web.xml中的前端控制器选择加载mvc-context-freemarker.xml文件
<!-- Spring Servlet配置 -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/spring/mvc-context-freemarker.xml,
classpath*:/spring/controller/*
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
三,编辑mvc-context-freemarker.xm文件,注意标红部分。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <!-- 控制器适配器和控制器映射器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置jsp文件视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" />
<!-- 视图渲染顺序 --> <property name="order" value="1" /> </bean> <!-- 配置freeMarker视图解析器 --> <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <property name="contentType" value="text/html; charset=UTF-8"/> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> <property name="cache" value="true" />
<!-- 不需要写前缀 --> <property name="suffix" value=".html" /> <property name="order" value="0"/> </bean> <!-- 配置freeMarker的模板路径 --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- freemarker前缀 --> <property name="templateLoaderPath" value="/html/"/> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> <property name="defaultEncoding" value="UTF-8"/> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/> <!-- 多部分文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="104857600" /> <property name="maxInMemorySize" value="4096" /> <property name="defaultEncoding" value="UTF-8"></property> </bean>
</beans>
四,编辑Controller
@RequestMapping("/doLogin")
public String doLogin(HttpServletRequest req, HttpServletResponse res, Model model) {
model.addAttribute("name", "czx");
return "hello";
}
五,编辑Hello.html模板文件,注意存放路径问题
<html>
<head>
<title>freemarker Test</title>
</head>
<body>
<h1>Hello,${name}</h1>
</body>
</html>
结果:
标签:response prefix div value 路径问题 load macro resource 2.x
原文地址:https://www.cnblogs.com/caozx/p/10732509.html