Struts BaseAction工具类,封装Session,Request,Application,ModelDriven

时间:2024-01-13 10:30:02
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;
} }