-->

Using more than one result type in the same action

2019-02-28 06:01发布

问题:

I have used the result type stream in my action method which depends on an ajax call. This ajax call is activated on focus out of a textfield on my JSP. The function of this AJAX call is to display an error if the data entered by the user is already present in the backend server. Basically, the server returns a status code that specifies the outcome of the business logic i.e. in my case if my server returns 1016, the data is already present in the server and thus I display an error.

However, I also have to implement exception handling for the code in my action class method. In this case, what actually happens is that the page to which I want to forward on focus out gets jumbled up in the same place as where my error should have displayed and I get a very ugly output on my web page.

I tried everything I could to redirect to another page but nothing seems to work and all I get is 2 pages jumbled into one.

Here is my struts.xml code for the particluar method (as it was earlier):

<action name="useridCheck"
       class="com.action.registration.RegistrationAction" 
      method="isUserIdAvailable">
        <result type="stream">
            <param name="contentType">text/html</param>
            <param name="inputName">inputStream</param>
        </result>
</action>

This is the AJAX method :

 request = $.ajax({
              url: "ao/useridCheck.action",
              method: "POST",
              data: { customerLoginId : userid  },
              dataType: "text"
            });
            request.done(function( response ) {
                if(response!="") {
                             $('.error-customerId').html('<span class="clearfix"><img src="../images/ao/ico_error_small.png" alt="error message" class="pull-left error-message" /><span class="error-rgt">'+response+'</span></span>');
                             $('.error-customerId').prev().addClass('error-field');
                             v = 1;

                }
                else
                {
                    $('.error-customerId').text('');
                    $('.error-customerId').prev().removeClass('error-field');
                }
                //$('#customerLoginId').addClass('error-field');
              // $('.error-amount').prev().addClass('error-field');

            });

            request.fail(function( jqXHR, textStatus ) {
            alert( "Request failed: " + textStatus );

        });

What do I have to do in struts.xml or AJAX or in my action method so that the page is redirected on focus out?

回答1:

The error is logical: if you return an error result, or an error mapped to an exception, you are still returning a success result, because the relative httpheader (statusCode) will always be 200 (Success).

It is an error only to you, the browser (and hence the jQuery callback function) can't know it is an error result.

To enter the request.fail(function( jqXHR, textStatus ) { part, you need to send a different statusCode (4xx or 5xx), eg. 500 (Internal Server Error), otherwise you'll always enter the .done

To do this in Struts2, you can use:

  1. the Struts2 JSON plugin returning the following result as described here:

    <result name="error" type="json">
        <param name="errorCode">500</param>
    </result>
    
  2. the Struts2 HttpHeader result. Due to an error, the documentation is not available right now; I've opened a JIRA ticket for it, meanwhile you can check the Javadoc:

    <result name="error" type="httpheader">
        <param name="error">500</param>
    </result>