轉自:http://blog.csdn.net/eson_15/article/details/51297698
上一節我們搭建好了Struts2、hibernate和spring的開發環境,併成功將它們整合在一起。這節主要完成一些基本的增刪改查以及Service、Dao和Action的抽取。
1. Service層的抽取
上一節中,我們在service層簡單寫了save和update方法,這裏我們開始完善該部分的代碼,然後對service層的代碼進行抽取。
1.1 完善CategoryService層
對數據庫的操作無非是增刪改查,首先我們來完善CategoryService層的接口和實現:
- //CategoryService接口
- public interface CategoryService extends BaseService<Category> {
- public void save(Category category); //插入
- public void update(Category category);//更新
- public void delete(int id); //刪除
- public Category get(int id); //獲取一個Category
- public List<Category> query(); //獲取全部Category
- }
//CategoryService接口
public interface CategoryService extends BaseService<Category> {
public void save(Category category); //插入
public void update(Category category);//更新
public void delete(int id); //刪除
public Category get(int id); //獲取一個Category
public List<Category> query(); //獲取全部Category
}
對CategoryService接口的具體實現:
- public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {
- private SessionFactory sessionFactory;
- //Spring會注進來
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- protected Session getSession() {
- //從當前線程獲取session,如果沒有則創建一個新的session
- return sessionFactory.getCurrentSession();
- }
- @Override
- public void save(Category category) {
- getSession().save(category);
- }
- @Override
- public void update(Category category) {
- getSession().update(category);
- }
- @Override
- public void delete(int id) {
- /*第一種方法有個弊端,就是沒刪除一次得先查詢一次
- Object obj = getSession().get(Category.class, id);
- if(obj != null) {
- getSession().delete(obj);
- }*/
- String hql = "delete Category while id=:id";
- getSession().createQuery(hql) //
- .setInteger("id", id) //
- .executeUpdate();
- }
- @Override
- public Category get(int id) {
- return (Category) getSession().get(Category.class, id);
- }
- @Override
- public List<Category> query() {
- String hql = "from Category";
- return getSession().createQuery(hql).list();
- }
- }
public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {
private SessionFactory sessionFactory;
//Spring會注進來
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
//從當前線程獲取session,如果沒有則創建一個新的session
return sessionFactory.getCurrentSession();
}
@Override
public void save(Category category) {
getSession().save(category);
}
@Override
public void update(Category category) {
getSession().update(category);
}
@Override
public void delete(int id) {
/*第一種方法有個弊端,就是沒刪除一次得先查詢一次
Object obj = getSession().get(Category.class, id);
if(obj != null) {
getSession().delete(obj);
}*/
String hql = "delete Category while id=:id";
getSession().createQuery(hql) //
.setInteger("id", id) //
.executeUpdate();
}
@Override
public Category get(int id) {
return (Category) getSession().get(Category.class, id);
}
@Override
public List<Category> query() {
String hql = "from Category";
return getSession().createQuery(hql).list();
}
}
1.2 Service層抽取實現
完成了CategoryService後,我們來抽取Service層的基礎實現。思路是這樣的:我們抽取一個基礎接口BaseService以及基礎接口的實現BaseServiceImpl,後面開發的時候,如果需要新的Service,只需要做兩步即可:首先定義一個新的接口xxxService繼承BaseService接口,這個接口可以增加新的抽象方法;然後定義一個新的實現類xxxServiceImpl繼承BaseServiceImpl並實現xxxService接口即可。這樣更加便於項目的維護。
我們先根據上面的CategoryService接口來創建BaseService接口:
- //基礎接口BaseService,使用泛型
- public interface BaseService<T> {
- public void save(T t);
- public void update(T t);
- public void delete(int id);
- public T get(int id);
- public List<T> query();
- }
//基礎接口BaseService,使用泛型
public interface BaseService<T> {
public void save(T t);
public void update(T t);
public void delete(int id);
public T get(int id);
public List<T> query();
}
然後再根據CategoryServiceImpl實現類創建BaseService接口的實現類BaseServiceImpl:
- /**
- * @Description TODO(公共模塊的抽取)
- * @author eson_15
- *
- */
- @SuppressWarnings("unchecked")
- public class BaseServiceImpl<T> implements BaseService<T> {
- private Class clazz; //clazz中存儲了當前操作的類型,即泛型T
- private SessionFactory sessionFactory;
- public BaseServiceImpl() {
- //下面三個打印信息可以去掉,這裏是給自己看的
- System.out.println("this代表的是當前調用構造方法的對象" + this);
- System.out.println("獲取當前this對象的父類信息" + this.getClass().getSuperclass());
- System.out.println("獲取當前this對象的父類信息(包括泛型信息)" + this.getClass().getGenericSuperclass());
- //拿到泛型的參數類型
- ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
- clazz = (Class)type.getActualTypeArguments()[0];
- }
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- protected Session getSession() {
- //從當前線程獲取session,如果沒有則創建一個新的session
- return sessionFactory.getCurrentSession();
- }
- @Override
- public void save(T t) {
- getSession().save(t);
- }
- @Override
- public void update(T t) {
- getSession().update(t);
- }
- @Override
- public void delete(int id) {
- System.out.println(clazz.getSimpleName());
- String hql = "delete " + clazz.getSimpleName() + " as c where c.id=:id";
- getSession().createQuery(hql) //
- .setInteger("id", id) //
- .executeUpdate();
- }
- @Override
- public T get(int id) {
- return (T) getSession().get(clazz, id);
- }
- @Override
- public List<T> query() {
- String hql = "from " + clazz.getSimpleName();
- return getSession().createQuery(hql).list();
- }
- }
/**
* @Description TODO(公共模塊的抽取)
* @author eson_15
*
*/
@SuppressWarnings("unchecked")
public class BaseServiceImpl<T> implements BaseService<T> {
private Class clazz; //clazz中存儲了當前操作的類型,即泛型T
private SessionFactory sessionFactory;
public BaseServiceImpl() {
//下面三個打印信息可以去掉,這裏是給自己看的
System.out.println("this代表的是當前調用構造方法的對象" + this);
System.out.println("獲取當前this對象的父類信息" + this.getClass().getSuperclass());
System.out.println("獲取當前this對象的父類信息(包括泛型信息)" + this.getClass().getGenericSuperclass());
//拿到泛型的參數類型
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
clazz = (Class)type.getActualTypeArguments()[0];
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
//從當前線程獲取session,如果沒有則創建一個新的session
return sessionFactory.getCurrentSession();
}
@Override
public void save(T t) {
getSession().save(t);
}
@Override
public void update(T t) {
getSession().update(t);
}
@Override
public void delete(int id) {
System.out.println(clazz.getSimpleName());
String hql = "delete " + clazz.getSimpleName() + " as c where c.id=:id";
getSession().createQuery(hql) //
.setInteger("id", id) //
.executeUpdate();
}
@Override
public T get(int id) {
return (T) getSession().get(clazz, id);
}
@Override
public List<T> query() {
String hql = "from " + clazz.getSimpleName();
return getSession().createQuery(hql).list();
}
}
抽取完了後,我們就可以改寫CategoryService接口和CategoryServiceImpl實現類了。如下:
- //CategoryService接口繼承BaseService接口
- public interface CategoryService extends BaseService<Category> {
- /*
- * 只要添加CategoryService本身需要的新的方法即可,公共方法已經在BaseService中了
- */
- }
- /**
- * @Description TODO(模塊自身的業務邏輯)
- * @author eson_15
- *
- */
- public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {
- /*
- * 只需實現CategoryService接口中新增的方法即可,公共方法已經在BaseServiceImpl中實現了
- */
- }
//CategoryService接口繼承BaseService接口
public interface CategoryService extends BaseService<Category> {
/*
* 只要添加CategoryService本身需要的新的方法即可,公共方法已經在BaseService中了
*/
}
/**
* @Description TODO(模塊自身的業務邏輯)
* @author eson_15
*
*/
public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {
/*
* 只需實現CategoryService接口中新增的方法即可,公共方法已經在BaseServiceImpl中實現了
*/
}
從代碼中可以看出,新增的Service只需要繼承BaseService接口,然後在接口中新增本Service所需要的業務邏輯即可。新增的ServiceImpl只需要繼承BaseServiceImpl並實現新增的業務邏輯即可。但是別忘了很重要的一點:就是修改Spring的配置文件beans.xml中的bean。
- <!-- 泛型類是不能實例化的,所以要加lazy-init屬性 -->
- <bean id="baseService" class="cn.it.shop.service.impl.BaseServiceImpl" lazy-init="true">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="categoryService" class="cn.it.shop.service.impl.CategoryServiceImpl" parent="baseService"/>
<!-- 泛型類是不能實例化的,所以要加lazy-init屬性 -->
<bean id="baseService" class="cn.it.shop.service.impl.BaseServiceImpl" lazy-init="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="categoryService" class="cn.it.shop.service.impl.CategoryServiceImpl" parent="baseService"/>
將原來categoryService中的property幹掉,然後增加parent屬性,指明繼承baseService;然後配置一下baseService,將sessionFactory配到baseService中去,另外要注意一點:設置lazy-init屬性爲true,因爲baseService是泛型類,泛型類是不能實例化的。至此,Service層的抽取就搞定了。2. Service層添加一個Account
剛剛抽取好了Service層,那麼現在我們想寫一個Account(管理員)的service就很簡單了:
首先寫一個AccountService接口繼承BaseService:
- public interface AccountService extends BaseService<Account> { //注意BaseService裏的泛型現在是Account
- /*
- * 只要添加AccountService本身需要的新的方法即可,公共方法已經在BaseService中了
- */
- }
public interface AccountService extends BaseService<Account> { //注意BaseService裏的泛型現在是Account
/*
* 只要添加AccountService本身需要的新的方法即可,公共方法已經在BaseService中了
*/
}
然後寫一個AccountServiceImpl實現類繼承BaseServiceImpl實現類,並實現AccountService接口即可:- public class AccountServiceImpl extends BaseServiceImpl<Account> implements AccountService {
- /*
- * 只需實現AccountService接口中新增的方法即可,公共方法已經在BaseServiceImpl中實現了
- */
- //管理登陸功能,後期再完善
- }
public class AccountServiceImpl extends BaseServiceImpl<Account> implements AccountService {
/*
* 只需實現AccountService接口中新增的方法即可,公共方法已經在BaseServiceImpl中實現了
*/
//管理登陸功能,後期再完善
}
最後在beans.xml文件里加上如下配置:
- <bean id="accountService" class="cn.it.shop.service.impl.AccountServiceImpl" parent="baseService" />
<bean id="accountService" class="cn.it.shop.service.impl.AccountServiceImpl" parent="baseService" />
這樣就寫好了一個新的service了,以後需要添加service就遵循這個流程,非常方便。3. Action的抽取
3.1 Action中往域(request,session,application等)中存數據
我們知道,在Action中可以直接通過ActionContext.getContext()去獲取一個ActionContext對象,然後通過該對象再去獲得相應的域對象;也可以通過實現xxxAware接口來注入相應的域對象。我們先來看一下這兩種方法:
- public class CategoryAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
- private Category category;
- private CategoryService categoryService;
- public void setCategoryService(CategoryService categoryService) {
- this.categoryService = categoryService;
- }
- public String update() {
- System.out.println("----update----");
- categoryService.update(category);
- return "index";
- }
- public String save() {
- System.out.println("----save----");
- return "index";
- }
- public String query() {
- //解決方案一,採用相應的map取代原來的內置對象,這樣與jsp沒有依賴,但是代碼量比較大
- // ActionContext.getContext().put("categoryList", categoryService.query()); //放到request域中
- // ActionContext.getContext().getSession().put("categoryList", categoryService.query()); //放到session域中
- // ActionContext.getContext().getApplication().put("categoryList", categoryService.query()); //放到application域中
- //解決方案二,實現相應的接口(RequestAware,SessionAware,ApplicationAware),讓相應的map注入
- request.put("categoryList", categoryService.query());
- session.put("categoryList", categoryService.query());
- application.put("categoryList", categoryService.query());
- return "index";
- }
- public Category getCategory() {
- return category;
- }
- public void setCategory(Category category) {
- this.category = category;
- }
- private Map<String, Object> request;
- private Map<String, Object> session;
- private Map<String, Object> application;
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- }
public class CategoryAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Category category;
private CategoryService categoryService;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public String update() {
System.out.println("----update----");
categoryService.update(category);
return "index";
}
public String save() {
System.out.println("----save----");
return "index";
}
public String query() {
//解決方案一,採用相應的map取代原來的內置對象,這樣與jsp沒有依賴,但是代碼量比較大
// ActionContext.getContext().put("categoryList", categoryService.query()); //放到request域中
// ActionContext.getContext().getSession().put("categoryList", categoryService.query()); //放到session域中
// ActionContext.getContext().getApplication().put("categoryList", categoryService.query()); //放到application域中
//解決方案二,實現相應的接口(RequestAware,SessionAware,ApplicationAware),讓相應的map注入
request.put("categoryList", categoryService.query());
session.put("categoryList", categoryService.query());
application.put("categoryList", categoryService.query());
return "index";
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
還是上一節整合三大框架時的CategoryAction類,我們在裏面加了一個query方法,在該方法中,我們通過向request域、session域和application域中存入查詢的結果。第一種方法是直接使用ActionContext來實現,不需要實現任何接口,但是代碼量較大;第二種方法通過實現RequestAware、SessionAware和ApplicationAware接口,實現該接口的三個抽象方法把request、session和application注入進來,然後賦給相應的成員變量中,這樣就可以在query方法中向域中存放查詢結果了。這代碼量貌似比第一種方法更大……但是我們可以抽取,先往下看。我們在index.jsp中新加一個查詢連接來測試能否將查詢結果顯示出來:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- <a href="${pageContext.request.contextPath }/category_update.action?category.id=2&category.type=gga&category.hot=false">訪問update</a>
- <a href="category_save.action">訪問save</a>
- <a href="category_query.action">查詢所有類別</a><br/>
- <c:forEach items="${requestScope.categoryList }" var="category">
- ${category.id } | ${category.type } | ${category.hot } <br/>
- </c:forEach>
- <c:forEach items="${sessionScope.categoryList }" var="category">
- ${category.id } | ${category.type } | ${category.hot } <br/>
- </c:forEach>
- <c:forEach items="${applicationScope.categoryList }" var="category">
- ${category.id } | ${category.type } | ${category.hot } <br/>
- </c:forEach>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/category_update.action?category.id=2&category.type=gga&category.hot=false">訪問update</a>
<a href="category_save.action">訪問save</a>
<a href="category_query.action">查詢所有類別</a><br/>
<c:forEach items="${requestScope.categoryList }" var="category">
${category.id } | ${category.type } | ${category.hot } <br/>
</c:forEach>
<c:forEach items="${sessionScope.categoryList }" var="category">
${category.id } | ${category.type } | ${category.hot } <br/>
</c:forEach>
<c:forEach items="${applicationScope.categoryList }" var="category">
${category.id } | ${category.type } | ${category.hot } <br/>
</c:forEach>
</body>
</html>
3.2 抽取BaseAction
剛剛提到了,第二種方法的代碼量更大,但是我們可以抽取一個BaseAction,專門處理這些域相關的操作。
- public class BaseAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
- protected Map<String, Object> request;
- protected Map<String, Object> session;
- protected Map<String, Object> application;
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- }
public class BaseAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
然後我們自己的Action如果需要用到這些域對象來存儲數據時,直接繼承BaseAction即可,就能直接使用request、session和application對象了。所以修改後的CategoryAction如下:
- public class CategoryAction extends BaseAction {
- private Category category;
- <pre name="code" class="java">
- private CategoryService categoryService;
- public void setCategoryService(CategoryService categoryService) {
- this.categoryService = categoryService;
- }
public class CategoryAction extends BaseAction {
private Category category;
<pre name="code" class="java">
private CategoryService categoryService;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public String update() {System.out.println("----update----");categoryService.update(category); return "index"; }public String save() {System.out.println("----save----");return "index"; } public String query() {request.put("categoryList", categoryService.query());
session.put("categoryList", categoryService.query()); application.put("categoryList", categoryService.query()); return "index"; } public Category getCategory() { return category; } public void setCategory(Category category) {this.category = category; }}
後面所有要使用request、session和application域的Action,只要直接繼承BaseAction即可,非常方便。
3.3 獲取參數(ModelDriven)
我們繼續看上面的CategoryAction類,裏面有個成員變量category,這是個POJO,定義這個變量並寫好set和get方法是爲了JSP頁面可以通過url後面附帶參數傳進來,參數是category對象中的屬性,比如id,type等,但是url中的參數必須寫成category.id、category.type等。這樣struts會自動將這寫參數注入到category對象中,然後我們就可以直接使用這個category對象了,但是這樣有點繁瑣。我們可以使用ModelDriven來更方便的解決。
- public class CategoryAction extends BaseAction implements ModelDriven<Category>{
- private Category category;
- //使用ModelDriven接口必須要實現getModel()方法,此方法會把返回的項壓到棧頂
- @Override
- public Category getModel() {
- category = new Category();
- return category;
- }
- <pre name="code" class="java"> private CategoryService categoryService;
- public void setCategoryService(CategoryService categoryService) {
- this.categoryService = categoryService;
- }
- public String update() {
- System.out.println("----update----");
- categoryService.update(category);
- return "index";
- }
- public String save() {
- System.out.println("----save----");
- return "index";
- }
- public String query() {
- request.put("categoryList", categoryService.query());
- session.put("categoryList", categoryService.query());
- application.put("categoryList", categoryService.query());
- return "index";
- }
- }
public class CategoryAction extends BaseAction implements ModelDriven<Category>{
private Category category;
//使用ModelDriven接口必須要實現getModel()方法,此方法會把返回的項壓到棧頂
@Override
public Category getModel() {
category = new Category();
return category;
}
<pre name="code" class="java"> private CategoryService categoryService;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public String update() {
System.out.println("----update----");
categoryService.update(category);
return "index";
}
public String save() {
System.out.println("----save----");
return "index";
}
public String query() {
request.put("categoryList", categoryService.query());
session.put("categoryList", categoryService.query());
application.put("categoryList", categoryService.query());
return "index";
}
}
這樣我們在前臺JSP頁面就不用帶category.id這種繁瑣的參數了,看JSP頁面中的ModelDriven部分:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- </head>
- <body>
- <a href="${pageContext.request.contextPath }/category_update.action?category.id=2&category.type=gga&category.hot=false">訪問update</a>
- <a href="category_save.action?id=1&type=haha&hot=true">測試ModelDriven</a>
- <a href="category_query.action">查詢所有類別</a><br/>
- <c:forEach items="${requestScope.categoryList }" var="category">
- ${category.id } | ${category.type } | ${category.hot } <br/>
- </c:forEach>
- <c:forEach items="${sessionScope.categoryList }" var="category">
- ${category.id } | ${category.type } | ${category.hot } <br/>
- </c:forEach>
- <c:forEach items="${applicationScope.categoryList }" var="category">
- ${category.id } | ${category.type } | ${category.hot } <br/>
- </c:forEach>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/category_update.action?category.id=2&category.type=gga&category.hot=false">訪問update</a>
<a href="category_save.action?id=1&type=haha&hot=true">測試ModelDriven</a>
<a href="category_query.action">查詢所有類別</a><br/>
<c:forEach items="${requestScope.categoryList }" var="category">
${category.id } | ${category.type } | ${category.hot } <br/>
</c:forEach>
<c:forEach items="${sessionScope.categoryList }" var="category">
${category.id } | ${category.type } | ${category.hot } <br/>
</c:forEach>
<c:forEach items="${applicationScope.categoryList }" var="category">
${category.id } | ${category.type } | ${category.hot } <br/>
</c:forEach>
</body>
</html>
測試結果是可以獲得catgory,並且將id,type和hot屬性全部賦值好。我們可以看出,通過實現ModelDriven接口,我們可以很方便的在url中攜帶參數,Action中只需要實現getModel方法,new一個要使用的對象返回即可。到這裏我們很容易想到,struts中肯定會有很多這種model需要獲取,所以這一塊我們也要抽取到BaseAction中去。
3.4 抽取ModelDriven到BaseAction
首先我們在BaseAction中添加ModelDriven部分的代碼,如下:
- //因爲有很多不同的model都需要使用ModelDriven,所以這裏使用泛型
- public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {
- protected Map<String, Object> request;
- protected Map<String, Object> session;
- protected Map<String, Object> application;
- protected T model;
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- @Override
- public T getModel() { //這裏通過解析傳進來的T來new一個對應的instance
- ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
- Class clazz = (Class)type.getActualTypeArguments()[0];
- try {
- model = (T)clazz.newInstance();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return model;
- }
- }
//因爲有很多不同的model都需要使用ModelDriven,所以這裏使用泛型
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {
protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
protected T model;
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public T getModel() { //這裏通過解析傳進來的T來new一個對應的instance
ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
Class clazz = (Class)type.getActualTypeArguments()[0];
try {
model = (T)clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
return model;
}
}
抽取完了後,CategoryAction中的代碼會越來越少:
- //繼承BaseAction,並且加上泛型
- public class CategoryAction extends BaseAction<Category> {
- private CategoryService categoryService;
- public void setCategoryService(CategoryService categoryService) {
- this.categoryService = categoryService;
- }
- public String update() {
- System.out.println("----update----");
- categoryService.update(model);//直接使用model
- return "index";
- }
- public String save() {
- System.out.println("----save----");
- System.out.println(model); //直接使用model
- return "index";
- }
- public String query() {
- request.put("categoryList", categoryService.query());
- session.put("categoryList", categoryService.query());
- application.put("categoryList", categoryService.query());
- return "index";
- }
- }
//繼承BaseAction,並且加上泛型
public class CategoryAction extends BaseAction<Category> {
private CategoryService categoryService;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public String update() {
System.out.println("----update----");
categoryService.update(model);//直接使用model
return "index";
}
public String save() {
System.out.println("----save----");
System.out.println(model); //直接使用model
return "index";
}
public String query() {
request.put("categoryList", categoryService.query());
session.put("categoryList", categoryService.query());
application.put("categoryList", categoryService.query());
return "index";
}
}
到這裏,還有一個看着不爽的地方,就是categoryService這個成員變量,它一直存在在CategoryAction裏,因爲CategoryAction中有用到categoryService對象中的方法,所以必須得創建這個對象,並且有set方法才能注入進來。這就導致一個弊端:如果很多Action都需要使用categoryService的話,那就必須在它們的Action裏創建這個對象和set方法,而且,如果一個Action中要使用好幾個不同的service對象,那就得全部創建,這樣就變得很冗雜。
3.5 抽取service到BaseAction
針對上面的問題,我們將工程中所有的service對象都抽取到BaseAction中創建,這樣其他Action繼承BaseAction後,想用什麼service就直接拿來用即可:
- //我將BaseAction中的內容歸歸類了
- public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {
- //service對象
- protected CategoryService categoryService;
- protected AccountService accountService;
- public void setCategoryService(CategoryService categoryService) {
- this.categoryService = categoryService;
- }
- public void setAccountService(AccountService accountService) {
- this.accountService = accountService;
- }
- //域對象
- protected Map<String, Object> request;
- protected Map<String, Object> session;
- protected Map<String, Object> application;
- @Override
- public void setApplication(Map<String, Object> application) {
- this.application = application;
- }
- @Override
- public void setSession(Map<String, Object> session) {
- this.session = session;
- }
- @Override
- public void setRequest(Map<String, Object> request) {
- this.request = request;
- }
- //ModelDriven
- protected T model;
- @Override
- public T getModel() {
- ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
- Class clazz = (Class)type.getActualTypeArguments()[0];
- try {
- model = (T)clazz.newInstance();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return model;
- }
- }
//我將BaseAction中的內容歸歸類了
public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {
//service對象
protected CategoryService categoryService;
protected AccountService accountService;
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
//域對象
protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
//ModelDriven
protected T model;
@Override
public T getModel() {
ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
Class clazz = (Class)type.getActualTypeArguments()[0];
try {
model = (T)clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
return model;
}
}
這樣CategoryAction中就更加清爽了:
- public class CategoryAction extends BaseAction<Category> {
- public String update() {
- System.out.println("----update----");
- categoryService.update(model);
- return "index";
- }
- public String save() {
- System.out.println("----save----");
- System.out.println(model);
- return "index";
- }
- public String query() {
- request.put("categoryList", categoryService.query());
- session.put("categoryList", categoryService.query());
- application.put("categoryList", categoryService.query());
- return "index";
- }
- }
public class CategoryAction extends BaseAction<Category> {
public String update() {
System.out.println("----update----");
categoryService.update(model);
return "index";
}
public String save() {
System.out.println("----save----");
System.out.println(model);
return "index";
}
public String query() {
request.put("categoryList", categoryService.query());
session.put("categoryList", categoryService.query());
application.put("categoryList", categoryService.query());
return "index";
}
}
有人可能會問,BaseAction中注入了那麼多service對象的話不會冗餘麼?這是不會的,因爲就算不寫在BaseAction中,Spring容器也是會創建這個對象的,這點沒有關係,相反,service對象全放在BaseAction中更加便於其他Action的開發,而且BaseAction不需要配到struts.xml文件中,因爲根本就沒有哪個JSP會請求BaseAction,它只是讓其他Action來繼承用的。
還有一點別忘了:那就是修改在beans.xml中的配置:
- <!-- 如果是prototype類型,默認是使用時創建,不是啓動時自動創建 -->
- <bean id="baseAction" class="cn.it.shop.action.BaseAction" scope="prototype">
- <property name="categoryService" ref="categoryService"></property>
- <property name="accountService" ref="accountService"></property>
- </bean>
- <bean id="categoryAction" class="cn.it.shop.action.CategoryAction" scope="prototype" parent="baseAction"/>
<!-- 如果是prototype類型,默認是使用時創建,不是啓動時自動創建 -->
<bean id="baseAction" class="cn.it.shop.action.BaseAction" scope="prototype">
<property name="categoryService" ref="categoryService"></property>
<property name="accountService" ref="accountService"></property>
</bean>
<bean id="categoryAction" class="cn.it.shop.action.CategoryAction" scope="prototype" parent="baseAction"/>
新加一個baseAction的bean,將工程中所有service對象作爲property配好,將原來的categoryAction中的property幹掉。
以後我們如果要寫新的xxxAction,直接繼承BaseAction即可,如果xxxAction中有用到某個service,直接拿來用即可,只需要在beans.xml文件中加一個xxxAction對應的bean,在struts.xml文件中配置好跳轉即可。
4. 將xml改成註解
我們可以看到,隨着項目越寫越大,beans.xml中的配置會越來越多,而且很多配置有冗餘,爲了更加便於開發,我們現在將xml的配置改成註解的形式,我們先看一下beans.xml中的配置:
這些是我們之前搭建環境以及抽取的時候寫的bean,這些都需要轉換成註解的形式,下面我們一塊一塊的換掉:首先替換service部分,這部分有三個:baseService、categoryService和accountService。替換如下:
然後將beans.xml中的相應部分幹掉即可。接下來修改ActIon部分,主要有baseAction、categoryAction和accountAction三個,替換如下:
然後再幹掉beans.xml中的Action部分的配置即可,最後在beans.xml文件中添加一個如下配置,就可以使用註解了。
<context:component-scan base-package="cn.it.shop.."/>
有人可能會問,爲什麼service和action兩個使用註解的時候不一樣呢?service中使用的是@Service而action中使用的是@Controller呢?其實是一樣的,只是爲了區分它們是不同層的bean而已,便於閱讀。相關閱讀:http://blog.csdn.net/column/details/str2hiberspring.html
整個項目的源碼下載地址:http://blog.csdn.net/eson_15/article/details/51479994