SSH框架开发使用的BaseAction


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.hnjz.core.model.DicData;
import com.hnjz.core.model.Org;
import com.hnjz.core.model.Role;
import com.hnjz.core.model.User;
import com.hnjz.core.web.action.PageList;
import com.hnjz.core.web.listener.StaticLoad;
import com.hnjz.sys.search.ErrorPool;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware,
                                             RequestAware, CookiesAware, ParameterAware,
                                             SessionAware, ApplicationAware {
    /**
     * 当返回值为json时,保存一些特殊处理的介质
     */
    protected Map                 jsonResultMap    = new HashMap();

    protected HttpServletRequest  httpServletRequest;

    protected HttpServletResponse httpServletResponse;

    protected Map                 request;

    protected Map                 session;

    protected Map                 application;

    protected Map                 parameters;

    protected Map                 cookiesMap;

    protected PageList            pl;

    protected String              first;

    protected String              success;

    protected ErrorPool           errorPool;

    protected int                 page;

    protected final Log           log              = LogFactory.getLog(getClass());

    private static final long     serialVersionUID = 5321284593923166095L;

    /**
     * 得到当前用户
     * @return
     */
    public User getCurUser() {

        return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 得到当前用户的角色
     * @return
     */
    public Role getCurRole() {
        AbstractCurdManager curdManager = (AbstractCurdManager) StaticLoad.getBean("curdManager");
        User user = ((User) curdManager.get(User.class, getCurUser().getId()));

        return user.getUserRole().iterator().next().getRole();
    }

    /**
     * 得到当前用户所属的行政区划
     * @return
     */
    public DicData getCurDept() {

        AbstractCurdManager curdManager = (AbstractCurdManager) StaticLoad.getBean("curdManager");
        User user = ((User) curdManager.get(User.class, getCurUser().getId()));

        return user.getDept();
    }
    
    /**
     * 得到当前用户所属的行政区划
     * @return
     */
    public Org getCurOrg() {

        AbstractCurdManager curdManager = (AbstractCurdManager) StaticLoad.getBean("curdManager");
        User user = ((User) curdManager.get(User.class, getCurUser().getId()));
        return user.getGxOrg();
    }

    public void setServletRequest(HttpServletRequest arg0) {
        this.httpServletRequest = arg0;

    }

    public Map getJsonResultMap() {
        return jsonResultMap;
    }

    public void setServletResponse(HttpServletResponse arg0) {
        this.httpServletResponse = arg0;

    }

    public void setRequest(Map arg0) {
        this.request = arg0;

    }

    public HttpServletRequest getHttpServletRequest() {
        return httpServletRequest;
    }

    public void setCookiesMap(Map arg0) {
        this.cookiesMap = arg0;

    }

    public void setParameters(Map arg0) {
        this.parameters = arg0;
        this.errorPool = new ErrorPool(parameters);
    }

    public void setSession(Map arg0) {
        this.session = arg0;

    }

    public void setApplication(Map arg0) {
        this.application = arg0;

    }

    public PageList getPl() {
        return pl;
    }

    public void setPl(PageList pl) {
        this.pl = pl;
    }

    public int getPage() {

        return page;
    }

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    /**
     * 通过处理判断列表页面的页码。
     * 
     * @return
     */
    public int getPageNum() {
        // 读取页码
        int pageNum = 1;
        if (parameters.get("page") != null) {
            String pageStr = ((String[]) parameters.get("page"))[0];
            if (StringUtils.isNumeric(pageStr) && StringUtils.isNotBlank(pageStr)) {
                pageNum = Integer.parseInt(pageStr);
                session.put("search_page", pageNum);
            } else if (!"1".equals(first)) {
                Integer pageObj = (Integer) session.get("search_page");
                if (pageObj != null) {
                    pageNum = pageObj.intValue();
                }
            }
        }
        return pageNum;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public void setPage(String page) {
        this.page = Integer.parseInt(page);
    }
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * 根据当前用户和外部功能查询所对应代码
     * @param userid    当前用户
     * @param funcCode  外部功能编码
     * @return
     */
    protected String getMyFuncCity(String areaid, String funcCode) {
        AbstractCurdManager curdManager = (AbstractCurdManager) StaticLoad.getBean("curdManager");
        if (StringUtils.isBlank(areaid)) {//如果所属地市为空,返回""
            return "";
        }

        String hsql = "select code from TOutfuncRelation m where m.cityid.id=? and m.functionid.attrName2= ?";
        String[] canshu = new String[] {areaid, funcCode };
        List list = curdManager.find(hsql, canshu);
        if (list.size() > 0) {
            return String.valueOf(list.get(0));
        }
        return "";
    }

}

你可能感兴趣的:(java,框架,ssh)