自定义文章以XML类型的文章为主,下面自定义需求修改均基于opencms中自定义XML类型文章
根据自定义配置(自定义配置也为xml)添加默认文件名
新建文件时默认文件名获取方式可以在system\workplace\commons\newresource_xmlcontent.jsp中找到一下代码:
其中
为文件取值的位置,然后可以很容易找到一下代码:
org.opencms.workplace.explorer.CmsNewResource#setInitialResourceName()方法
将此方法中的相应部分修改一下就可以在每次新建文章时生成默认文件名,此处以当前日期的毫秒数为例:
以上代码中添加了从当前目录下读取配置文件的步骤,不需要可忽略
根据自定义配置(自定义配置也为xml)添加默认文件名
新建文件时默认文件名获取方式可以在system\workplace\commons\newresource_xmlcontent.jsp中找到一下代码:
<td class="maxwidth"><input name="<%= wp.PARAM_RESOURCE %>" id="newresfield" type="text" value="<%= wp.getParamResource() %>" class="maxwidth" onkeyup="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("");
}
}
以上代码中添加了从当前目录下读取配置文件的步骤,不需要可忽略