zoukankan      html  css  js  c++  java
  • Struts BaseAction工具类,封装Session,Request,Application,ModelDriven

    package com.ssh.shop.action;
    
    import java.io.InputStream;
    import java.lang.reflect.ParameterizedType;
    import java.util.List;
    import java.util.Map;
    
    import javax.annotation.Resource;
    
    import org.apache.struts2.interceptor.ApplicationAware;
    import org.apache.struts2.interceptor.RequestAware;
    import org.apache.struts2.interceptor.SessionAware;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    //...
    
    
    /**
     * 设置jsp中使用到的session、request、application
     * 其他action继承该类,可以使用这些对象,而不用再重复地自己创建
     * 
     * 注意:在项目启动的时候,struts过滤器会自动把相应的内置对象,
     *     和内置对象对应的sessionMap、requestMap、applicateMap,
     *     存储到ActionContext和值栈中,
     *     实现的拦截器为servletConfig(org.apache.struts2.interceptor.ServletConfigInterceptor)
     * 
     * Struts执行流程:创建action ---> 调用拦截器  ---> 拦截器访问成功后调用action的方法
     * 
     * ServletConfigInterceptor源代码中有下面代码:(判断实现什么接口,就注入什么对象)
     *     if (action instanceof ApplicationAware) //如果action实现了ApplicationAware接口
                ((ApplicationAware) action).setApplication(context.getApplication());
        if (action instanceof SessionAware)
            ((SessionAware) action).setSession(context.getSession());
        if (action instanceof RequestAware)
            ((RequestAware) action).setRequest((Map) context.get("request"));
     * 
     * 加入的ModelDriven泛型的使用方法
     * @author Administrator
     *
     */
    @SuppressWarnings("unchecked")
    @Controller
    @Scope("prototype")
    public class BaseAction<T> extends ActionSupport implements SessionAware,RequestAware,ApplicationAware,ModelDriven<T>{
    
        //批量删除的记录的id数组
        protected String ids;
        
        protected T model;
        
        protected Map<String, Object> session;
        protected Map<String, Object> request;
        protected Map<String, Object> applicate;
        
        
        /**
         * 存储数据给前台,可以为记录count和总记录数,用来分页
         */
        protected Map<String, Object> pageMap = null;
        /**
         * 流形式数据
         */
        protected InputStream inputStream;
        
            /**
         * Json格式数据
         */
        List<T> jsonList = null;
        
    
        @Resource  //spring属性注入
        protected CategoryService categoryService;
        @Resource
        protected AccountService accountService;
        @Resource
        protected ProductService productService;
        @Resource
        protected UploadUtil uploadUtil;
        
        //用户文件上传(使用方法见product/save.jsp中图片上传)
        protected FileUpload fileUpload;
    
        /**
             * 很重要
         * 获取T的具体类型
         * 实现ModelDriven接口,必须要写此方法:获取实例
         */
        @Override
        public T getModel() {
            ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
            Class clazz = (Class) type.getActualTypeArguments()[0];
            try {
                model = (T) clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
    //        System.out.println("model:"+model);
            return model;
        }
        
        
        @Override
        public void setApplication(Map<String, Object> applicate) {
            this.applicate = applicate;
        }
    
        @Override
        public void setRequest(Map<String, Object> request) {
            this.request = request;
        }
    
        @Override
        public void setSession(Map<String, Object> session) {
            this.session = session;
        }
    
        public Map<String, Object> getPageMap() {
            return pageMap;
        }
    
    
        /**
         * 只写get方法,用于将获取的数据传输给前台
         * @return
         */
        public InputStream getInputStream() {
            return inputStream;
        }
        /**
         * 只写get方法,用于将获取的数据传输给前台
         * @return
         */
        public List<T> getJsonList() {
            return jsonList;
        }
    
        public FileUpload getFileUpload() {
            return fileUpload;
        }
    
        public void setFileUpload(FileUpload fileUpload) {
            this.fileUpload = fileUpload;
        }
        
    }
        
    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    (转)php读写文件
    CentOS5.2下安装GCC4.1.2
    使用php模拟post提交数据
    Linux系统信息查看命令大全
    强制卸载MYSQL
    Php文件操作
    redhat linux上安装 gcc编译器
    centos5.2安装mysql6.0
    如何收缩数据库日志文件(ldf)
    浑沌的JSON,JS Object,JS Array
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/5108904.html
Copyright © 2011-2022 走看看