Andna
Andna

Reputation: 6689

Spring and Tiles - manually rendering JSP files

We are using Spring 3 and Apache Tiles to create web application that runs on Tomcat 7.

Is it possible, using those tools to manually generate HTML from JSP files in java code during runtime?

For example, I have sample.jsp with some dynamically generated content based on a contents of a passed model. I would like to store rendered HTML from JSP in String object.

On a very high level of abstraction:

String renderedHtml=renderHtmlFromJSP(jspName,model);

If not, is it possible to dynamically change definition of Tiles elements? For example

<put-attribute name="headerRight" value="dynamically_set_value" />

?

Upvotes: 1

Views: 1714

Answers (1)

Dewfy
Dewfy

Reputation: 23644

This question can be coupled with this one: Execute and render JSP inside a Filter So your renderHtmlFromJSP can look like:

String renderHtmlFromJSP(
      String fileName, 
      ???? dataModel, 
      HttpServletRequest sourceRequest){
   //you need emulate response to produce output in string (see bellow)
   MyStringResponse resp = new MyStringResponse();
   sourceRequest
        .getRequestDispatcher("/WEB-INF/header.jsp")
        .include(request, resp);
   resp.flushBuffer();
   return resp.getMyInternalBufferContent(); 
}

To implement MyStringResponse use HttpServletResponseWrapper override method getOutputStream() and return there instance of ByteArrayOutputStream

Upvotes: 1

Related Questions