最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Java的Spring框架來(lái)操作FreeMarker模板的示例

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

1、通過(guò)String來(lái)創(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最簡(jiǎn)單的例子 
* 
* @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、通過(guò)文件來(lái)創(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最簡(jiǎn)單的例子 
* 
* @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><!-- 攔截以/所有請(qǐng)求 --> 
 </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, 頁(yè)面使用方法: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)文章

  • 本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式

    本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式

    這篇文章主要介紹了本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 詳解Spring不同數(shù)據(jù)庫(kù)異常如何抽象的

    詳解Spring不同數(shù)據(jù)庫(kù)異常如何抽象的

    根據(jù)spring-jdbc中的定義,所有的數(shù)據(jù)操作異常都會(huì)轉(zhuǎn)換為 DataAccessException,下面這篇文章主要給大家介紹了關(guān)于Spring不同數(shù)據(jù)庫(kù)異常如何抽象的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • 解決spring boot環(huán)境切換失效的問(wèn)題

    解決spring boot環(huán)境切換失效的問(wèn)題

    這篇文章主要介紹了解決spring boot環(huán)境切換失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • maven多profile 打包下 -P參和-D參數(shù)的實(shí)現(xiàn)

    maven多profile 打包下 -P參和-D參數(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了maven多profile 打包下 -P參和-D參數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例

    java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例

    這篇文章主要介紹了java從mysql導(dǎo)出數(shù)據(jù)的具體實(shí)例,有需要的朋友可以參考一下
    2013-12-12
  • RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法講解

    RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法講解

    這篇文章主要介紹了RocketMQ NameServer保障數(shù)據(jù)一致性實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • java通過(guò)HTTP接收json詳細(xì)實(shí)例代碼

    java通過(guò)HTTP接收json詳細(xì)實(shí)例代碼

    Java作為一門廣泛使用的編程語(yǔ)言,很多開發(fā)人員會(huì)用它來(lái)進(jìn)行http請(qǐng)求,獲取json數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于java通過(guò)HTTP接收json的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 使用Spring掃描Mybatis的mapper接口的三種配置

    使用Spring掃描Mybatis的mapper接口的三種配置

    這篇文章主要介紹了使用Spring掃描Mybatis的mapper接口的三種配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 詳解java設(shè)計(jì)模式中的門面模式

    詳解java設(shè)計(jì)模式中的門面模式

    門面模式又叫外觀模式(Facade?Pattern),主要用于隱藏系統(tǒng)的復(fù)雜性,并向客戶端提供了一個(gè)客戶端可以訪問(wèn)系統(tǒng)的接口,本文通過(guò)實(shí)例代碼給大家介紹下java門面模式的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-02-02
  • Java中的反射,枚舉及l(fā)ambda表達(dá)式的使用詳解

    Java中的反射,枚舉及l(fā)ambda表達(dá)式的使用詳解

    這篇文章主要為大家詳細(xì)介紹了Java的反射,枚舉及l(fā)ambda表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03

最新評(píng)論

杨浦区| 调兵山市| 科技| 库车县| 吉首市| 宁都县| 武平县| 武夷山市| 夏邑县| 富川| 砚山县| 垫江县| 四会市| 奉贤区| 巨野县| 古丈县| 西畴县| 九龙城区| 台北县| 朝阳市| 南投市| 哈尔滨市| 赣榆县| 满城县| 钦州市| 巍山| 紫阳县| 云龙县| 宽城| 额济纳旗| 镇康县| 巫山县| 灵璧县| 手游| 日土县| 江山市| 乡城县| 沂南县| 宁阳县| 北辰区| 神池县|