<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>


    草之戒_
    posts - 2,comments - 14,trackbacks - 0
          在這里我不說struts2的驗證原理以及語法,我只講一下關(guān)于struts2驗證返回input視圖的解決辦法。
          當(dāng)然如果你是使用一個方法對一個類,則不存在上面的問題,當(dāng)然一個方法對應(yīng)一個類,這種方式雖然可讀性很高但是實際開發(fā)確實不可取,因為他會使類極具增加。如果你是使用一個類對應(yīng)多個方法,你肯定碰到過這樣一個問題,那就是驗證時返回input視圖,struts2默認驗證失敗返回input視圖,但是我們在寫程序中經(jīng)常會有多個方法需要驗證,打個比方如:create、update這兩個操作一般情況下都會驗證,但是我們把它寫在一個類中,我們?nèi)绻麉^(qū)分驗證失敗后,到底是create失敗了還是update失敗了呢?這個時候有沒有懷疑過struts2,呵呵。
          當(dāng)然struts2其時已經(jīng)給了我們解決辦法,如果你是一個細心的人讀過struts2源碼便過發(fā)現(xiàn)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;
                    
    /*
                    在這里大家讀一下源碼即可明白,當(dāng)前處理的Action如果是一個ValidationWorkflowAware類型的,
                    則調(diào)用他的getInputResultName作用返回值
                    為了方便我直接把ValidationWorkflowAware放在下面的注釋中,大家看他是一個接口,
                    也就是說如果我們的Action實現(xiàn)了ValidationWorkflowAware接口
                    他則會調(diào)用getInputResultName方法返回的值,而非input,而默認的ActionSupport沒有實現(xiàn)這個接口,我們需要手動實現(xiàn)

                    
    */
                    
    //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();
        }

    }
          大家看到上面是不是已經(jīng)恍然大悟了,呵呵,是的我們實現(xiàn)了ValidationWorkflowAware接口之后,只需要定義一個inputResultName屬性生成了對應(yīng)的get、set方法是不是就對應(yīng)有了getInputResultName,而這個屬性的值我們可以動態(tài)傳入一個值進來,呵呵,大家看下面這個實例。


    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 分頁參數(shù)對象。
         
    */
        
    public Pagination getPagn() {
            
    return pagn;
        }

        
    /**
         * 
         * 
    @return HttpServletRequest對象。
         
    */
        
    public HttpServletRequest getRequest() {
            
    return ServletActionContext.getRequest();
        }

        
    /**
         * 
         * 
    @return HttpServletResponse對象。
         
    */
        
    public HttpServletResponse getResponse() {
            
    return ServletActionContext.getResponse();
        }

        
    /**
         * 
         * 
    @return 查詢結(jié)果集。
         
    */
        
    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
         *            設(shè)置標識。
         
    */
        
    public void setId(Object id) {
            
    this.id = id;
        }

        
    /**
         * 
         * 
    @param pagn
         *            設(shè)置分頁參數(shù)對象。
         
    */
        
    public void setPagn(Pagination pagn) {
            
    this.pagn = pagn;
        }

        
    /**
         * 
         * 
    @param results
         *            設(shè)置返回的結(jié)果集。
         
    */
        
    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類,該類實現(xiàn)了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");
        }
        
    //..省略了其它無關(guān)方法
    }
          在上面我就只是做了一個簡單了模擬驗證然后跳轉(zhuǎn)到指定的頁面。這里你可以這樣請求,測試一個最終struts2是否調(diào)用了getInputResultName方法并使用其返回值,作為返回視圖的名稱:http://地址:端口/project/test/demo!index.action?inputResultName=hello,大家測試如果跳轉(zhuǎn)到了baidu就說明成功了。
          至此,有問題可以留言。


    轉(zhuǎn)載時請注明轉(zhuǎn)載地址,onlyeffort.QQ:501276913
    posted on 2010-06-08 20:13 Aidan 閱讀(6120) 評論(7)  編輯  收藏

    FeedBack:
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-08 23:15 | 18傲骨中文
    收藏慢慢看~~  回復(fù)  更多評論
      
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-09 08:17 | quaff
    請使用@InputConfig

    public class DownloadController extends ActionSupport {

    @InputConfig(methodName="inputIndex")
    //@InputConfig(resultName="input")
    public String index() {
    System.out.println("-------index-----------");
    return "xx";
    }

    public void validateIndex() {
    addFieldError("hell", ".my hello.");
    System.out.println("ok");
    }

    public string inputIndex() {
    return "input";
    }
    }  回復(fù)  更多評論
      
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-09 08:46 | 雪山飛鵠
    通常都是自己在validator方法中做判斷,沒研究過  回復(fù)  更多評論
      
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-09 11:34 | Aidan
    @quaff
    Thank you.使用@InputConfig能更簡單的完成任務(wù),而且可讀性更高。  回復(fù)  更多評論
      
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-09 23:04 | aaaaaa
    struts2早提供了每個方法一個validate,就是會執(zhí)行validateXXX方法,比如業(yè)務(wù)方法update,那么會執(zhí)行validateUpdate.  回復(fù)  更多評論
      
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-10 08:39 | Aidan
    @aaaaaa
    不好意思,我們討論的不是一個話題。  回復(fù)  更多評論
      
    # re: Struts2-疑難雜癥之Validator返回input視圖
    2010-06-10 08:50 | quaff
    @Aidan
    這是我提交給xwork的,看你的需求就知道這個功能就是為這個打造的  回復(fù)  更多評論
      

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    編程部落   qq群:37996359(上限500人,一起關(guān)注java、討論技術(shù),互相學(xué)習(xí),共同進步)
    主站蜘蛛池模板: 亚洲午夜在线电影| 亚洲高清乱码午夜电影网| 精品国产亚洲一区二区在线观看| fc2免费人成在线视频| 亚洲AV日韩AV天堂久久| 在线jlzzjlzz免费播放| 中文字幕的电影免费网站| 亚洲人成人77777在线播放| 最新黄色免费网站| 亚洲天堂中文字幕| 免费高清av一区二区三区| 国产精品偷伦视频免费观看了| 亚洲小说图片视频| 亚洲国产午夜中文字幕精品黄网站| 久久免费国产视频| 黄色一级视频免费| 亚洲美女一区二区三区| 亚洲成a人片在线观看日本麻豆| 91久久成人免费| 国产免费人成视频在线播放播 | 国产免费131美女视频| 午夜免费福利小电影| 丰满少妇作爱视频免费观看| 亚洲成人黄色在线观看| 国产专区一va亚洲v天堂| 免费无码看av的网站| h视频在线免费看| 中文毛片无遮挡高清免费| 亚洲Av永久无码精品黑人| 久久亚洲精品无码aⅴ大香| 久久精品亚洲男人的天堂| 精品国产麻豆免费网站| **aaaaa毛片免费| 在线观看免费播放av片| jizz在线免费观看| 亚洲AV无码成人网站在线观看| 亚洲制服丝袜精品久久| 亚洲第一精品在线视频| 亚洲一区二区三区在线观看精品中文 | 亚洲av一本岛在线播放| 亚洲视频免费一区|