Struts2中的动态JSP标记文件

时间:2012-10-28 20:00:49

标签: jsp struts2

我正在使用JSP标记文件将“小部件”添加到我的应用程序页面。

是否可以将操作与JSP标记文件相关联,因此它将像普通页面一样填充动态内容,然后在某个地方“注入”?

假设我有这样调用的标记文件:<c:onlinemembers />。它显示在线成员列表。 我需要有一种方法在显示动态数据之前填充它。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我会使用Ajax并将数据作为JSON返回并使用jQuery显示或创建自定义JSP标记来执行此操作。我不是很喜欢后者。从您的操作中返回JSON并使用jQuery是一种简单的方法。希望这可以帮助。

答案 1 :(得分:0)

编辑:正如Alexandr所指出的,这对Struts 2.0.x来说很好,Struts集成了Dojo标签。从Struts 2.1.x开始,应该使用jQuery(使用类似的机制)。

一切都取决于您的Struts2版本。


或者您可以使用内置的Struts2 / Dojo AJAX功能。

<head>
    <script type="text/javascript">
      // Scripts executed when loading the JSP:
      function configPage(){
        // Dojo topic function subscribing:
        dojo.event.topic.getTopic("myTopic").subscribe(null, "updateDivFunction");
      }

      function updateDivFunction(param1, param2){
        // Get the Div
        var myDiv = dojo.widget.byId("myDynamicDiv"); 

        // Set a new href to the AJAX action to call, 
        // which will return a partial JSP (a snippet),
        // as mapped normally in struts.xml...      
        myDiv.href = "loadDynamicDivAjax.do?param1="+param1+"&param2="+param2;

        // Force the div to reload with AJAX now.
        resultDiv.refresh();
      }

      function updateDivWrapper(param1, param2){
        // Publishing the topic now, will invoke the 
        // "updateDivFunction" set in configPage()...
        dojo.event.topic.publish("myTopic", param1, param2);
      }


    </script>
</head>

<body onload="javascript:configPage();">
    <h1>My main JSP page</h1>

    <br/><br/>

    <s:div id="myDynamicDiv" theme="ajax" 
           loadingText="Loading..." 
           errorText="Errors occurred...">
    </s:div>

    <br/>

    <span>
        Click below links to load the DIV with DOJO 
        through an AJAX call, and different parameters.
    </span>

    <br/><br/>

    <a href="javascript:updateDivWrapper('1','2')">
        Update with param1=1 and param2=2
    </a>

    <br/><br/>

    <a href="javascript:updateDivWrapper('8','9')">
        Update with param1=8 and param2=9
    </a>
</body>
在Struts.xml中,您应该将loadDynamicDivAjaxAction映射到返回正常的JSP片段作为结果。就是这样......

相关问题