ExecAndWaitInterceptor不能与Jquery ajax调用一起使用

时间:2015-01-21 10:18:34

标签: struts2

我有一个使用ExecAndWait Interceptor的工作动作。我的等待页面是

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

是否可以将ExecAndWait Interceptor与JQuery Ajax调用一起使用?如果是这样,我做错了什么?

问候

修改

这里要求的是行动

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>

1 个答案:

答案 0 :(得分:0)

好的,找到了。

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

更改行

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

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

修复了问题,现在一切都按预期运行