欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年03月02日 17:27:42   作者:cxl2012  
這篇文章主要介紹了基于Java的Spring框架來操作FreeMarker模板的示例,講到了用于進(jìn)行web模板文件的插值操作等例子,需要的朋友可以參考下

1、通過String來創(chuàng)建模版對(duì)象,并執(zhí)行插值處理
 

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{ 
        //創(chuàng)建一個(gè)模版對(duì)象 
        Template t = new Template(null, new StringReader("用戶名:${user};URL:  ${url};姓名:  ${name}"), null); 
        //創(chuàng)建插值的Map 
        Map map = new HashMap(); 
        map.put("user", "lavasoft"); 
        map.put("url", "http://www.baidu.com/"); 
        map.put("name", "百度"); 
        //執(zhí)行插值,并輸出到指定的輸出流中 
        t.process(map, new OutputStreamWriter(System.out)); 
    } 
}
 

執(zhí)行后,控制臺(tái)輸出結(jié)果:

用戶名:lavasoft;

URL:  http://www.baidu.com/;

姓名:  百度 
Process finished with exit code 0

 
 
2、通過文件來創(chuàng)建模版對(duì)象,并執(zhí)行插值操作
 

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;      //模版配置對(duì)象 

    public void init() throws Exception { 
        //初始化FreeMarker配置 
        //創(chuàng)建一個(gè)Configuration實(shí)例 
        cfg = new Configuration(); 
        //設(shè)置FreeMarker的模版文件夾位置 
        cfg.setDirectoryForTemplateLoading(new File("G:\\testprojects\\freemarkertest\\src")); 
    } 

    public void process() throws Exception { 
        //構(gòu)造填充數(shù)據(jù)的Map 
        Map map = new HashMap(); 
        map.put("user", "lavasoft"); 
        map.put("url", "http://www.baidu.com/"); 
        map.put("name", "百度"); 
        //創(chuàng)建模版對(duì)象 
        Template t = cfg.getTemplate("test.ftl"); 
        //在模版上執(zhí)行插值操作,并輸出到制定的輸出流中 
        t.process(map, new OutputStreamWriter(System.out)); 
    } 

    public static void main(String[] args) throws Exception { 
        Test hf = new Test(); 
        hf.init(); 
        hf.process(); 
    } 
}

 
創(chuàng)建模版文件test.ftl

<html> 
<head> 
  <title>Welcome!</title> 
</head> 
<body> 
  <h1>Welcome ${user}!</h1> 
  <p>Our latest product: 
  <a href="${url}">${name}</a>! 
</body> 
</html> 
尊敬的用戶你好: 
用戶名:${user}; 
URL:  ${url}; 
姓名:  ${name}

 
執(zhí)行后,控制臺(tái)輸出結(jié)果如下:

<html> 
<head> 
  <title>Welcome!</title> 
</head> 
<body> 
  <h1>Welcome lavasoft!</h1> 
  <p>Our latest product: 
  <a >百度</a>! 
</body> 
</html> 

尊敬的用戶你好:

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


3.基于注解的Spring+freemarker實(shí)例
web項(xiàng)目圖

201632172236408.png (406×557)

web.xml文件

<?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配置文件位置 不指定使用默認(rèn)情況 --> 
   <init-param>   
    <param-name>contextConfigLocation</param-name>   
    <param-value>/WEB-INF/springmvc-servlet.xml</param-value> 
    <!--默認(rèn):/WEB-INF/<servlet-name>-servlet.xml 
    classpath方式:<param-value>classpath:/spring-xml/*.xml</param-value>   
     -->   
    </init-param> 
  <!-- 設(shè)置啟動(dòng)順序 --> 
  <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文件

<?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"> 
 
   <!-- 默認(rèn)注解映射支持 --> 
   <mvc:annotation-driven/>  
   <!-- 自動(dòng)掃描包 -->  
   <context:component-scan base-package="com.spring.freemarker" /> 
    
   <!--<context:annotation-config /> 配置自動(dòng)掃描包配置此配置可省略-->   
   <!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 配置自動(dòng)掃描包配置此配置可省略/>--> 
   <!-- 配置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類

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類

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文件

<!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,運(yùn)行:http://localhost:8080/springmvc/home/index
  顯示結(jié)果:

 username : zhangsan
 password : 1234

相關(guān)文章

  • 如何使用Java將word解析出來(包含格式和圖片)

    如何使用Java將word解析出來(包含格式和圖片)

    今天遇到一個(gè)讀取word模板內(nèi)容的需求,下面這篇文章主要給大家介紹了關(guān)于如何使用Java將word解析出來,包含格式和圖片,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Java排序算法之計(jì)數(shù)排序解析

    Java排序算法之計(jì)數(shù)排序解析

    這篇文章主要介紹了Java排序算法之計(jì)數(shù)排序解析,找到數(shù)組中數(shù)值最大的元素,創(chuàng)建一個(gè)長度為最大元素+1的臨時(shí)數(shù)組,這樣就可以把原始數(shù)組轉(zhuǎn)換為以原始數(shù)組元素值為下標(biāo),相同元素個(gè)數(shù)為值的臨時(shí)數(shù)組,需要的朋友可以參考下
    2023-10-10
  • DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題

    DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題

    這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java集合Map的clear與new Map區(qū)別詳解

    Java集合Map的clear與new Map區(qū)別詳解

    這篇文章主要介紹了Java集合Map的clear與new Map區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java編程之多線程死鎖與線程間通信簡單實(shí)現(xiàn)代碼

    Java編程之多線程死鎖與線程間通信簡單實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java編程之多線程死鎖與線程間通信簡單實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用

    MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用

    這篇文章主要介紹了MyBatis-Plus條件構(gòu)造器之condition參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • SpringBoot多模塊項(xiàng)目框架搭建過程解析

    SpringBoot多模塊項(xiàng)目框架搭建過程解析

    這篇文章主要介紹了SpringBoot多模塊項(xiàng)目框架搭建過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java深入探究Object類的方法

    Java深入探究Object類的方法

    java繼承中說到的Object類是java中一個(gè)特殊的類,所有的類都是直接或者間接的繼承自O(shè)bject類,即如果某個(gè)類沒有使用extends關(guān)鍵字則默認(rèn)是java.lang.Object類的子類,所以所有的類都可以使用Object類中定義的方法,下面介紹Object類的常用方法
    2022-05-05
  • 基于@PostConstruct注解的使用,解決向靜態(tài)變量注入值

    基于@PostConstruct注解的使用,解決向靜態(tài)變量注入值

    這篇文章主要介紹了基于@PostConstruct注解的使用,解決向靜態(tài)變量注入值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring P標(biāo)簽的使用詳解

    Spring P標(biāo)簽的使用詳解

    這篇文章主要介紹了Spring P標(biāo)簽的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論