Home > Back-end >  Can I preserve content negotiation easy in springboot while doing layouting in thymeleaf with view-n
Can I preserve content negotiation easy in springboot while doing layouting in thymeleaf with view-n

Time:10-14

I develope a website made in Springboot and I've set up some list exportation through content negotiation and AbstractView implementations. I'll explain listing the elements I'm using:

  • My first aproach for it was to use the view-named bean definition.
@Component("intranet/ciudadanos/export")
public class SomeEntityCsvView extends AbstractView {
//...
}
spring.mvc.contentnegotiation.favor-parameter=true
spring.mvc.contentnegotiation.media-types.pdf=application/pdf
spring.mvc.contentnegotiation.media-types.csv=application/csv
spring.mvc.contentnegotiation.media-types.xlsx=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • Controller works as this:
    The same url is used to visit the list webpage then a link with query parameter ?format= puts the content negotiation to work.

  • Later arose the need for make some layouting:
    I followed the "other options" on the thymeleaf documentation on this matter link which consists on handle the name of the view and substitute it with the reference to the file that works as layout:

if my controller returns "list/entity", a HandlerInterceptor class will change it for "list-layout" and the original view name will work as reference for concrete fragments.

  • Now it happens that I can't use the view name as before to put the export link on the same url and viewname.

Workarounds and alternative aproaches are welcome.

Thank you in advance.

CodePudding user response:

Came out with the workaround.

  • Controller: I declare a /somePath/export request mapping method. The method just returns the name of a view which includes the word "export".
  • View name handling: If the view name contains "export" I don't make any substitution so it's kept as it's returned by the controller.
  • Abstract views: are used as they are intended with @Component("name of view wich contains export")
  • Content negotiation: as it is. Doesn't need anyhthing special.
  • Related