I'm doing this exact thing using jQuery.
In my main page where the tiles are defined, I have:
<script type="text/javascript" src="/path/to/jquery/jquery-1.4.2.min.js" />
Then, in the places where I want to call a page fragment I have:
<script type="text/javascript"><![CDATA[
$(document).ready(function() {
$("#addNewFragment").click(function() {
$.get("/app/fragments/target.page",function(data){$("#addFragmentLocation").before(data);});
});
});
]]></script>
And lower in the same page where I want the fragment to appear, I have:
<span id="addFragmentLocation" />
And I have some element with an ID of "addNewFragment" so that when I click on it, the jQuery function executes.
I have a controller called FragmentController.java. It takes the form of:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping("/fragments/**")
@Controller
public class FragmentController {
// Add request mappings as you need to.
@RequestMapping(value = "/fragments/target.page", method = RequestMethod.GET)
public String getFragment(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
//Add model elements as you need to.
return "fragmentView";
}
}
Finally, I have a view in the views.xml file declared which maps the "fragmentView" view back to an actual .jspx page.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="fragmentView" template="/WEB-INF/path/to/fragments/myNewPageFragment.jspx" />
</tiles-definitions>
As an aside, jspx pages are naturally XML based. jQuery can't insert XML into an HTML based DOM. Make sure you start them with:
<div xmlns:jsp="http://java.sun.com/JSP/Page" >
<jsp:output omit-xml-declaration="yes"/>
<jsp:directive.page contentType="text/html; charset=ISO-8859-1" />
Otherwise, you get a mysterious JavaScript error:
Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: "http://127.0.0.1:8080/path/to/jquery/jquery-1.4.2.min.js Line: 113"]
Hope this helps!