自從上次project中使用struts1.1以後,對於開源的框架,就再也沒有使用過。
struts2開始就和webwork沒有什麼區別了,想了想還是看一下webwork吧,webwork是基於xwork的,而xwork是獨立於webcontainer而存在的,那麼就從xwork開始。
xwork是構築與command模式的。
對於command模式的講解,請看。
http://www.dofactory.com/Patterns/PatternCommand.aspx
xwork中command模式中的幾個類
命令的發出者:ActionProxy
命令的傳送者:ActionInvaction
命令的執行者:Action
類圖如下:
當然,xwork的內部調用沒有這麼簡單,使用xml配置文件來聯繫各個類。
現在用程序模擬以下xwork的comman模式工作方式。
首先是ActionProxy
ActionProxy.java
public interface ActionProxy {
public String execute() throws Exception;
}
ActionProxy是命令的發出着,也就是說client並不是直接讓Action執行的,而是讓ActionProxy來代理執行(雖然這兒看起來有點兒像代理模式,但是不是).
下面是ActionProxy的實現
package jp.co.wqf;
public class ActionProxyImpl implements ActionProxy {
Invocation invocation;
public ActionProxyImpl(Invocation invocation) {
this.invocation = invocation;
}
public String execute() throws Exception {
return invocation.invoke();
}
}
要注入一個Invocation讓其來傳送命令。
接下來是ActionInvocation
ActionInvocation.java
public interface Invocation {
public String invoke() throws Exception;
}
定義一個調用方法。
它的實現爲
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class InvocationImpl implements Invocation {
Iterator interceptors;
Action action;
Map resultMap;
boolean executed = false;
String resultCode;
public InvocationImpl(ArrayList interceptors, Action action, Map resultMap){
this.interceptors = interceptors.iterator();
this.action = action;
this.resultMap = resultMap;
}
public String invoke() throws Exception {
if(executed){
throw new Exception("Action has been execute!");
}
if(interceptors.hasNext()) {
Interceptor interceptor = (Interceptor) interceptors.next();
interceptor.intercept(InvocationImpl.this);
}else{
resultCode = action.exectute();
}
if(!executed){
Result result = (Result) resultMap.get(resultCode);
if(result!=null)
result.execute();
executed = true;
}
return resultCode;
}
}
這是一個比較主要的類,很多處理都是通過這兒來中轉的,首先它通過注入和遞歸調用來實現了方法攔截,然後再調用Action來執行真正的處理,並根據處理結果來調用不同的結果處理類(Result)
最後是Action接口
Action.java
public interface Action {
public String exectute();
}
Action接口裏定義了一個execute方法,用來執行某種操作,例如,數據庫的CRUD各個操作等,用一個String的返回值來表示執行結果,比如“success”,“error”等。實際上xwork支持任何的執行方法,不僅限於execute,只要在xwork.xml文件中指定執行哪個方法就可以了
以下是Action接口的實現類,這兒返回一個“error”字符串。
public class ActionImpl implements Action {
public String exectute() {
System.out.println("Action Execute");
//return "success";
return "error";
}
}
另外還有Interceptor和Result接口,實現類。
Interceptor.java
public interface Interceptor {
public void intercept(Invocation invocation) throws Exception;
}
InterceptorImpl.java
public class InterceptorImpl implements Interceptor {
private String name;
public InterceptorImpl(String name){
this.name = name;
}
public void intercept(Invocation invocation) throws Exception {
System.out.println(name+", start");
invocation.invoke();
System.out.println(name+", end");
}
}
Result.java
public interface Result {
public void execute();
}
ResultImpl.java
public class ResultImpl implements Result {
private String name;
public ResultImpl(String name) {
this.name = name;
}
public void execute() {
System.out.println("Result "+this.name+" execute!");
}
}
測試類如下:
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ArrayList interceptors = new ArrayList();
for (int i = 0; i < 5; i++) {
Interceptor inter = new InterceptorImpl("Interceptor_"+i);
interceptors.add(inter);
}
Result result = null;
Map resultMap = new HashMap();
result = new ResultImpl("success");
resultMap.put("success", result);
result = new ResultImpl("error");
resultMap.put("error", result);
Action action = new ActionImpl();
Invocation invocation = new InvocationImpl(interceptors, action, resultMap);
ActionProxy actionProxy = new ActionProxyImpl(invocation);
actionProxy.execute();
}
}
要點:
1.comman模式
2.IOC注入
3.遞歸調用(使用遞歸調用來實現方法攔截是個比較新穎的方法)
自從上次project中使用struts1.1以後,對於開源的框架,就再也沒有使用過。
struts2開始就和webwork沒有什麼區別了,想了想還是看一下webwork吧,webwork是基於xwork的,而xwork是獨立於webcontainer而存在的,那麼就從xwork開始。
xwork是構築與command模式的。
對於command模式的講解,請看。
http://www.dofactory.com/Patterns/PatternCommand.aspx
xwork中command模式中的幾個類
命令的發出者:ActionProxy
命令的傳送者:ActionInvaction
命令的執行者:Action
類圖如下:
當然,xwork的內部調用沒有這麼簡單,使用xml配置文件來聯繫各個類。
現在用程序模擬以下xwork的comman模式工作方式。
首先是ActionProxy
ActionProxy.java
public interface ActionProxy {
public String execute() throws Exception;
}
ActionProxy是命令的發出着,也就是說client並不是直接讓Action執行的,而是讓ActionProxy來代理執行(雖然這兒看起來有點兒像代理模式,但是不是).
下面是ActionProxy的實現
package jp.co.wqf;
public class ActionProxyImpl implements ActionProxy {
Invocation invocation;
public ActionProxyImpl(Invocation invocation) {
this.invocation = invocation;
}
public String execute() throws Exception {
return invocation.invoke();
}
}
要注入一個Invocation讓其來傳送命令。
接下來是ActionInvocation
ActionInvocation.java
public interface Invocation {
public String invoke() throws Exception;
}
定義一個調用方法。
它的實現爲
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class InvocationImpl implements Invocation {
Iterator interceptors;
Action action;
Map resultMap;
boolean executed = false;
String resultCode;
public InvocationImpl(ArrayList interceptors, Action action, Map resultMap){
this.interceptors = interceptors.iterator();
this.action = action;
this.resultMap = resultMap;
}
public String invoke() throws Exception {
if(executed){
throw new Exception("Action has been execute!");
}
if(interceptors.hasNext()) {
Interceptor interceptor = (Interceptor) interceptors.next();
interceptor.intercept(InvocationImpl.this);
}else{
resultCode = action.exectute();
}
if(!executed){
Result result = (Result) resultMap.get(resultCode);
if(result!=null)
result.execute();
executed = true;
}
return resultCode;
}
}
這是一個比較主要的類,很多處理都是通過這兒來中轉的,首先它通過注入和遞歸調用來實現了方法攔截,然後再調用Action來執行真正的處理,並根據處理結果來調用不同的結果處理類(Result)
最後是Action接口
Action.java
public interface Action {
public String exectute();
}
Action接口裏定義了一個execute方法,用來執行某種操作,例如,數據庫的CRUD各個操作等,用一個String的返回值來表示執行結果,比如“success”,“error”等。實際上xwork支持任何的執行方法,不僅限於execute,只要在xwork.xml文件中指定執行哪個方法就可以了
以下是Action接口的實現類,這兒返回一個“error”字符串。
public class ActionImpl implements Action {
public String exectute() {
System.out.println("Action Execute");
//return "success";
return "error";
}
}
另外還有Interceptor和Result接口,實現類。
Interceptor.java
public interface Interceptor {
public void intercept(Invocation invocation) throws Exception;
}
InterceptorImpl.java
public class InterceptorImpl implements Interceptor {
private String name;
public InterceptorImpl(String name){
this.name = name;
}
public void intercept(Invocation invocation) throws Exception {
System.out.println(name+", start");
invocation.invoke();
System.out.println(name+", end");
}
}
Result.java
public interface Result {
public void execute();
}
ResultImpl.java
public class ResultImpl implements Result {
private String name;
public ResultImpl(String name) {
this.name = name;
}
public void execute() {
System.out.println("Result "+this.name+" execute!");
}
}
測試類如下:
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ArrayList interceptors = new ArrayList();
for (int i = 0; i < 5; i++) {
Interceptor inter = new InterceptorImpl("Interceptor_"+i);
interceptors.add(inter);
}
Result result = null;
Map resultMap = new HashMap();
result = new ResultImpl("success");
resultMap.put("success", result);
result = new ResultImpl("error");
resultMap.put("error", result);
Action action = new ActionImpl();
Invocation invocation = new InvocationImpl(interceptors, action, resultMap);
ActionProxy actionProxy = new ActionProxyImpl(invocation);
actionProxy.execute();
}
}
要點:
1.comman模式
2.IOC注入
3.遞歸調用(使用遞歸調用來實現方法攔截是個比較新穎的方法)