在struts 2中的同一个动作方法中使用多个结果类型? [英] Using more than one result type in the same action method in struts 2?

查看:196
本文介绍了在struts 2中的同一个动作方法中使用多个结果类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在我的action方法中使用了结果类型流,这取决于ajax调用。这个ajax调用是在我的JSP上的文本字段焦点上激活的。此AJAX调用的功能是,如果用户输入的数据已存在于后端服务器中,则显示错误。基本上,服务器返回一个状态代码,指定业务逻辑的结果,即在我的情况下,如果我的服务器返回1016,数据已经存在于服务器中,因此我显示错误。



但是,我还必须在我的action类方法中为代码实现异常处理。在这种情况下,实际发生的事情是我想要转移到焦点的页面在我的错误应该显示的位置混乱,我的网页上输出非常难看。



我尽我所能重定向到另一个页面,但似乎没有任何效果,我得到的只是2页混乱到一个。



<这是我的particluar方法的struts.xml代码(如前所述):

 < 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>

这是AJAX方法:

  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.pngalt =错误消息class =pull-left error-message/>< span class =error-rgt> ;'+ response +'< / span>< / span>');
$('。error-customerId')。prev()。addClass('error-field');
v = 1;

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

});

request.fail(函数(jqXHR,textStatus){
alert(请求失败:+ textStatus);

});

我在struts.xml或AJAX或我的操作方法中需要做什么才能使页面焦点被重定向?

解决方案

错误是合乎逻辑的:如果您返回错误结果或者映射到异常的错误,您仍然返回成功结果,因为相对的httpheader(statusCode)将始终 200 (成功)。



这只是一个错误,浏览器(以及jQuery回调函数)无法知道它是错误结果。



要输入 request.fail(函数(jqXHR,textStatus){部分,您需要发送不同的statusCode( 4xx 5xx ),例如 500 (内部服务器错误),否则您将始终输入 .done



要在Struts2中执行此操作,您可以使用:


  1. Struts2 JSON plu gin 返回以下结果如此处所述



    < pre class =lang-xml prettyprint-override> < result name =errortype =json>
    < param name =errorCode> 500< / param>
    < / result>


  2. Struts2 HttpHeader结果。由于错误,文档目前无法使用;我为它开了一个 JIRA票,同时你可以检查Javadoc

     < result name =error类型= 的HTTPHeader > 
    < param name =error> 500< / param>
    < / result>



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?

解决方案

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>
    

这篇关于在struts 2中的同一个动作方法中使用多个结果类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆