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

网友投稿 202 2023-07-21


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

1、通过String来创建模版对象,并执行插值处理

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://baidu.com/");

map.put("name", "百度");

//执行插值,并输出到指定的输出流中

t.process(map, new OutputStreamWriter(System.out));

}

}

执行后,控制台输出结果:

用户名:lavasoft;

URL: http://baidu.com/;

姓名:  百度

Process finished with exit code 0

2、通过文件来创建模版对象,并执行插值操作

import freemarker.template.Configuration;

import freemarker.template.Template;

import java.io.File;

import java.io.OutpuHlRYyAtStreamWriter;

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://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

Our latest product:

${name}!

尊敬的用户你好:

用户名:${user};

URL: ${url};

姓名:  ${name}

执行后,控制台输出结果如下:

Our latest producthttp://:

百度!

尊敬的用户你好:

用户名:lavasoft;

URL: http://baidu.com/;

姓名:  百度

Process finished with exit code 0

3.基于注解的Spring+freemarker实例

web项目图

web.xml文件

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/springmvc-servlet.xml

1

springmvc

/

index.jsp

springmvc-servlet.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

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 users = new ArrayList();

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

<#list users as user>

username : ${user.username}

password : ${user.password}

#list>

部署到tomcat,运行:http://localhost:8080/springmvc/home/index

  显示结果:

username : zhangsan

password : 1234


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:举例分析Python中设计模式之外观模式的运用
下一篇:深入解析Java并发程序中线程的同步与线程锁的使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~