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;
相關文章
- React 入門例項教程React
- 【Hibernate】—Hibernate+mysql 環境搭建+入門例項MySql
- gitbook 入門教程之環境要求Git
- Web Components 入門例項教程Web
- Flutter入門教程(二)開發環境搭建Flutter開發環境
- PHP框架Yii系列教程(一):入門例項PHP框架
- Fireworks MX 2004 入門例項教程(轉)
- TypeScript入門例項TypeScript
- Websocet 入門例項Web
- SoapUI入門例項UI
- Flutter 入門例項Flutter
- Kafka入門例項Kafka
- Struts入門例項
- scrapy入門教程1:scrapy環境配置以及安裝
- 單機環境配置ASM例項ASM
- ActiveMQ 入門及例項MQ
- Ionic2入門教程(一)安裝和環境配置
- mybatis實戰教程(一)環境配置及簡單入門MyBatis
- Rest Webservice 環境搭建流程和例項RESTWeb
- jafka環境搭建步驟--例項可用
- golang 入門之環境搭建Golang
- Meilisearch 快速入門(Windows 環境)Windows
- ARM開發環境入門開發環境
- Mule 入門之:環境搭建
- 入門(一)搭建GAE環境
- DockerSwarm入門(二)配置選項與基本執行環境要求DockerSwarm
- opengl簡單入門例項
- Django+MySQL 例項入門DjangoMySql
- Vue專案入門例項Vue
- React入門學習例項React
- Jquery入門及例項一jQuery
- QT開發快速入門-教程1:搭建QT開發環境QT開發環境
- 使用React搭建初始化環境(React入門)React
- ASM單例項(Oracle 11.2.0.4)環境(一)ASM單例Oracle
- ASM單例項(Oracle 11.2.0.4)環境(二)ASM單例Oracle
- (轉)Oracle rac環境下清除asm例項OracleASM
- 單機環境安裝配置ASM例項ASM
- Docker 入門(Mac環境)-part 1 入門基本操作DockerMac