靜態化-VeloCity

1.在spring.xml中配置css

 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方法html

 1

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

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

 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,內容以下,我整理了經常使用的標籤在模版中app

$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.引用jarjsp

velocity-1.7.jar網站

相關文章
相關標籤/搜索