Struts2通用BaseAction的实现

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;
    }  
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值