سؤال

i am calling struts2 action from jquery ajax on success it returns string and on error it should be dispatched to same page and should display errors. i have used following code check it..

$(document).ready(function(){
            $('#getActionRs').click(function(){
                alert("call action");
                $.ajax({
                    type:"POST",
                    url: "returnToAjax",
                    data: "firstinput=" +$('#firstinput').val()+"&secondinput=" +$('#secondinput').val(),
                    success: function(msg){
                        alert("success:"+msg);
                    }
                    });
            });
        });

i have used above code to call my struts action. onclick of button this thing get called

i have specified my action element in config file as follows

<action name="returnToAjax" class="example.returnToAjax">
      <result name="success" type="stream">
         <param name="contentType">text/html</param>
         <param name="inputName">inputStream</param>
      </result>
      <result name="input" type="dispatcher">/Login.jsp</result> 
    </action>

when it returns success it return string correctly, i have done some validation of this action by validation xml but when it returns error it just shows me Login.jsp file code it does not dispatches it to Login.jsp

هل كانت مفيدة؟

المحلول

You can follow this procedure :

  1. Form submit
  2. Validation error (checked through validation.xml)
  3. pass error.jsp as input result where error.jsp contains a word, suppose say ERRORPAGE
  4. you'll get error.jsp via the ajax response.
  5. check out the response content if it starts from ERRORPAGE, do a javascript redirect to login.jsp

Here's a code snippet of the example which does struts2 jquery grid validation through ajax.

afterSubmit:function(response,postdata){
                  return isError(response.responseText);
                  }

<script type="text/javascript">
            function isError(text){
                if(text.indexOf('ERRORPAGE')>=0){
                    return [false,text];  //do redirection here something like
                    //window.location="/login.action";
                }
                return [true,''];
            }
        </script>

نصائح أخرى

you are doing things fundamentally wrong.Ajax is something which means doing a backed process without refreshing page and letting user stay on the same page.

Your approach is something what not in the right scope of Ajax principal.So even when you have validation failure in you action class control is coming back to same handler in your Jquery code and since you have specified the Login.jsp as the view template the steam result is picking the whole jsp and returning back its content.

If you want to go with same approach, just return action name from the Action if validation failed and den redirect user to input page using JavaScript form submit function.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top