出处:
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页面的引用语句
- <%@ 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>
- ... ...
<%@ 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,其生成的策略是:当前的年月日时分秒+三位随即数字。
- package cn.javajiao.templates.util;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Random;
- public class DceDateUtil {
- //setting the format of date
- public static final SimpleDateFormat
- fileDateFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
- /**
- * method use to generate file name by the given rule.
- */
- public static String autoGenerateFileName() {
- String temp_name = "";
- Date current_date = new Date();
- try {
- return fileDateFormatter.format(current_date)
- + generateThreeDigitRandom();
- } catch (Exception e) {
- e.printStackTrace();
- return new Long(0l).toString();
- }
- }
- /**
- * method use to generate a random string of tree digit.
- */
- public static String generateThreeDigitRandom() {
- Random random = new Random();
- int j = (int) (random.nextInt(1000));
- if (j < 10) {
- return "00" + j;
- } else if (j < 100) {
- return "0" + j;
- }
- return "" + j;
- }
- }