1

I am using Struts 2.1.6 with Dojo plugin, whole app has ajax links (sx:a).

Did anybody succeed to implement back button functionality and linking to certain content?

Does anybody have any experience how to implement? I am planning to implement (if there is no good solution already) something like so:

  • changing address bar link (adding parameters) with js which I can then read and get proper content and then publish it with notifyTopics.

Or should I just change whole app to use jQuery plugin? Do jQuery has good solutions for back button and linking on ajax pages?

4

4 回答 4

1

我可以想到两种简单的方法:

<s:form action="actionName">
    <input type="hidden" value="<s:property value="someProperty"/>" name="someProperty"/>
    <input type="hidden" value="<s:property value="someProperty2"/>" name="someProperty2"/>
    <s:submit value="Back" />
</s:form>

或者

<s:url name="backURL" action="actionName">
    <s:param name="someProperty" value="someProperty"/>
    <s:param name="someProperty2" value="someProperty2"/>
</s:url>
<a href="<s:property value="#backURL"/>">Back</a>

如果您已经有查询字符串参数:

<a href="#" onclick="javascript.window=document.referrer;">Back</a>

或者

<input type="button" value="Back" onclick="javascript.window=document.referrer;"/>
于 2009-12-08T15:11:15.237 回答
1

我曾尝试将 Struts 2 与 dojo 一起使用并实现后退按钮。您已经超越了 Struts 2 的 ajax 实现。他们主要使用和编写它来编写简单快速的 ajax 函数调用,不太适合更广泛的用途。加上当你做 s:head theme='ajax' tag; struts 将导入您“可能”需要的每个 ajax js 文件,这会缩短加载时间。

我建议要么 1. 学习 dojo 并使用独立于 struts 的库 2. 或者 2. 获取 jQuery,我能够实现一个相对简单的后退按钮功能(比 struts 2 theme='ajax' 更是如此)。

于 2009-12-08T15:30:40.447 回答
0

我的所有链接都通过一个查找参数 menuId 的操作(当然是必须显示的菜单的 id)。

从这个动作中,在返回响应之前,我设置了一个要调用的 javascript 函数:

setBackMenuId(menuId,sometext) 

MenuId 是 id,sometext 是该菜单的名称,因此浏览器日志历史记录更好。

function setBackMenuId(id,subtekst) {
 window.location.hash = "menuId="+id;
 document.title = subtekst;
 selectedHash = document.location.hash;
 if(intervalHashSearch == '') {
    initializeHashSearch();
 } 
}

然后,其他需要的js函数:

function publishLinkTarget() {
    var param = window.location.hash;
    if(param) {
        if(param.indexOf("menuId") > 0) {
            var id = param.split("=", 2);
            if(id[1]) {
                setCookie('backMenuId',id[1],1,false);
                setTimeout('publishLayoutContent()', 100);
            }
        }
    }
}

var selectedHash = '';
var intervalHashSearch = '';
function initializeHashSearch() {
    intervalHashSearch = window.setInterval('checkHash()', 500);
}

function stopHashSearch() {
    window.clearInterval(intervalHashSearch);
    intervalHashSearch = '';
}

function checkHash() {
    var currentHash = document.location.hash;
    if(selectedHash != currentHash) {
        selectedHash = currentHash;
        publishLinkTarget();
    }
}

function publishLayoutContent() {
    dojo.event.topic.publish("layoutContentTarget");
}

如果你看它你会发现,它首先被称为'setBackMenuId',它将哈希和参数添加到地址栏并更改标题,然后记住这个哈希,因此间隔哈希搜索可以找出差异。然后它初始化这个哈希搜索。

'checkHash' 运行 500 毫秒,并检查哈希是否已更改(这意味着,按下后退按钮,而不是单击新链接(setBackMenuId 设置 selectedHash)。如果为真(按下后退/前进按钮)函数'调用publishLinkTarget',它从hash中读取参数,如果没问题,首先我设置一个cookie,这样我就可以从HttpServletRequest中读取它并找出哪个菜单id链接。如果我在这里意味着我还必须发布使用“publishLayoutContent”制作的内容。

在动作类中(这是MenuAction,方法视图,与发布的相同)只有这一点很重要:

    Integer menuId = null;
    if(request.getParameter("menuId") != null) {
        menuId = Integer.valueOf(request.getParameter("menuId"));
    } else {
        menuId = getIntCookie("hiddenMenuId");
    } 

因此,如果我没有从参数中获取菜单 ID(单击链接),我会从 cookie(后退/前进按钮)中获取。

和具有此目标的 JSP:

<s:url var="layoutContentUrl" action="Menu-view" namespace="/public" />
    <sx:div showLoadingText="false" indicator="ajaxIndicator"
        id="layout-content" href="%{layoutContentUrl}" theme="ajax"
        listenTopics="layoutContentTarget" preload="false"
                    afterNotifyTopics="/ajaxAfter">
</sx:div>

注意:如果您通过一个参数连接所有内容,这是一种特殊情况,但可以使用发布其他目标的其他参数轻松扩展它。我将尝试使其足够通用以在某处发布它,但这(我猜)还有很长的路要走:)

如果您有任何问题,请发布。

于 2009-12-15T12:48:20.880 回答
0

不了解 Struts,但您看过 dojo.undo (0.4.3) 吗?

于 2009-12-07T16:09:22.420 回答