【问题标题】:How to get Ajax response in Struts2如何在 Struts2 中获得 Ajax 响应
【发布时间】:2013-12-11 05:32:07
【问题描述】:

我已经参考了这个链接Ajax action calling 但我不能 嗨,使用 ajax 进行操作调用。 这是我的代码。

   $.ajax({
            type:'POST',
            dataType:'json',
            url:'ajaxAction?ajax_docno='+entry1+'-'+entry2+'-'+entry3+'-'+entry4,
        success:function(data,textStatus,jqXHR)
                   {
                     if(jqXHR.status==200)
                        alert("success");
                      }
                     if(jqXHR.status==500)
                       {
                           alert("Fail");
                          }
               }


               }
                   );

我的 STRUTS.XML

 <action name="ajaxAction" class="com.AjaxActionClass" method="checkExistence" > 
        <result name="success" type="httpheader">
                <param name="status" >200</param>
        </result>
        <result name="fail" type="httpheader">
                <param name="status" >500</param>
        </result>

    </action>

在 MY ACTION 类中

 public  String checkExistence()
   {
        //DB checkup
         if exists return success;
         if not exists return fail;
       }

一切正常

在我的操作类中,我正在检查 DB 中的数据是否存在并将一些值返回给 jSP 以显示消息。

我的问题是如果数据不在数据库中,如何将变量传递给 jsp 以向 USER 显示一些 alert 消息。

【问题讨论】:

    标签: ajax jsp struts2


    【解决方案1】:

    有3种方式:

    您可以发送 JSP 或 JSON,也可以只发送状态码。

    JSP

    $.ajax({
    ...
    success:function(data){
        alert(data);            //In case of JSP
    }
    
    <action name="myaction" class="actions.MyAction">
        <result name="success">/success.jsp</result>
        <result name="error">/error.jsp</result>
    </action>
    

    JSON

    $.ajax({
    ...
    success:function(data){
        var json=eval(data);       //Untested, but should give an idea - JSON
        alert(json.message); 
    }
    
    
     <action name="myaction" class="actions.MyAction">
        <result name="success" type="json"/>
        <result name="error" type="json"/>
    </action>         <!-- for JSON result, don't forget to extend json-default in the package-->
    

    状态

    $.ajax({
    ...
    success:function(data,textStatus,jqXHR){
       if(jqXHR.status==200){
           alert('success');
       }else if(jqXHR.status==201){
           alert('failure');
       }
    }
    
    
    <action name="myaction" class="actions.MyAction">
        <result name="success" type="httpheader">
             <param name="status">200</param>
        </result>
        <result name="error" type="httpheader">
             <param name="status">201</param>
        </result>
    </action>
    

    (Jquery Ajax 文档](http://api.jquery.com/jQuery.ajax/)

    【讨论】:

    • 我是否需要为 sataus 参数创建任何 getter 和 setter?我试过第三种情况。但运气不好,我现在要发布我的整个代码。
    • 我不知道你的回答有什么问题我可以使用以下代码statusCode:{500:function(){alert("The document number is alredy Exist");}, 200:function(){alert("Good Work !! You can proceed.");}
    • @looser 您的回答只是 jquery Ajax 的另一种变体。无论如何,我很高兴你解决了它。
    • @looser:“+1 为您的贡献”。我看不到 +1。
    • @looser +1 表示支持。
    【解决方案2】:

    感谢您的回复我没有您的回答有什么问题,但我现在可以使用以下代码做到这一点

      $.ajax({
            type:'POST',
            dataType:'json',
            url:'ajaxAction?ajax_docno='+entry1+'-'+entry2+'-'+entry3+'-'+entry4,
          statusCode:{500:function(){alert("The document number is alredy Exist");},
                            200:function(){alert("Good Work !! You can proceed.");}                     }
    
    
               }
                   );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多