OpenCms创建资源文件自动生成文件[转]

出处:

http://javajiao.iteye.com/blog/257961 

 

在OpenCms中,大量数据添加的时候,文件名的命名及输入着实冗余繁杂。
如果能够在建立资源文件的时候,按照一定的规律自动生成文件名,并在建立
资源文件的时候就填充在Title文本框呢,这样就能改进前面提到的问题,加
快系统开发的进度。

实现思路:
1. 写一个bean,里面的方法能够按照规则生成文件名字符串;
2. 在/system/workplace/commons/newresource_xmlcontent.jsp
   中引用这个bean并调用其中的方法生成文件名;
3. 找到110行的<script type="text/javascript">
    在 checkValue();这一行下加上:
    "document.forms.main.resource.value="<%=fileName%>.html";"
4. 再次建立资源文件,测试是否已经自动生成了文件名.

PS:
1. newresource_xmlcontent.jsp页面的引用语句

Java代码 复制代码
  1. <%@ page import="org.opencms.workplace.explorer.*" %>   
  2. <%@ page import="cn.javajiao.templates.util.DceDateUtil"%>   
  3. <%   
  4. DceDateUtil du = new DceDateUtil();   
  5. String fileName = du.autoGenerateFileName();   
  6. %>   
  7. ... ...   
  8. <script type="text/javascript">   
  9.   
  10. document.forms.main.resource.value="<%=fileName%>.html";   
  11.   
  12. <!--   
  13.     checkValue();   
  14.        
  15. //-->   
  16. </script>   
  17. ... ...  
<%@ page import="org.opencms.workplace.explorer.*" %>
<%@ page import="cn.javajiao.templates.util.DceDateUtil"%>
<%
DceDateUtil du = new DceDateUtil();
String fileName = du.autoGenerateFileName();
%>
... ...
<script type="text/javascript">

document.forms.main.resource.value="<%=fileName%>.html";

<!--
	checkValue();
	
//-->
</script>
... ...



2. 自动生成文件名的bean,其生成的策略是:当前的年月日时分秒+三位随即数字。

Java代码 复制代码
  1. package cn.javajiao.templates.util;   
  2.   
  3. import java.text.SimpleDateFormat;   
  4. import java.util.Date;   
  5. import java.util.Random;   
  6.   
  7. public class DceDateUtil {   
  8.     //setting the format of date    
  9.     public static final SimpleDateFormat    
  10.            fileDateFormatter = new SimpleDateFormat("yyyyMMddHHmmss");   
  11.     /**  
  12.          * method use to generate file name by the given rule.  
  13.          */  
  14.     public static String autoGenerateFileName() {   
  15.         String temp_name = "";   
  16.         Date current_date = new Date();   
  17.         try {   
  18.             return fileDateFormatter.format(current_date)   
  19.                     + generateThreeDigitRandom();   
  20.         } catch (Exception e) {   
  21.             e.printStackTrace();   
  22.             return new Long(0l).toString();   
  23.         }   
  24.     }   
  25.     /**  
  26.          * method use to generate a random string of tree digit.  
  27.          */    
  28.     public static String generateThreeDigitRandom() {   
  29.         Random random = new Random();   
  30.         int j = (int) (random.nextInt(1000));   
  31.         if (j < 10) {   
  32.             return "00" + j;   
  33.         } else if (j < 100) {   
  34.             return "0" + j;   
  35.         }   
  36.         return "" + j;   
  37.     }   
  38.   
  39. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值