opencms8.0.1添加默认文件名

自定义文章以XML类型的文章为主,下面自定义需求修改均基于opencms中自定义XML类型文章


根据自定义配置(自定义配置也为xml)添加默认文件名


新建文件时默认文件名获取方式可以在system\workplace\commons\newresource_xmlcontent.jsp中找到一下代码:
<td class="maxwidth"><input name="<%= wp.PARAM_RESOURCE %>" id="newresfield" type="text" value="<%= wp.getParamResource() %>" class="maxwidth" οnkeyup="checkValue();"></td>




其中
<%= wp.getParamResource() %>

为文件取值的位置,然后可以很容易找到一下代码:


org.opencms.workplace.explorer.CmsNewResource#setInitialResourceName()方法


将此方法中的相应部分修改一下就可以在每次新建文章时生成默认文件名,此处以当前日期的毫秒数为例:


原方法:

/**
	 * Sets the initial resource name of the new page.
	 * <p>
	 * 
	 * This is used for the "new" wizard after creating a new folder followed by the "create index file" procedure.
	 * <p>
	 */
	protected void setInitialResourceName() {


		if (isCreateIndexMode()) {


			// creation of an index file in a new folder, use default file name
			String defaultFile = "";
			try {
				defaultFile = OpenCms.getDefaultFiles().get(0);
			} catch (IndexOutOfBoundsException e) {


				// list is empty, ignore
			}
			if (CmsStringUtil.isEmpty(defaultFile)) {


				// make sure that the default file name is not empty
				defaultFile = "index.html";
			}
			setParamResource(defaultFile);
		} else {
			// 设置默认文件名
			setParamResource("");
		}
	}



修改后:

protected void setInitialResourceName() {


		if (isCreateIndexMode()) {


			// creation of an index file in a new folder, use default file name
			String defaultFile = "";
			try {
				defaultFile = OpenCms.getDefaultFiles().get(0);
			} catch (IndexOutOfBoundsException e) {


				// list is empty, ignore
			}
			if (CmsStringUtil.isEmpty(defaultFile)) {


				// make sure that the default file name is not empty
				defaultFile = "index.html";
			}
			setParamResource(defaultFile);
		} else {
			// 设置默认文件名
			setDefaultResourceNm();
		}
	}


	/**
	 * 设置默认文件名
	 */
	private void setDefaultResourceNm() {
		String xpath_type = "filetype";
		String xpath_rule = "rule";
		String fileType = "";
		String rule = "";
		String resourcename = getParamCurrentFolder() + "relations.properties";
		try {
			CmsFile file = getCms().readFile(resourcename, CmsResourceFilter.ALL.addExcludeFlags(CmsResource.FLAG_INTERNAL));
			CmsXmlContent xml = CmsXmlContentFactory.unmarshal(getCms(), file);
			fileType = xml.getStringValue(getCms(), xpath_type, Locale.CHINESE);
			rule = xml.getStringValue(getCms(), xpath_rule, Locale.CHINESE);
		} catch (CmsException e) {
		}
		// 文件类型与配置的文件类型一致
		if (!StringUtils.isEmpty(fileType) && fileType.equals(getParamNewResourceType())) {
			setParamResource(String.valueOf(new java.util.Date().getTime()));
		} else {
			setParamResource("");
		}
	}


以上代码中添加了从当前目录下读取配置文件的步骤,不需要可忽略
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值