ExecAndWaitInterceptor 不适用于 Jquery ajax 调用

ExecAndWaitInterceptor doesn't work with Jquery ajax call

我有一个正在使用 ExecAndWait 拦截器的工作操作。我的等待页面是

<%@ page language="java" pageEncoding="UTF-8" session="false"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="2"/>"/>
</head>
<body>  
<h3>Waiting</h3>
<div id="wait-result">
</div>
</body>
</html>

这工作正常,并为我的操作添加了一个断点我可以看到无论刷新次数如何,该操作只被调用一次。应该的。

现在,如果我删除元刷新标签并将其替换为 JQuery 脚本 重新加载页面,我看到的是每个请求都会调用我的操作,但最终结果页面永远不会到达。

<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
<script>

var repeat = false;

function executeQuery() {

    $.ajax({
    type: 'GET',
    url: 'search',
    async: false,
    success: function(data) {

      if ($(data).find('#wait-result')) {
        repeat = true;
      } else {
          repeat = false;
          $('#wait-result').html(data);
      }


    }

  });

    if (repeat) {
          setTimeout(executeQuery, 1000);
      }
}

$(document).ready(function() {
    setTimeout(executeQuery, 1000);
});

</script>

</head>
<body>  
<h3>Waiting</h3>
<div id="wait-result">
</div>
</body>
</html>

是否可以通过 JQuery Ajax 调用使用 ExecAndWait 拦截器?如果是这样,我做错了什么?

此致

编辑

这里要求的是动作

package com.harkonnen.actions.search;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.harkonnen.services.filter.ZeroResFilter;
import com.harkonnen.services.search.SearchService;
import com.harkonnen.services.search.Town;

@Component
@Scope("protototype")
@Namespace("/search")
@ParentPackage("search")
@InterceptorRefs({
    @InterceptorRef(value="secureStack"),
    @InterceptorRef(value="execAndWait", params={"delay", "500", "delaySleepInterval","500"}
)
})
@Results({
    @Result(name="input", location="start.jsp"),
    @Result(name="success", type="redirectAction", location="start" ),
    @Result(name="wait", location="wait.jsp")

})
public class Search extends BaseAction  {

private int x;
private int y;
private int radius;
private List<Town> towns;



@Autowired
SearchService service;

private static final Logger logger = Logger
        .getLogger(Search.class.getName());

public String execute() {

    try {
        towns = new ZeroResFilter(service.search(x,y,radius)).filter();
        System.out.println("Search Complete");
    } catch (SQLException e) {
        logger.error(e);
        addActionError("Sorry, there was an unexpected error with your query.");
        return INPUT;
    }



    if (towns.size() == 0) {
        addActionError("There were no results found for the specified search.");
        return INPUT;
    }

    context.setSearchX(x);
    context.setSearchY(y);
    context.setRadius(radius);
    context.setResults(new ArrayList<Town>(towns));
    context.setTowns(towns);

    return SUCCESS;
}


public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}
public int getRadius() {
    return radius;
}
public void setRadius(int radius) {
    this.radius = radius;
}


}

基本动作

package com.harkonnen.actions.search;

import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.harkonnen.services.SearchContext;
import com.opensymphony.xwork2.ActionSupport;

@Component
@Scope("protototype")
@Namespace("/search")
public class BaseAction extends ActionSupport implements SessionAware {

protected SearchContext context;

protected boolean isLoggedIn() {

    return true;
}

@Override
public void setSession(Map<String, Object> session) {
    this.context = (SearchContext) session.get("context");
}


public SearchContext getContext() {
    return context;
}

}

成功jsp

<%@ page language="java" pageEncoding="UTF-8" session="false"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>

<head>

<style type="text/css">@import "<%=request.getContextPath()%>/resources/css/global.css";</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>HoTH Search</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script>
</head>
<body id="body">

<div id="container">

<div id="login">
    <s:action var="login" name="login" executeResult="true"/>
</div>

<div id="perform"> 
    <s:action var="search" name="action-prompt" executeResult="true"/>
    <s:actionerror/>
</div>

<div id="results">
<s:action var="results" name="display-search-results" executeResult="true"/>
</div>


<div id="footer-wrapper">
    <p>Copyright &copy;2015 Harkonnen Solutions.</p>
</div>
</div>

</body>
</html>

好的,找到了。

问题在于尝试将对 GET 请求的响应解析为 HTML 对象,然后尝试 运行 选择器。

换行

if ($(data).find('#wait-result')) {

if (data.indexOf('#wait-result') >=0) {

解决了问题,现在一切 运行 都符合预期