Cómo usar controladores Spring anotados con Freemarker para devolver respuestas ajax personalizadas

I primarily use annotated controllers that return ModelAndView object used by Spring and Freemarker to render and return pages to browsers - works great. I'd now like to respond to ajax gets and posts that return html fragments as payload inside an XML message.

The problem I'm having not returning a ModelAndView objects seems to confuse Spring WRT to @ModelAttribute (s) and session objects that support data to / and from forms.

I've had to hack the freemarker template to support i18n messages, etc. I'm finding that simply rendering an FTL file while also trying to utilize Spring's object rendering from froms is becoming quite a rabbit hole.

I'm trying to manually (guess what and) do the things that get done behind the scenes and it's not working optimally. To edit an object I'm manually placing the object in the session on rendering the edit form. When the post comes back there are residual/different values in the object I get back out of the session AND the ModelAttribute that Spring rendered for me.

Bottom line? Questions about Spring, Freemarker and custom non-ModelAndView responses that are rendered by Freemarker.

preguntado el 01 de febrero de 12 a las 14:02

I feel like I need to clarify. The main problem is trying to render an FTL file by directly accessing the the Template. When doing that the i18n object is missing as well as any BindingResult provided by Spring. I got it to work by putting them in there myself but that is where the slippery slope starts and I'm sliding :-( -

The solution: I added another instance of FreeMarkerViewResolver that only that has viewNames ='*.frg'. The viewClass is my own class that renders the template the way I want by overriding the 'processTemplate' method. Now all things are in harmony. -

It is absolutely acceptable and even preferred to post an answer to your own question. -

1 Respuestas

Add another instance of FreeMarkerViewResolver that only that has viewNames ='*.frg'. El viewClass should be your own class that renders the template the way you want by overriding the processTemplate método.

Respondido el 10 de Septiembre de 12 a las 08:09

(Note: just bringing the OP's answer down from the comments to an actual answer.) - Mu Mind

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.