templateact.java
来自「JEECSM是JavaEE版网站管理系统(Java Enterprise Edi」· Java 代码 · 共 331 行
JAVA
331 行
package com.jeecms.core.action;
import static com.jeecms.core.Constants.FILE_SPT;
import static com.jeecms.core.Constants.SPT;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jeecms.core.JeeCoreAction;
import com.jeecms.core.util.FileWrap;
@SuppressWarnings("serial")
public class TemplateAct extends JeeCoreAction {
private static final Log log = LogFactory.getLog(TemplateAct.class);
public String doResMain() {
return MAIN;
}
@SuppressWarnings("unchecked")
public String doLeft() {
String path = getWeb().getTplRootReal(contextPvd.getAppRoot())
.toString().replace(SPT, FILE_SPT);
File tplFile = new File(path);
treeRoot = new FileWrap(tplFile, path, new FileFilter() {
@Override
public boolean accept(File f) {
if (f.getName().startsWith(".") || f.getName().startsWith("$")) {
return false;
} else {
return true;
}
}
});
return LEFT;
}
public String doResLeft() {
String resPath = contextPvd.getAppRealPath(getWeb().getResRoot()
.toString());
treeRoot = new FileWrap(new File(resPath), resPath, new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory() || f.getName().endsWith(".js")
|| f.getName().endsWith(".css")
|| f.getName().endsWith(".html")
|| f.getName().endsWith(".txt")) {
return true;
} else {
return false;
}
}
});
return LEFT;
}
public String doRight() {
return RIGHT;
}
public String doList() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
File dir = new File(path);
subDir = new FileWrap(dir).getChild();
return LIST;
}
public String doResList() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot().append(
relPath).toString());
File dir = new File(path);
subDir = new FileWrap(dir).getChild();
return LIST;
}
public String doAdd() {
return ADD;
}
public String doResAdd() {
return ADD;
}
private void saveFile(String path) {
File tpl = new File(path + relPath + FILE_SPT + tplName);
try {
FileUtils.writeStringToFile(tpl, tplContent);
} catch (IOException e) {
log.error("写入模板失败!", e);
addActionError("写入模板失败!");
}
}
public String doSave() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
saveFile(path);
return doList();
}
public String doResSave() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot()
.toString());
saveFile(path);
return doResList();
}
private void editFile(String path) {
File tpl = new File(path);
tplName = tpl.getName();
parentPath = relPath.substring(0, relPath.lastIndexOf(FILE_SPT));
try {
tplContent = FileUtils.readFileToString(tpl);
} catch (IOException e) {
log.error("读取模板文件失败", e);
addActionError("读取模板文件失败!");
}
}
public String doEdit() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
editFile(path);
return EDIT;
}
public String doResEdit() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot().append(
relPath).toString());
editFile(path);
return EDIT;
}
private void updateFile(String path) {
File tpl = new File(path);
File newFile = tpl;
if (!tpl.getName().equals(tplName)) {
newFile = new File(tpl.getParent() + FILE_SPT + tplName);
tpl.renameTo(newFile);
}
try {
FileUtils.writeStringToFile(newFile, tplContent);
} catch (IOException e) {
log.error("写文件失败", e);
jsonRoot.put("success", false);
jsonRoot.put("msg", "写文件失败!");
}
jsonRoot.put("success", true);
jsonRoot.put("msg", "保存成功");
}
public String doUpdate() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
updateFile(path);
return SUCCESS;
}
public String doResUpdate() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot().append(
relPath).toString());
updateFile(path);
return SUCCESS;
}
private void renameFile(String path) {
File tpl = new File(path);
if (!origName.equals(tplName)) {
boolean b = tpl.renameTo(new File(tpl.getParent() + FILE_SPT
+ tplName));
if (!b) {
jsonRoot.put("success", false);
}
}
jsonRoot.put("success", true);
}
public String doRename() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(origName).toString());
renameFile(path);
return SUCCESS;
}
public String doResRename() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot().append(
relPath).append(SPT).append(origName).toString());
renameFile(path);
return SUCCESS;
}
private void deleteFile(String path) {
File tpl = new File(path);
if (FileUtils.deleteQuietly(tpl)) {
addActionError("删除成功!");
} else {
addActionError("删除失败!");
}
}
public String doDelete() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(tplName).toString());
deleteFile(path);
return doList();
}
public String doResDelete() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot().append(
relPath).append(SPT).append(tplName).toString());
deleteFile(path);
return doResList();
}
public String doCreateDir() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(dirName).toString());
File dir = new File(path);
dir.mkdir();
return doList();
}
public String doResCreateDir() {
String path = contextPvd.getAppRealPath(getWeb().getResRoot().append(
relPath).append(SPT).append(dirName).toString());
File dir = new File(path);
dir.mkdir();
return doResList();
}
private Map<String, Object> jsonRoot = new HashMap<String, Object>();
private FileWrap treeRoot;
private FileWrap resRoot;
private String relPath;
private String parentPath;
private String tplContent;
private String tplName;
private String dirName;
private String origName;
private List<FileWrap> subDir;
public FileWrap getTreeRoot() {
return treeRoot;
}
public void setTreeRoot(FileWrap treeRoot) {
this.treeRoot = treeRoot;
}
public String getTplContent() {
return tplContent;
}
public void setTplContent(String tplContent) {
this.tplContent = tplContent;
}
public String getTplName() {
return tplName;
}
public void setTplName(String tplName) {
this.tplName = tplName;
}
public String getRelPath() {
return relPath;
}
public void setRelPath(String relPath) {
this.relPath = relPath;
}
public Map<String, Object> getJsonRoot() {
return jsonRoot;
}
public void setJsonRoot(Map<String, Object> jsonRoot) {
this.jsonRoot = jsonRoot;
}
public List<FileWrap> getSubDir() {
return subDir;
}
public void setSubDir(List<FileWrap> subDir) {
this.subDir = subDir;
}
public String getOrigName() {
return origName;
}
public void setOrigName(String origName) {
this.origName = origName;
}
public String getDirName() {
return dirName;
}
public void setDirName(String dirName) {
this.dirName = dirName;
}
public String getParentPath() {
return parentPath;
}
public void setParentPath(String parentPath) {
this.parentPath = parentPath;
}
public FileWrap getResRoot() {
return resRoot;
}
public void setResRoot(FileWrap resRoot) {
this.resRoot = resRoot;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?