一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 基于Java的Spring框架來操作FreeMarker模板的示例

基于Java的Spring框架來操作FreeMarker模板的示例

2020-04-07 11:45cxl2012 JAVA教程

這篇文章主要介紹了基于Java的Spring框架來操作FreeMarker模板的示例,講到了用于進行web模板文件的插值操作等例子,需要的朋友可以參考下

1、通過String來創建模版對象,并執行插值處理
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import freemarker.template.Template;
 
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
 
/**
* Freemarker最簡單的例子
*
* @author leizhimin 11-11-17 上午10:32
*/
public class Test2 {
    public static void main(String[] args) throws Exception{
        //創建一個模版對象
        Template t = new Template(null, new StringReader("用戶名:${user};URL:  ${url};姓名:  ${name}"), null);
        //創建插值的Map
        Map map = new HashMap();
        map.put("user", "lavasoft");
        map.put("url", "http://www.baidu.com/");
        map.put("name", "百度");
        //執行插值,并輸出到指定的輸出流中
        t.process(map, new OutputStreamWriter(System.out));
    }
}

執行后,控制臺輸出結果:

?
1
2
3
4
5
6
用戶名:lavasoft;
 
URL:  http://www.baidu.com/;
 
姓名:  百度
Process finished with exit code 0

 
 
2、通過文件來創建模版對象,并執行插值操作
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import freemarker.template.Configuration;
import freemarker.template.Template;
 
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
 
/**
* Freemarker最簡單的例子
*
* @author leizhimin 11-11-14 下午2:44
*/
public class Test {
    private Configuration cfg;      //模版配置對象
 
    public void init() throws Exception {
        //初始化FreeMarker配置
        //創建一個Configuration實例
        cfg = new Configuration();
        //設置FreeMarker的模版文件夾位置
        cfg.setDirectoryForTemplateLoading(new File("G:\\testprojects\\freemarkertest\\src"));
    }
 
    public void process() throws Exception {
        //構造填充數據的Map
        Map map = new HashMap();
        map.put("user", "lavasoft");
        map.put("url", "http://www.baidu.com/");
        map.put("name", "百度");
        //創建模版對象
        Template t = cfg.getTemplate("test.ftl");
        //在模版上執行插值操作,并輸出到制定的輸出流中
        t.process(map, new OutputStreamWriter(System.out));
    }
 
    public static void main(String[] args) throws Exception {
        Test hf = new Test();
        hf.init();
        hf.process();
    }
}

 
創建模版文件test.ftl

?
1
2
3
4
5
6
7
8
9
10
<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Welcome ${user}!</h1>
  <p>Our latest product:
  <a href="${url}">${name}</a>!
</body>
</html>
?
1
2
3
4
尊敬的用戶你好:
用戶名:${user};
URL:  ${url};
姓名:  ${name}

 
執行后,控制臺輸出結果如下:

?
1
2
3
4
5
6
7
8
9
10
<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Welcome lavasoft!</h1>
  <p>Our latest product:
  <a href="http://www.baidu.com/">百度</a>!
</body>
</html>

尊敬的用戶你好:

?
1
2
3
4
用戶名:lavasoft;
URL:  http://www.baidu.com/;
姓名:  百度
Process finished with exit code 0

 


3.基于注解的Spring+freemarker實例
web項目圖

基于Java的Spring框架來操作FreeMarker模板的示例

web.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <servlet>
  <!-- 配置DispatcherServlet -->
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 指定spring mvc配置文件位置 不指定使用默認情況 -->
   <init-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    <!--默認:/WEB-INF/<servlet-name>-servlet.xml
    classpath方式:<param-value>classpath:/spring-xml/*.xml</param-value>  
     -->  
    </init-param>
  <!-- 設置啟動順序 -->
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- 配置映射 servlet-name和DispatcherServlet的servlet一致 -->
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern><!-- 攔截以/所有請求 -->
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

 
springmvc-servlet.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd">
 
   <!-- 默認注解映射支持 -->
   <mvc:annotation-driven/> 
   <!-- 自動掃描包 -->
   <context:component-scan base-package="com.spring.freemarker" />
    
   <!--<context:annotation-config /> 配置自動掃描包配置此配置可省略-->  
   <!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 配置自動掃描包配置此配置可省略/>-->
   <!-- 配置freeMarker的模板路徑 -->
   <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="WEB-INF/ftl/" />
    <property name="defaultEncoding" value="UTF-8" />
   </bean>
   <!-- freemarker視圖解析器 -->
   <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="suffix" value=".ftl" />
    <property name="contentType" value="text/html;charset=UTF-8" />
    <!-- 此變量值為pageContext.request, 頁面使用方法:rc.contextPath -->
    <property name="requestContextAttribute" value="rc" />
   </bean>
</beans>

 
 FreeMarkerController類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.spring.freemarker;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import com.spring.vo.User;
 
@Controller
@RequestMapping("/home")
public class FreeMarkerController {
 
  @RequestMapping("/index")
  public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) {
 
    User user = new User();
    user.setUsername("zhangsan");
    user.setPassword("1234");
    List<User> users = new ArrayList<User>();
    users.add(user);
    return new ModelAndView("index", "users", users);
  }
 
}

 User類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.spring.vo;
 
public class User {
 
  private String username;
  private String password;
 
  public String getUsername() {
    return username;
  }
 
  public void setUsername(String username) {
    this.username = username;
  }
 
  public String getPassword() {
    return password;
  }
 
  public void setPassword(String password) {
    this.password = password;
  }
 
}

 
 index.ftl文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!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>Insert title here</title>
</head>
<body>
<#list users as user>
username : ${user.username}<br/>
password : ${user.password}
</#list>
</body>
</html>

 部署到tomcat,運行:http://localhost:8080/springmvc/home/index
  顯示結果:

?
1
2
username : zhangsan
password : 1234

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 闺蜜的样子小说安沁在线阅读 | 人人澡人 | 韩国日本香港毛片免费 | www在线看| 草免费视频 | 精品一区二区三区视频日产 | 美女又爽又黄免费 | 无限好资源第一片免费韩国 | 黄色wwwwww| 999任你躁在线精品免费不卡 | 古装床戏做爰无遮挡三级 | 国产农村一级特黄α真人毛片 | 欧美日韩在线一区 | 国产成人看片免费视频观看 | 女仆色在线观看 | 日本人成动漫网站在线观看 | 大逼美女 | 亚洲国产欧美另类 | 日韩一区在线播放 | 亚洲精品国产成人99久久 | 风间由美被义子中文字幕 | 99久久无色码中文字幕 | 免费欧美视频 | 国产一卡二卡3卡4卡四卡在线视频 | 2015小明台湾永久区域免费 | 四虎影音| 日韩福利一区 | 热巴在公交车h文 | 欧美日韩精品一区二区三区高清视频 | 91热国内精品永久免费观看 | 极品蜜桃臀美女啪啪 | 女同69式互添在线观看免费 | 成人影院免费看 | 欧美在线视频一区在线观看 | 四虎综合九九色九九综合色 | 亚洲欧美久久婷婷爱综合一区天堂 | 女海盗斯蒂内塔的复仇2免费观看 | 久久午夜夜伦痒痒想咳嗽P 久久无码AV亚洲精品色午夜麻豆 | 236zz宅宅最新伦理 | 91手机看片国产永久免费 | 18无删减羞羞网站动漫 |