Question

I'd like to get the string output of running a JSPX template, so that I can javascript-string-escape it and use it in a JS snippet.

https://stackoverflow.com/a/13721859/684934 shows a way to do it with plain servlets, but I'm concerned that this method may not play nicely with SpringMVC (my controller methods don't actually receive the request and response objects, and my understanding is that jspx uses its own templating engine).

Are there any built-in ways to do this?

The motivation is that I'd like to do what is common practice in Ruby on Rails: re-use the view partials to draw results of ajax calls.

Was it helpful?

Solution

It's more verbose than what Rails does and requires an extra file, but you can accomplish sort of the same thing using tags.

partial.tagx

<jsp:root params...>
   <jsp:directive.attribute name="arg1" params.../>
   <jsp:directive.attribute name="arg2" params.../>
   <jsp:directive.attribute name="arg3" params.../>
   <!--- etc --->

   <!--- partial content --->
</jsp:root>

Then you can reuse the partial content. For just the partial:

partial.jspx

<jsp:root params...>
   <myprefix:partial arg1="..." arg2="..." arg3="..." otherparams="..."/>
</jsp:root>

Which you then serve with regular controller function. You can use the within your view:

view.jspx

<!--- Other view content --->
<div class="somewhere">
   <myprefix:partial arg1="..." arg2="..." arg3="..." otherparams="..."/>
</div>
<!--- Other view content --->

It's not pretty, but at least you get code reuse from this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top