开发者

Load a Struts 2 action using jquery in javascript

开发者 https://www.devze.com 2022-12-30 23:10 出处:网络
I\'m trying to reload a target div from a javascript by using jquery to target the div and a struts action that loads the content.

I'm trying to reload a target div from a javascript by using jquery to target the div and a struts action that loads the content.

开发者_JS百科Does anyone know how to do this? The problem is how I use (javascript) jquery to do this.

BR, Tobias


The simplest thing to do is use the jQuery .load() function.

$('#targetDivId').load('${your.struts.url}', function() {
  // stuff to do when the div has been reloaded
});

Now understand that you should make sure that the response from your action is a page that's not really a complete HTML page, because you can't stuff a complete HTML document inside a <div>. If you have a complete document, and you only want a portion of it (say, a block contained within a <div> with id "usefullStuff"), you can do this:

$('#targetDivId').load('${your.struts.url} #usefullStuff', function() {
  // code
});


Or with Struts2 jQuery Plugin:

<%@ taglib prefix="s" uri="/struts-tags"%> 
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%> 
<html> 
  <head> 
    <sj:head/> 
  </head> 
  <body> 
    <div id="div1">Div 1</div> 
    <s:url id="ajaxTest" value="/AjaxTest.action"/> 

    <sj:a id="link1" href="%{ajaxTest}" targets="div1"> 
      Update Content 
    </sj:a> 
  </body> 
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消