Java 在Ajax中获取操作方法结果状态

Java 在Ajax中获取操作方法结果状态,java,jsp,jquery,struts2,Java,Jsp,Jquery,Struts2,情况是这样的 在我的JSP页面中,我调用了一个action类方法 <input type="text" class="inputstyle" name="feedName" id="feedName" placeholder="<s:text name="global.feed_name" />" required> struts.xml <action name="feedCheck" method="feedCheck" class="com.analytic

情况是这样的

在我的JSP页面中,我调用了一个action类方法

<input type="text" class="inputstyle" name="feedName" id="feedName" placeholder="<s:text name="global.feed_name" />" required>
struts.xml

<action name="feedCheck" method="feedCheck" class="com.analytic.webapp.action.AAIDCAIndexAction">
            <result name="success">DCAAnalytix.jsp</result>
            <result name="error">DCAAnalytix.jsp</result>
        </action>

DCAAnalytix.jsp
DCAAnalytix.jsp
动作类方法

public String feedCheck()
    {
        MClient client = (MClient) getRequest().getSession().getAttribute(
                AAI_CLIENT);
        List<String> feedNamesFromDB=mFeedManager.getAllFeedNameByClient(client.getClientKey());
        System.out.println(feedName);
        if(feedNamesFromDB.size()>0){
            if(feedNamesFromDB.contains(feedName)){
                return ERROR;
            }
        }
        return SUCCESS;
    }
公共字符串feedCheck()
{
MClient客户端=(MClient)getRequest().getSession().getAttribute(
AAI_客户);
列出feedNamesFromDB=mFeedManager.getAllFeedNameByClient(client.getClientKey());
System.out.println(feedName);
如果(feedNamesFromDB.size()>0){
if(feedNamesFromDB.contains(feedName)){
返回误差;
}
}
回归成功;
}
ajax调用工作正常,调用action类方法并执行。但问题是,在Ajax中,结果总是出错。也就是说,如果该方法返回SUCCESS,则网页也会发出带有“notavailable!!!”


我不熟悉Ajax。当我搜索时,大多数帖子也是关于返回JSON数据的。我不想要JSON数据。我只需要结果状态以及如何在Ajax中获得它?

struts2中Ajax的结果类型应该是流。在你的代码中尝试这个

<action name="feedCheck" method="feedCheck" class="com.analytic.webapp.action.AAIDCAIndexAction">
  <result type="stream">
    <param name="contentType">text/html</param>
    <param name="inputName">inputStream</param>
  </result>
</action>
然后在你的方法中

public String feedCheck()
{
    MClient client = (MClient) getRequest().getSession().getAttribute(
            AAI_CLIENT);
    List<String> feedNamesFromDB=mFeedManager.getAllFeedNameByClient(client.getClientKey());
    System.out.println(feedName);
    if(feedNamesFromDB.size()>0)
    {
        if(feedNamesFromDB.contains(feedName))
        {
            this.setInputStream(new ByteArrayInputStream(ERROR.getBytes()));
        }
        else
        {
            this.setInputStream(new ByteArrayInputStream(SUCCESS.getBytes()));
        }
    }
    else
    {
        this.setInputStream(new ByteArrayInputStream(SUCCESS.getBytes()));
    }

    return SUCCESS;

}

流数据将返回字符串“error”或“success”。它将在您的成功ajax方法查看页面中提供。

您使用的是哪个版本的struts?谢谢。现在它只会走向成功。即使在Action类方法中将其设置为error,它也不会出错(始终显示“AVAILABLE!”警报)。我需要更改Ajax Jquery脚本中的任何内容吗?谢谢,第一个条件是总是正确的。也就是说,
如果(response.indexOf(“success”)==-1)
始终变为true并警告其相应的消息,请在操作方法中进行调试,并查看它返回的内容。从该方法返回的字符串仅在ajax成功中可用。您还可以通过mozilla的firebug监视ajax的请求和响应。我进行了调试,当条件为true时,它返回错误。我做了
alert(response.indexOf(“success”);警报(response.indexOf(“错误”))
和两者都用“-1”显示,这就是为什么它在
中的内容,如果
显示的是经过编辑的回答,请检查jsp页面中的ajax函数“success:function(data,success){”
private InputStream inputStream;
public String feedCheck()
{
    MClient client = (MClient) getRequest().getSession().getAttribute(
            AAI_CLIENT);
    List<String> feedNamesFromDB=mFeedManager.getAllFeedNameByClient(client.getClientKey());
    System.out.println(feedName);
    if(feedNamesFromDB.size()>0)
    {
        if(feedNamesFromDB.contains(feedName))
        {
            this.setInputStream(new ByteArrayInputStream(ERROR.getBytes()));
        }
        else
        {
            this.setInputStream(new ByteArrayInputStream(SUCCESS.getBytes()));
        }
    }
    else
    {
        this.setInputStream(new ByteArrayInputStream(SUCCESS.getBytes()));
    }

    return SUCCESS;

}
$(document).ready(function() {
        $('#feedName').blur(function() {
            var feedName=$("#feedName").val();
            if(feedName!="")
            {
                $.ajax( {
                      traditional: true,
                      type: "POST",      
                      url: "feedCheck",
                      data:"feedName="+feedName,
                      dataType: "text",
                      success: function(data, success) {
                          if(data.indexOf("success")==-1){
                                  alert("Action returned Error")
                           }else{
                                 alert("Action returned Success") 
                           }
                      }      
                    });
            }
        });
    });