FreaMarker入門教程,環境整合到使用例項
一、spring整合FreaMarker
1、下載freemarker.jar
2、配置web.xml
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
此處需要注意自己的架包版本hibernate3、hibernate4
3、配置spring.xml
<!-- Freemarker配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
<!--freemarker檢視直譯器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="suffix" value=".ftl"/>
<property name="cache" value="true" />
<property name="order" value="0" />
</bean>
<!--springmvc普通檢視直譯器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
4、靜態頁面生產
模版hello.ftl
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${title}</title>
</head>
<body>
${content}
</body>
</html>
控制器
@RequestMapping("/hello")
public ModelAndView handleRequest(HttpServletRequest hsr
, HttpServletResponse hsr1) throws Exception {
ModelAndView mv = new ModelAndView("hello");
mv.addObject("title", "Spring MVC And Freemarker");
mv.addObject("content", " Hello world , test my first spring mvc ! ");
Map<String, Object> root = new HashMap<String, Object>();
root.put("title", "Spring MVC And Freemarker");
root.put("content", "Hello world , test my first spring mvc !");
String templatesPath = "E:/xkm/JKD/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/b/WEB-INF/views";
String templateName = "/hello.ftl";
String fileName = templatesPath + "/hello.html";
FileUtil.analysisTemplate(templatesPath, templateName, fileName, root);
return mv;
}
5、直接返回模版頁面
ModelAndView mv = new ModelAndView("hello");
mv.addObject("title", "Spring MVC And Freemarker");
mv.addObject("content", " Hello world , test my first spring mvc ! ");
return mv;
相關文章
- Web Components 入門例項教程Web
- gitbook 入門教程之環境要求Git
- Websocet 入門例項Web
- Flutter 入門例項Flutter
- TypeScript入門例項TypeScript
- SoapUI入門例項UI
- Flutter入門教程(二)開發環境搭建Flutter開發環境
- Django+MySQL 例項入門DjangoMySql
- Vue專案入門例項Vue
- opengl簡單入門例項
- Ionic2入門教程(一)安裝和環境配置
- 使用Falcosidekick將執行時安全整合到現有環境中IDE
- 【Oracle】ASM例項安裝入門OracleASM
- MyBatis基於Maven入門例項MyBatisMaven
- React 入門-寫個 TodoList 例項React
- Python——astroplan庫入門例項(二)PythonAST
- ARM開發環境入門開發環境
- SpringMVC入門與環境搭建SpringMVC
- Meilisearch 快速入門(Windows 環境)Windows
- golang 入門之環境搭建Golang
- Spark程式設計環境搭建及WordCount例項Spark程式設計
- Python爬蟲入門教程 1-100 CentOS環境安裝Python爬蟲CentOS
- 使用React搭建初始化環境(React入門)React
- Docker 入門(Mac環境)-part 1 入門基本操作DockerMac
- C#快速入門教程(8)——整數C#
- Python入門基礎知識例項,Python
- Android入門教程 | RecyclerView使用入門AndroidView
- 一個案例入門補環境
- Elasticsearch快速入門和環境搭建Elasticsearch
- ORACLE-LINUX環境字元介面單例項安裝OracleLinux字元單例
- Kubernetes入門,使用minikube 搭建本地k8s 環境K8S
- Java例項教程Java
- ORM 例項教程ORM
- 使用原生js實現選項卡功能例項教程JS
- Maven例項講解教程,從零開始學Maven,帶你快速入門!Maven
- ActiveMQ入門系列二:入門程式碼例項(點對點模式)MQ模式
- 雲容器例項服務入門必讀
- iOS架構入門 - MVC模式例項演示iOS架構MVC模式
- 超級簡單入門vuex 小例項Vue