including jspf in an ajax response

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

including jspf in an ajax response

Chris Cheshire
I have a few large forms on my site that have pieces submitted individually by ajax. The ajax handler returns JSON via a streaming resolution and then the javascript acts upon the result updating the page.

Since a piece of a form might need rendering either initially when the page is displayed, or after an add action has succeeded, I have the html for a piece contained in a template file. The JSP has a custom tag handler that calls a helper class that loads the file and does string replacement and then writes the replaced template in the tag's output stream.

public class MyTagHandler extends BodyTagSupport() {
    public int doEndTag() {
        this.pageContext.getOut().print(new MyFormatter().format(myObject));

public class MyFormatter() {
    public String format(MyObject myObject) {
        String template = readFile("path to template");
        return template.replace("..", "..").replace("...", "...");

The ajax handler for the add does its work, then calls the same helper class (MyFormatter) and includes the resulting html in a variable in the json response. The HTML is not the only thing in the JSON response however, there are usually other control variables like object ids for the javascript to use.


{ html: "some fragment of html representing the item added", item: { id: "123", someField: "someValue" } }

The javascript handler takes the html and appends it to the element on the page that contains the previously rendered items and potentially does other things with the other data returned in the response.

What I would like to be able to do is move the templates into jspf fragments instead so that I can then include these in the JSP without the need of a custom tag library. This will allow me a little more control of the templates and the logic in them. That part is easy.

However, I would also like to be able to use the jspf in the ajax handler too. I can't create a new HttpClient and call the fragment because it lives under the WEB-INF directory and is not publically visible.

In my streaming resolution, I can't use RequestDispatcher.include() because it just adds the output from the JSPF directly to the response stream, outside of my json object. Is there a way to internally call the jspf and have direct access to the resulting output without it being inserted immediately into the response stream?



Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead.
Download for free and get started troubleshooting in minutes.
Stripes-users mailing list
[hidden email]