JAVA_WEB專案之Action層利用ModelDriven抽取BaseAction

chenchudongsg發表於2014-07-15

在做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";
	}
}



相關文章