在做web项目的过程中,我们会遇到Action层随时的从前台获取数据交给业务层处理,但是如果有多个Action类,我们会发现一些共同点,如获取数据存储到request,session等存储域里面,当然我们一般在Struts中都是通过ActionContext获取的,我们都知道ActionContext没有侵入性,下面介绍抽取出BaseAction,让所有Action类继承这个类,同时我们也可以不用ActionContext存数据,我们实现RequestWare,SessionWare等接口,通过map集合存储,可以减少我们每次在每个Action类中都通过ActionContext存数据,如果有多个我们就要写多次ActionContext获取request,session等相关对象存储,完全可以不这么做,因而我们可以使用下面的方法减少代码的编写。
BaseAction:
package com.shop.action;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
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;
import com.shop.service.AccountService;
import com.shop.service.CategoryService;
@Controller(value="baseAction")
@Scope(value="prototype")
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T>{
@Resource(name="categoryService")//可以看做是xml中的bean的ref,依赖于Service层中的categoryService
protected CategoryService categoryService=null;
@Resource
protected AccountService accountService=null;
protected T model;
public T getModel() {
System.out.println("---model----"+model);
return model;
}
public BaseAction(){
System.out.println(this.getClass().getGenericSuperclass());
Type type= this.getClass().getGenericSuperclass();
ParameterizedType parameterizedType=(ParameterizedType) type;
Class clazz= (Class) parameterizedType.getActualTypeArguments()[0];
try {
model=(T) clazz.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
throw new RuntimeException(e);
}
}
protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application=application;
}
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session=session;
}
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request=request;
}
}
让CategoryAction继承BaseAction:
package com.shop.action;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
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.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shop.pojo.Category;
import com.shop.service.CategoryService;
@Controller(value="categoryAction")
@Scope(value="prototype")
public class CategoryAction extends BaseAction<Category>{
public CategoryAction(){
super();
System.out.println("--CategoryAction---");
}
/**
* 数据在前台如何显示的集中方法
* @return
*/
public String save() {
// TODO Auto-generated method stub
System.out.println("save方法:"+model.getType()+","+model.getHot());
//categoryService.save(category);
//第一种Action依赖于Http内置的对象,不推荐使用
// ServletActionContext.getRequest().setAttribute("req",model.getType() );
// ServletActionContext.getRequest().getSession().setAttribute("ses",model.getType() );
// ServletActionContext.getServletContext().setAttribute("ses",model.getType() );
//第二种:没有侵入性,但是代码多,比较繁琐
// ActionContext.getContext().put("res", model.getType() );
// ActionContext.getContext().getSession().put("ses", model.getType() );
// ActionContext.getContext().getApplication().put("app", model.getType());
//第三种方法
request.put("req", model.getType());
session.put("ses", model.getType());
application.put("app", model.getType() );
return "index";
}
public String query(){
request.put("list", categoryService.query());
return "index";
}
}