`
747017186
  • 浏览: 318588 次
社区版块
存档分类
最新评论

Velocity模板路径的配置

    博客分类:
  • html
 
阅读更多

我们一般用 Velocity作为模板进行页面的配置,之所以这样做是因为很简单。可以通过ajax可以返回已打算html代码,把这些html代码放到指定的div当中,实现异步刷新。这样不必花大量时间去解析json代码。

ajax 前台页面我不写了,直接写后台的代码:

/**
 * Mainbo.com Inc.
 * Copyright (c) 2015-2017 All Rights Reserved.
 */
package com.mainbo.jy.lecturerecords.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mainbo.jy.comment.bo.CommentInfo;
import com.mainbo.jy.comment.service.CommentInfoService;
import com.mainbo.jy.common.page.PageList;
import com.mainbo.jy.common.web.controller.AbstractController;

/**
 * 听课记录控制器接口
 * <pre>
 *
 * </pre>
 *
 * @author Generate Tools
 * @version $Id: LectureRecords.java, v 1.0 2015-03-30 Generate Tools Exp $
 */
@Controller
@RequestMapping("/jy/lecturerecords")
public class LectureRecordsController extends AbstractController{
//map是装在数据的,key值可以直接在模板中使用
	public String aa(HttpServletRequest request,Map<String,Object> map) {
		Properties p = new Properties();
//第一种:文件路径的方式,注意一定是绝对路径
		String s=request.getSession().getServletContext().getRealPath("/")+File.separator+"WEB-INF";//模板都放在web-info下
		p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, s);

//第二种:通过类路径加载模板,模板都放在classpath下,也就是src下面
 p.setProperty("class.resource.loader.class""org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
			 
        VelocityEngine ve = new VelocityEngine(p);//生成模板引擎
        ve.init();
        /* next, get the Template */
        Template t = ve.getTemplate("hellosite.html");
        /* create a context and add data */
        VelocityContext context = new VelocityContext();
        context.put("name", "DingDangXiaoMa");
        context.put("site", "http://www.sun.com");
        
        Set<String> keys=map.keySet();
        for (String key : keys) {
        	context.put(key, map.get(key));
		}
        /* now render the template into a StringWriter */
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        /* show the World */
        System.out.println(writer.toString());
        
        return writer.toString();//得到模板上面所有的文本,这样就可以直接通过打印到前段页面了
	}
}

/**
	 * 返回字符串UTF-8编码
	 * @param response
	 * @param result 字符串内容。text/plain
	 * @throws Exception
	 */
	protected final void writeText(HttpServletResponse response, String result) throws Exception {
		response.setContentType("text/plain; charset=UTF-8");
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		out.write(result);
		out.close();
	}

	/**
	 * 用于导出、下载XML文件
	 * @param response
	 * @param result 字符串内容。
	 * @param fileName 下载的文件名
	 * @throws Exception
	 */
	protected final void downloadXML(HttpServletResponse response, String result, String fileName) throws Exception {
		// 避免过滤器赋值导致无法下载
		response.setHeader("Cache-Control", "");

		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
		OutputStream out = response.getOutputStream();
		out.write(result.getBytes("UTF-8"));
		out.flush();
		out.close();
}

模板:
<div>
Hello $name! Welcome to $site world!
$data
#foreach($product in $data.datalist) <!--传过来的map的key值-->
<li>$product.username:$product.content</li> 
#end
</div>	 

 

分享到:
评论

相关推荐

    基于模板的代码生成器LKGenerator1.1.0_x86

    (1)配置模版路径,这个配置可以配置用于生成代码的velocity模版路径,生成的代码将模版中的特定符号见“模版中可使用的变量”中的描述。 (2)配置生成代码的固定文件路径,这个配置中的文件将原样拷贝到最终生成...

    基于模板的代码生成器LKGenerator1.1.0_x64

    (1)配置模版路径,这个配置可以配置用于生成代码的velocity模版路径,生成的代码将模版中的特定符号见“模版中可使用的变量”中的描述。 (2)配置生成代码的固定文件路径,这个配置中的文件将原样拷贝到最终生成...

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    velocity配置文件路径 struts.velocity.contexts List of Velocity context names velocity的context列表 struts.velocity.manager.classname org.apache.struts2.views.velocity.VelocityManager ...

    express-velocity:基于express搭建的velocity服务器

    express-velocity 基于express搭建的velocity服务器 使用方式 Command: $ git clone ... 'viewsPath': 'vm/', //模板文件路径 'staticPath': 'public' //静态资源文件路径 } 依赖

    AutoEE_V1-自动智能开发平台免费下载

    AutoEE-自动智能开发平台是一款使用当前主流框架搭建,并结合Velocity模板引擎和MyBatis Generator实现的高度可自定义、高度可配置的自动智能生成代码的开源的快速开发平台。 使用AutoEE开发平台可以简单、快速、...

    Struts2属性文件详解

    该属性指定视图主题所需要模板文件的位置, 该属性的默认值是template,即默认加载template路径下的模板文件. struts.ui.templateSuffix 该属性指定模板文件的后缀,该属性的默认属性值是ftl.该属性还允许使用ftl、vm...

    springboot参考指南

    自定义管理服务器的上下文路径 iii. 41.3. 自定义管理服务器的端口 iv. 41.4. 自定义管理服务器的地址 v. 41.5. 禁用HTTP端点 vi. 41.6. HTTP Health端点访问限制 iv. 42. 基于JMX的监控和管理 i. 42.1. 自定义...

    Struts2\constant应用

    该属性指定视图主题所需要模板文件的位置,该属性的默认值是template,即默认加载template路径下的模板文件。 struts.ui.templateSuffix 该属性指定模板文件的后缀,该属性的默认属性值是ftl。该属性还允许使用...

    static-starter:静态项目启动器,包括 Gulp、Browserify、Sass、Bourbon、Neat、Bitters 和 Jade-ified html5 样板。 基于 gulp-starter

    安装 npm 依赖 npm install之后,只需运行default gulp 任务: gulp配置所有路径和插件设置都被抽象到gulp/config.js的集中配置对象中。 根据项目的结构和需求调整路径和设置。生产文件您还可以运行一个production...

    Spring in Action(第2版)中文版

    14.4.1使用velocity模板 14.4.2使用freemarker 14.5产生非html输出 14.5.1产生excel工作表 14.5.2产生pdf文档 14.5.3开发自定义视图 14.6小结 第15章使用springwebflow 15.1开始springwebflow之旅 15.1.1...

    Spring in Action(第二版 中文高清版).part2

    14.4.1 使用Velocity模板 14.4.2 使用FreeMarker 14.5 产生非HTML输出 14.5.1 产生Excel工作表 14.5.2 产生PDF文档 14.5.3 开发自定义视图 14.6 小结 第15章 使用Spring Web Flow 15.1 开始Spring Web ...

    Spring in Action(第二版 中文高清版).part1

    14.4.1 使用Velocity模板 14.4.2 使用FreeMarker 14.5 产生非HTML输出 14.5.1 产生Excel工作表 14.5.2 产生PDF文档 14.5.3 开发自定义视图 14.6 小结 第15章 使用Spring Web Flow 15.1 开始Spring Web ...

    word源码java-code-generator:代码生成器

    通过Velocity模板引擎 实现代码的生成(java、xml、sql)支持CRUD下载 。 需要生成的数据库必须有完整的备注和库表注释,不然程序无法是识别 项目支持word、excel文件格式下载 项目支持批量生成代码,加快编程效率 ...

    Spring 2.0 开发参考手册

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    Spring-Reference_zh_CN(Spring中文参考手册)

    14.4.3. 创建模板 14.4.4. 高级配置 14.4.4.1. velocity.properties 14.4.4.2. FreeMarker 14.4.5. 绑定支持和表单处理 14.4.5.1. 用于绑定的宏 14.4.5.2. 简单绑定 14.4.5.3. 表单输入生成宏 14.4.5.4. 重载HTML...

    Spring中文帮助文档

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    Spring API

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    spring chm文档

    14.4. Velocity和FreeMarker 14.4.1. 需要的资源 14.4.2. Context 配置 14.4.3. 创建模板 14.4.4. 高级配置 14.4.5. 绑定支持和表单处理 14.5. XSLT 14.5.1. 写在段首 14.5.2. 小结 14.6. 文档视图(PDF/...

    Struts2 in action中文版

    8.3.1 VelocityResult,也叫做velocity 189 8.3.2 FreemarkerResult,也叫做freemarker 191 8.4 全局结果 192 8.5 小结 193 第四部分 完善应用程序 第9章 集成Spring和Hibernate/JPA 196 9.1 为什么在Struts 2中...

    单点登录源码

    Velocity | 模板引擎 | [http://velocity.apache.org/](http://velocity.apache.org/) ZooKeeper | 分布式协调服务 | [http://zookeeper.apache.org/](http://zookeeper.apache.org/) Dubbo | 分布式服务框架 | ...

Global site tag (gtag.js) - Google Analytics