商城ssh架构 action层
上代码先,实现ModelDriven,可以在jsp中传值不家类名如 user.name,可以直接写成 name
RequestAware, SessionAware, ApplicationAware可以直接在三个内置对象中些数据,map格式
package com.zzu.shopol.action;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zzu.shopol.pojo.Account;
import com.zzu.shopol.service.AccountService;
import com.zzu.shopol.service.CategoryService;
import com.zzu.shopol.service.ForderService;
import com.zzu.shopol.service.GoodsService;
import com.zzu.shopol.service.SorderService;
import com.zzu.shopol.util.FileUploadUtil;
@SuppressWarnings("all")
public class BaseAction<T> extends ActionSupport implements ModelDriven<T>,
RequestAware, SessionAware, ApplicationAware {
/**
*
*/
protected Map<String,Object> jsonMap=new HashMap<String,Object>();
private static final long serialVersionUID = -7030960600300720776L;
protected T model =null;
protected Map<String, Object> session = null;
protected Map<String, Object> application = null;
protected Map<String, Object> request = null;
protected AccountService accountService = null;
protected CategoryService categoryService = null;
protected GoodsService goodsService = null;
protected FileUploadUtil fileUploadUtil = null;
protected SorderService sorderService = null;
protected ForderService forderService =null;
//获取子类的直接父类
BaseAction(){
try {
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
Class clazz =(Class)type.getActualTypeArguments()[0];
model = (T) clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//此方法 在创建action之后,有拦截器调用,所以执行顺序在构造方法之后
public T getModel() {
return model;
}
public ForderService getForderService() {
return forderService;
}
public void setForderService(ForderService forderService) {
this.forderService = forderService;
}
public SorderService getSorderService() {
return sorderService;
}
public void setSorderService(SorderService sorderService) {
this.sorderService = sorderService;
}
public FileUploadUtil getFileUploadUtil() {
return fileUploadUtil;
}
public void setFileUploadUtil(FileUploadUtil fileUploadUtil) {
this.fileUploadUtil = fileUploadUtil;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public void setApplication(Map<String, Object> application) {
this.application = application;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public CategoryService getCategoryService() {
return categoryService;
}
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public GoodsService getGoodsService() {
return goodsService;
}
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public Map<String, Object> getJsonMap() {
return jsonMap;
}
public void setJsonMap(Map<String, Object> jsonMap) {
this.jsonMap = jsonMap;
}
public Map<String, Object> getSession() {
return session;
}
public Map<String, Object> getRequest() {
return request;
}
public void setModel(T model) {
this.model = model;
}
}