在這里我不說struts2的驗證原理以及語法,我只講一下關于struts2驗證返回input視圖的解決辦法。
當然如果你是使用一個方法對一個類,則不存在上面的問題,當然一個方法對應一個類,這種方式雖然可讀性很高但是實際開發確實不可取,因為他會使類極具增加。如果你是使用一個類對應多個方法,你肯定碰到過這樣一個問題,那就是驗證時返回input視圖,struts2默認驗證失敗返回input視圖,但是我們在寫程序中經常會有多個方法需要驗證,打個比方如:create、update這兩個操作一般情況下都會驗證,但是我們把它寫在一個類中,我們如果區分驗證失敗后,到底是create失敗了還是update失敗了呢?這個時候有沒有懷疑過struts2,呵呵。
當然struts2其時已經給了我們解決辦法,如果你是一個細心的人讀過struts2源碼便過發現struts2是如何解決問題的,在這里我也簡單的分析一下struts2的源碼,大家請看下面這個類。
package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.lang.reflect.Method;
public class DefaultWorkflowInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 7563014655616490865L;
private static final Logger LOG = LoggerFactory.getLogger(DefaultWorkflowInterceptor.class);
private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
//默認返回input視圖,是在這里定義的
private String inputResultName = Action.INPUT;
//可以在這里更改,一般沒有人會這么做
public void setInputResultName(String inputResultName) {
this.inputResultName = inputResultName;
}
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof ValidationAware) {
ValidationAware validationAwareAction = (ValidationAware) action;
if (validationAwareAction.hasErrors()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Errors on action " + validationAwareAction + ", returning result name 'input'");
}
//one
String resultName = inputResultName;
/*
在這里大家讀一下源碼即可明白,當前處理的Action如果是一個ValidationWorkflowAware類型的,
則調用他的getInputResultName作用返回值
為了方便我直接把ValidationWorkflowAware放在下面的注釋中,大家看他是一個接口,
也就是說如果我們的Action實現了ValidationWorkflowAware接口
他則會調用getInputResultName方法返回的值,而非input,而默認的ActionSupport沒有實現這個接口,我們需要手動實現
*/
//package com.opensymphony.xwork2.interceptor;
//public interface ValidationWorkflowAware {
// String getInputResultName();
//}
if (action instanceof ValidationWorkflowAware) {
resultName = ((ValidationWorkflowAware) action).getInputResultName();
}
//這里不做講述
InputConfig annotation = action.getClass().getMethod(invocation.getProxy().getMethod(), EMPTY_CLASS_ARRAY).getAnnotation(InputConfig.class);
if (annotation != null) {
if (!annotation.methodName().equals("")) {
Method method = action.getClass().getMethod(annotation.methodName());
resultName = (String) method.invoke(action);
} else {
resultName = annotation.resultName();
}
}
return resultName;
}
}
return invocation.invoke();
}
}
大家看到上面是不是已經恍然大悟了,呵呵,是的我們實現了ValidationWorkflowAware接口之后,只需要定義一個inputResultName屬性生成了對應的get、set方法是不是就對應有了getInputResultName,而這個屬性的值我們可以動態傳入一個值進來,呵呵,大家看下面這個實例。
public abstract class ActionSupport extends
com.opensymphony.xwork2.ActionSupport implements Result,
ValidationWorkflowAware {
private static final long serialVersionUID = 799075559195465128L;
public static final int ERROR_MSG = -1;
public static final int WARN_MSG = 0;
public static final int SUCCESS_MSG = 1;
public static long getSerialversionuid() {
return serialVersionUID;
}
private ActionContext actionContext;
private Object id;
private Pagination pagn = new Pagination();
private QueryResult<?> results;
private String inputResultName;
/**
* 初始化ActionContext對象
*/
public ActionSupport() {
actionContext = ActionContext.getContext();
}
/**
*
* @return ActionContext對象
*/
public ActionContext getActionContext() {
return actionContext;
}
/**
*
* @return Struts封裝后的ServletContext對象。
*/
public Map<String, Object> getApplication() {
return actionContext.getApplication();
}
/**
*
* @return 取得標識。
*/
public Object getId() {
return id;
}
/**
* 取得指定類型的標識。
*
* @param <E>
* @param c
* @return
*/
@SuppressWarnings("unchecked")
public <E> E getId(Class<E> c) {
return (E) id;
}
/**
*
* @return 輸出對象。
* @throws IOException
*/
public PrintWriter getOut() throws IOException {
return getResponse().getWriter();
}
/**
*
* @return 分頁參數對象。
*/
public Pagination getPagn() {
return pagn;
}
/**
*
* @return HttpServletRequest對象。
*/
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
/**
*
* @return HttpServletResponse對象。
*/
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
/**
*
* @return 查詢結果集。
*/
public QueryResult<?> getResults() {
return results;
}
/**
*
* @return ServletContext對象。
*/
public ServletContext getServletContext() {
return (ServletContext) this.actionContext
.get(StrutsStatics.SERVLET_CONTEXT);
}
/**
*
* @return Struts封裝后的HttpSession對象。
*/
public Map<String, Object> getSession() {
return actionContext.getSession();
}
/**
*
* @return Struts的ValueStack對象。
*/
public ValueStack getValueStack() {
return ServletActionContext.getValueStack(getRequest());
}
/**
* 向ActionContext中添加一個信息,此信息會保存到HttpServletRequest中。
*
* @param key
* 鍵。
* @param value
* 值。
*/
public void put(String key, Object value) {
actionContext.put(key, value);
}
public void setActionContext(ActionContext actionContext) {
this.actionContext = actionContext;
}
/**
*
* @param id
* 設置標識。
*/
public void setId(Object id) {
this.id = id;
}
/**
*
* @param pagn
* 設置分頁參數對象。
*/
public void setPagn(Pagination pagn) {
this.pagn = pagn;
}
/**
*
* @param results
* 設置返回的結果集。
*/
protected void setResults(QueryResult<?> results) {
this.results = results;
}
public String getInputResultName() {
return inputResultName;
}
public void setInputResultName(String inputResultName) {
this.inputResultName = inputResultName;
}
public abstract String show() throws Exception;
public abstract String edit() throws EditFailureException;
public abstract String destroy() throws DestroyFailureException;
public abstract String create() throws CreateFailureException;
public abstract String deleteConfirm() throws DeleteFailureException;
public abstract String index() throws Exception;
public abstract String update() throws UpdateFailureException;
public abstract String editNew() throws EditFailureException;
}
上面是我自定義一個ActionSupport類,該類實現了ValidationWorkflowAware,并重寫了getInputResultName方法。然后我再定義一個Action繼承了該類。
@Namespace(value = "/test")
@Action(params = { "actionName", "demo" })
@Results( {
@Result(name = "xx", type = "redirect", location = "http://www.google.com"),
@Result(name = "hello", type = "redirect", location = "http://www.baidu.com") })
@SuppressWarnings("serial")
public class DownloadController extends ActionSupport {
public String index() {
System.out.println("-------index-----------");
return "xx";
}
public void validateIndex() {
addFieldError("hell", ".my hello.");
System.out.println("ok");
}
//..省略了其它無關方法
}
在上面我就只是做了一個簡單了模擬驗證然后跳轉到指定的頁面。這里你可以這樣請求,測試一個最終struts2是否調用了getInputResultName方法并使用其返回值,作為返回視圖的名稱:http://地址:端口/project/test/demo!index.action?inputResultName=hello,大家測試如果跳轉到了baidu就說明成功了。
至此,有問題可以留言。
轉載時請注明轉載地址,onlyeffort.QQ:501276913
posted on 2010-06-08 20:13
Aidan 閱讀(6119)
評論(7) 編輯 收藏