web层公共BaseAction
package com.templete.action;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.templete.domain.User;
@SuppressWarnings("all")
public class BaseAction extends ActionSupport{
public static final String VIEW = "view";
public static final String LIST = "list";
public static final String STATUS = "status";
public static final String WARN = "warn";
public static final String SUCCESS = "success";
public static final String ERROR = "error";
public static final String MESSAGE = "message";
public static final String CONTENT = "content";
protected String id;
protected String[] ids;
//protected Pager pager;
protected String logInfo;// 日志记录信息
protected String redirectionUrl;// 操作提示后的跳转URL,为null则返回前一页
/**
* 获取当前登录的用户
*
* @return
*/
protected User getCurrentUser() {
return (User) ActionContext.getContext().getSession().get("user");
}
/**
* 保存上传的文件,并返回文件存储的真实路径(服务器端)
*
* @param upload
* @return
*/
protected String saveUploadFile(File upload) {
// 新建对象并设置属性(也可以使用model)
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
// >> 计算路径
String basePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload_files");
String subPath = sdf.format(new Date());
String path = basePath + subPath + UUID.randomUUID().toString(); // 使用UUID解决重名的问题
// >> 创建不存在的文件夹
File dir = new File(basePath + subPath);
if (!dir.exists()) {
dir.mkdirs();
}
// >> 保存文件
File dest = new File(path);
upload.renameTo(dest); // 当目标文件夹不存在或是目标文件已存在时,会返回false,移动不成功
return path;
}
// 获取Session
public Object getSession(String name) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
return session.get(name);
}
// 获取Session
public Map<String, Object> getSession() {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
return session;
}
// 设置Session
public void setSession(String name, Object value) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.put(name, value);
}
// 获取Request
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
// 获取Response
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
// 获取Application
public ServletContext getApplication() {
return ServletActionContext.getServletContext();
}
//获取Attribute
public Object getAttribute(String paramString){
return ServletActionContext.getRequest().getAttribute(paramString);
}
//设置Attribute
public void setAttribute(String name,Object value){
ServletActionContext.getRequest().setAttribute(name,value);
}
//获取Parameter
public String getParameter(String paramString){
return getRequest().getParameter(paramString);
}
//获取Parameter数组
public String[] getParameterValues(String name){
return getRequest().getParameterValues(name);
}
//查找该文件在本工程下的物理位置
public String getFilePath(String path){
return ServletActionContext.getServletContext().getRealPath(path);
}
/**
* 返回json字符串
* @throws IOException
*/
protected void renderJSON(Map map) throws IOException {
JSONObject jsonObjectFromMap = JSONObject.fromObject(map);
renderText(jsonObjectFromMap.toString());
}
/**
* 直接输出字符
* @throws IOException
*/
protected String renderText(String text) throws IOException {
getResponse().setContentType("text/html;charset=utf-8");
getResponse().getWriter().print(text);
return null;
}
}