Apache Struts 1: How do I return a processed JSP page as text/html for an AJAX retquest?

23 Views Asked by At

I need to create a search form in JSP for an Apache Struts 1 system. I want to submit the form via AJAX, process the requrest and return a table created using a JSP template.

I know how to create the AJAX request and return some text but I don't know how to have the Action return a processed JSP page without also forwarding or redirecting the user/browser.

Essentially, I want to return some HTML generated via JSP that will then be inserted into the page at a place of my choosing.

Partial example:

public class HelloWorldAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        /* I'll get the search term(s) from the request here. */

        TabularDataForm searchResult = DataFactory.getSearchResult(searchTerms);
        request.getSession( false ).setAttribute( "searchResult", searchResul);
        
        /* Code to load and process a JSP page here */
        String processedJSP = SomeHow.getJsp();

        /* Return text/html response to the original AJAX request, but don't forward or redirect. */
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.print(processedJSP);

        return null;
    }

}

I could manually biuld the HTML in Java, of course, but there are going to be a bunch of links and formatting that need to be generated dynamically based on the data. So I want to use JSP to make that a lot less painful.

Thanks for any insight you can provide.

0

There are 0 best solutions below