zoukankan      html  css  js  c++  java
  • 静态化-VeloCity

    1.在spring.xml中配置

     1     <!-- 指定vm模版路径 start  -->
     2     <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
     3         <property name="resourceLoaderPath" value="/WEB-INF/"/>
     4         <property name="preferFileSystemAccess" value="true"/>
     5         <property name="velocityProperties">
     6             <props>
     7                 <prop key="input.encoding">UTF-8</prop>
     8                 <prop key="output.encoding">UTF-8</prop>
     9                 <prop key="default.contentType">text/html;charset=UTF-8</prop>
    10             </props>
    11         </property>
    12     </bean>
    13      <!-- 指定vm模版路径 end  -->

    2.java方法

     1

    @Autowired
    @Qualifier("velocityEngine")
    private VelocityEngine velocityEngine;

    @RequestMapping(value = "/vm", method = RequestMethod.GET)  

     2     public void createHeader(HttpServletRequest request, HttpServletResponse response, String type) throws Exception {
     3         try {
     4             // 静态页生成路径
     5             String path1 = request.getSession().getServletContext().getRealPath("/") + "index/template/include/";
     6             createPath(path1);
     7             String path = path1+"header.jsp";
     8             // css路径
     9             String csspath = getPath(request);
    10             //velocityEngine.setProperty("resourceLoaderPath", "/WEB-INF/velocity/");
    11             Template tl = velocityEngine.getTemplate("./index.vm", "UTF-8");
    12             VelocityContext vc = new VelocityContext();
    13             //数据封装start
    14             vc.put("csspath", csspath);
    15             vc.put("tag", 12);
    16             vc.put("isnull", "");
    17             vc.put("bool", false);
    18             List l=new ArrayList();
    19             l.add("a");
    20             l.add("b");
    21             l.add("c");
    22             l.add("d");
    23             l.add("e");
    24             vc.put("list", l);
    25             SysNew s = new SysNew();
    26             s.setContents("contents");
    27             s.setEvents("eve");
    28             vc.put("sys", s);
    29             //数据封装end
    30             vc.put("charset","<%@ page language='java' import='java.util.*' pageEncoding='UTF-8'%>");
    31             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));
    32             tl.merge(vc, bw);
    33             
    34             //直接生成html代码不生成--发模版邮件使用star
    35             //StringWriter sw = new StringWriter();
    36             //tl.merge(vc, sw);
    37             //response.getWriter().print(sw.toString());
    38             //直接生成html代码不生成--发模版邮件使用end
    39             bw.close();
    40 
    41         } catch (Exception e) {
    42             System.out.println(e.getMessage() + "-----------------------");
    43         }
    44     }

    3.在WEB-INF下创建模版index.vm,内容如下,我整理了常用的标签在模版中

    $charset##去掉这个当前页面中文乱码
    1.变量的调用,后台存入“csspath”,前台
    $csspath</br>${csspath}<br />
    2.if判断</br>
    #if($tag == 1)
        等于1
    #else
        不等于1
    #end
    <br/>
    3.判断是否为空<br/>
    #if($isnull)
        变量为空
    #else
        变量不为空
    #end
    <br/>
    4.判断true OR false  <br/>
    #if($bool)
        true
    #else
        false
    #end
    <br/>
    5.注释:两个“#”注释单行,“#**#”注释代码块
    <br/>
    6.循环<br/>
    #foreach($lis in $list)
        $lis -- $velocityCount ##当前循环下标
    #end
    <br/>
    7.定义变量
    #set ($bianliang="bianliang")
    $bianliang
    <br/>
    8.调用对象中的值,和jsp中类似<br>
    $sys.events
    $sys.Contents
    <br/>
    9.引用模版<br/>
    主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
    如:##parse("/blog/top.html")或#include("/blog/top.html")
    parse与include的区别在于,若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样显示。
    ##inclue("模板文件名")或#parse("模板文件名")
    <br/>

    4.引用jar

    velocity-1.7.jar

  • 相关阅读:
    在ASP.NET 5中使用SignalR
    直传文件到Azure Storage的Blob服务中
    利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow
    【IDEA】如何设置代码超出长度限制时自动换行
    【IEDA】Typo: In woed 'xxx' more...(Ctrl + F1) 拼写检查
    【IDEA】URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs
    【IDEA】创建的 maven 项目,右键 New --> XML Configuration File 时,无 Spring Config 选项
    RHEL8网络配置
    Python GUI界面编程-初识
    Deepin系统中如何安装Visual Studio Code
  • 原文地址:https://www.cnblogs.com/rb2010/p/8177234.html
Copyright © 2011-2022 走看看