java - spring boot mvc how to support multiple viewresoler (eg. jsp and freemarker) -
i hope web application support jsp , freemarker both templates.i have read article @ below. https://spring.io/blog/2013/06/03/content-negotiation-using-views code below. result jsp template ok. wrong configration?
mvcconfiguration.java
@configuration @enablewebmvc public class mvcconfiguration extends webmvcconfigureradapter { @bean public internalresourceviewresolver getjspviewresolver() { internalresourceviewresolver resolver = new internalresourceviewresolver(); resolver.setprefix("/web-inf/jsp/"); resolver.setsuffix(".jsp"); resolver.setorder(0); return resolver; } @bean public freemarkerviewresolver getfmviewresolver() { freemarkerviewresolver resolver = new freemarkerviewresolver(); resolver.setcache(true); resolver.setprefix(""); resolver.setsuffix(".ftl"); resolver.setcontenttype("text/html; charset=utf-8"); resolver.setorder(1); return resolver; } @bean public freemarkerconfigurer freemarkerconfig() throws ioexception, templateexception { freemarkerconfigurationfactory factory = new freemarkerconfigurationfactory(); factory.settemplateloaderpath("classpath:templates"); factory.setdefaultencoding("utf-8"); freemarkerconfigurer result = new freemarkerconfigurer(); result.setconfiguration(factory.createconfiguration()); return result; } @bean public viewresolver contentnegotiatingviewresolver( contentnegotiationmanager manager) { list<viewresolver> resolvers = new arraylist<viewresolver>(); resolvers.add(getjspviewresolver()); resolvers.add(getfmviewresolver()); contentnegotiatingviewresolver resolver = new contentnegotiatingviewresolver(); resolver.setviewresolvers(resolvers); resolver.setcontentnegotiationmanager(manager); return resolver; } }
testcontroller.java
@controller public class testcontroller { @requestmapping("/hello") public string jspview() { return "hello"; } @requestmapping("/fm") public string freemarkerview() { return "fm"; } }
hello.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <% out.println("hello jsp"); %> </body> </html>
fm.ftl
<!doctype html> <html lang="en"> <body> hello freemarker </body> </html>
the viewresolver
s consulted in chain, until 1 of them returns view
render (javadoc).
however internalresourceviewresolver
little different other viewresolver
implementations, constructs url (using prefix
, suffix
) , returns view
in turn forward request url. hence *always* return a
view` instance.
now when have multiple viewresolver
s make sure internalresourceviewresolver
last in chain (as mentioned in javadocs of said class). if don't , first whole chain won't consulted.
to fix problem switch order of viewresolver
s , way have both freemarker , jsp views working.
Comments
Post a Comment