?? templateact.java
字號(hào):
package com.jeecms.core.action;
import static com.jeecms.core.Constants.ENCODING;
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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.jeecms.core.JeeCoreAction;
import com.jeecms.core.util.FileWrap;
import com.jeecms.core.util.UploadRule;
import com.ponyjava.common.util.ComUtils;
import com.ponyjava.common.util.Zipper;
import com.ponyjava.common.util.Zipper.FileEntry;
@SuppressWarnings("serial")
@Scope("prototype")
@Controller("core.templateAct")
public class TemplateAct extends JeeCoreAction {
private static final Logger log = LoggerFactory
.getLogger(TemplateAct.class);
public String resMain() {
return MAIN;
}
public String left() {
String path = getWeb().getTplRootReal(contextPvd.getAppRoot())
.toString().replace(SPT, FILE_SPT);
File tplFile = new File(path);
treeRoot = new FileWrap(tplFile, path, new FileFilter() {
public boolean accept(File f) {
if (f.getName().startsWith(".") || f.getName().startsWith("$")) {
return false;
} else {
return true;
}
}
});
return LEFT;
}
public String resLeft() {
String resPath = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.toString());
treeRoot = new FileWrap(new File(resPath), resPath, new FileFilter() {
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 right() {
return RIGHT;
}
public String list() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
File dir = new File(path);
subDir = new FileWrap(dir).getChild();
return LIST;
}
public String resList() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).toString());
File dir = new File(path);
subDir = new FileWrap(dir).getChild();
return LIST;
}
public String add() {
// 設(shè)置上傳規(guī)則
addUploadRule();
return ADD;
}
public String resAdd() {
return ADD;
}
private boolean saveFile(String path) {
File tpl = new File(path + relPath + FILE_SPT + tplName);
try {
FileUtils.writeStringToFile(tpl, tplContent, ENCODING);
return true;
} catch (IOException e) {
log.error("寫(xiě)入模板失敗!", e);
addActionError("寫(xiě)入模板失敗!");
return false;
}
}
public String save() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
if (saveFile(path)) {
removeUploadRule();
}
return list();
}
public String resSave() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.toString());
saveFile(path);
return resList();
}
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, ENCODING);
} catch (IOException e) {
log.error("讀取模板文件失敗", e);
addActionError("讀取模板文件失敗!");
}
}
public String edit() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
editFile(path);
// 設(shè)置上傳規(guī)則
addUploadRule();
return EDIT;
}
public String resEdit() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).toString());
editFile(path);
return EDIT;
}
private boolean 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, ENCODING);
jsonRoot.put("success", true);
jsonRoot.put("msg", "保存成功");
return true;
} catch (IOException e) {
log.error("寫(xiě)文件失敗", e);
jsonRoot.put("success", false);
jsonRoot.put("msg", "寫(xiě)文件失敗!");
return false;
}
}
public String update() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
if (updateFile(path)) {
removeUploadRule();
}
return SUCCESS;
}
public String resUpdate() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.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 rename() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(origName).toString());
renameFile(path);
return SUCCESS;
}
public String resRename() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.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 delete() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(tplName).toString());
deleteFile(path);
return list();
}
public String resDelete() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).append(tplName).toString());
deleteFile(path);
return resList();
}
public String createDir() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(dirName).toString());
File dir = new File(path);
dir.mkdir();
return list();
}
public String resCreateDir() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).append(dirName).toString());
File dir = new File(path);
dir.mkdir();
return resList();
}
public String resUpload() {
return "upload";
}
public String resUploadSubmit() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).toString());
if (resFile != null) {
try {
for (int i = 0; i < resFile.length; i++) {
FileUtils.copyFile(resFile[i], new File(path + FILE_SPT
+ resFileFileName[i]));
}
addActionMessage("上傳成功!");
} catch (IOException e) {
addActionError("上傳失敗!" + e.getMessage());
}
}
return resList();
}
public String solutionEdit() {
// 數(shù)據(jù)庫(kù)和目錄中都有的模板套件才顯示
// 數(shù)據(jù)庫(kù)中模板方案
solMap = getWeb().getSolutions();
// 模板目錄中模板方案
dirMap = new LinkedHashMap<String, String[]>();
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
File[] tplFiles = new File(path).listFiles(ComUtils.DIR_FILE_FILTER);
for (File f : tplFiles) {
if (solMap.containsKey(f.getName())) {
dirMap.put(f.getName(), f.list(ComUtils.DIR_FILE_FILTER));
}
}
return "solution";
}
public String solutionUpdate() {
Map<String, String> smap = getWeb().getSolutions();
for (String key : smap.keySet()) {
String s = solMap.get(key);
if (s != null) {
smap.put(key, s);
}
}
addActionMessage("操作成功");
return solutionEdit();
}
public String exportTpl() {
solSet = new LinkedHashSet<String>();
solMap = getWeb().getSolutions();
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
File[] sysDirs = new File(path).listFiles(ComUtils.DIR_FILE_FILTER);
for (File sysDir : sysDirs) {
if (solMap.containsKey(sysDir.getName())) {
for (File sol : sysDir.listFiles(ComUtils.DIR_FILE_FILTER)) {
solSet.add(sol.getName());
}
}
}
return "exportTpl";
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -