我正在使用 JSP 标记文件将“小部件”添加到我的应用程序页面。
是否有可能有一个与 JSP 标记文件关联的操作,因此它将像普通页面一样填充动态内容,然后在某个地方“注入”?
假设我有这样调用的标记文件:<c:onlinemembers />
. 它显示在线成员列表。我需要有一种在显示之前用动态数据填充它的方法。
有什么建议么?
I would either use Ajax and return the data as JSON and display using jQuery or create a custom JSP tag to do it. I'm not really fond of the latter. From your action just return JSON and using jQuery is a simple approach. Hope this helps.
编辑:正如 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+"¶m2="+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 片段作为结果。而已...