BaseAction的一般寫法(附結果放入值棧的方式)
阿新 • • 發佈:2019-02-06
一、BaseAction一般寫法
1、BaseAction類
(1)程式碼解析如下:
public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
private static final long serialVersionUID = 1L;
//封裝資料
private T t;
public T getModel() {
return t;
}
//例項化t
public BaseAction() {
try {
//獲得T執行時的Class
ParameterizedType paramType= (ParameterizedType) this.getClass().getGenericSuperclass();
Class<T> clazz=(Class<T>) paramType.getActualTypeArguments()[0];
//反射建立例項
t=clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException();
}
}
//spring注入多個service
//* 提供setter方法,讓spring進行注入
//* 提供getter方法,讓子類可以獲得spring注入的物件
//員工
private StaffService staffService;
public void setStaffService(StaffService staffService) {
this.staffService = staffService;
}
public StaffService getStaffService() {
return staffService;
}
//職務
private PostService postService;
public void setPostService(PostService postService) {
this.postService = postService;
}
public PostService getPostService() {
return postService;
}
//部門
private DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
public DepartmentService getDepartmentService() {
return departmentService;
}
//課程類別
private CourseTypeService courseTypeService;
public void setCourseTypeService(CourseTypeService courseTypeService) {
this.courseTypeService = courseTypeService;
}
public CourseTypeService getCourseTypeService() {
return courseTypeService;
}
//班級
private ClassesService classesService;
public void setClassesService(ClassesService classesService) {
this.classesService = classesService;
}
public ClassesService getClassesService() {
return classesService;
}
//分頁資料
private int pageNum=1;
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageNum() {
return pageNum;
}
private int pageSize=2;
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
//值棧操作
/**
* 壓棧
* @param o
*/
public void push(Object o){
ActionContext.getContext().getValueStack().push(o);
}
/**
* 值棧的set
* @param key
* @param o
*/
public void set(String key,Object o){
ActionContext.getContext().getValueStack().set(key, o);
}
/**
* map的put
* @param key
* @param o
*/
public void put(String key,Object o){
ActionContext.getContext().put(key, o);
}
/**
* session的put
* @param key
* @param o
*/
public void putSession(String key,Object o){
ActionContext.getContext().getSession().put(key, o);
}
/**
* application的put
* @param key
* @param o
*/
public void putApplication(String key,Object o){
ActionContext.getContext().getApplication().put(key, o);
}
}
2、action中寫法
(1)直接繼承BaseAction即可,填寫泛型T的值
(2)不用寫service層的介面和相應的setter方法
(3)通過getModel()直接獲得當前物件的例項化物件
(4)程式碼如下:
public class CourseTypeAction extends BaseAction<CrmCourseType>{
private static final long serialVersionUID = 1L;
/**
* 查詢所有
* @return
*/
public String findAll(){
PageBean<CrmCourseType> pageBean=getCourseTypeService().findAll(this.getModel(),this.getPageNum(),this.getPageSize());
put("pageBean", pageBean);
return "findAll";
}
/**
* 新增或編輯顯示jsp頁面
* @return
*/
public String addOrEditUI(){
if(StringUtils.isNotBlank(this.getModel().getCourseTypeId())){
//編輯(有id),查詢資料並顯示
CrmCourseType findCourseType=this.getCourseTypeService().findById(this.getModel().getCourseTypeId());
push(findCourseType);
}
return "addOrEditUI";
}
/**
* 編輯或新增功能
* @return
*/
public String addOrEdit(){
this.getCourseTypeService().addOrEdit(this.getModel());
return "addOrEdit";
}
}
二、結果存入值棧
1、將結果存放到值棧中的方式(方便jsp取資料)
(1)方式1:
context(map)存放put(key,value) jsp獲得”#key”
ActionContext.getContext.put(key,value)
(2)方式2:
root(值棧),push(obj) 一般資料為javaBean或者map。jsp頁面獲得”屬性名”或者”key”
ActionContext.getContext().getValueStack().push(o)
(3)方式3:
root(值棧),set(key,value),一般資料型別為list,jsp頁面獲得”key”
//set() 底層new Map(key,value),將push(map)