Page 1 of 2
how views are resolved by BroadleafThymeleafViewResolver
Posted: Sun May 25, 2014 4:48 am
by prabhat.kataria
I am a bit puzzled by the way BroadleafThymeleafViewResolver resolves the view returned by request mappings. As in applicationContext-servlet.xml i can see the BroadleafThymeleafViewResolver bean has map entry for account, catalog, checkout, layout, content but i could not find the same for authentication, cart, contactus. Secondly, what does the key-value pair mean?
Also, if i want to add a new view inside a new folder then will I have to map it inside BroadleafThymeleafViewResolver bean.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Wed May 28, 2014 1:58 pm
by phillipuniverse
That extra code within the BroadleafThymeleafViewResolver is used as a rudimentary layout system, as one did not exist when we first wrote it. However, we now recommend that people use the official Thymeleaf Layout Dialect
https://github.com/ultraq/thymeleaf-layout-dialect as all the functionality supercedes the rudimentary system that we wrote.
The beans you should really be looking at are the implementations of Thymeleaf's ITemplateResolver, which actually resolves the template file itself. The ViewResolvers are a Spring concept. Here is one of the template resolver beans hooked up to the view resolver:
Code: Select all
<bean id="blWebTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="cacheable" value="${cache.page.templates}"/>
<property name="cacheTTLMs" value="${cache.page.templates.ttl}" />
<property name="order" value="200"/>
</bean>
This means that it will look in the /WEB-INF/templates folder of your webapp, and resolve things that end in .html. So if you create a file called:
site/src/main/webapp/WEB-INF/templates/view/blah/test.html
Then from your Spring controller you can just return "view/blah/test" and the template resolver will take care of the rest.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Wed May 28, 2014 2:38 pm
by prabhat.kataria
Hi philip, thanks for the information. But my doubt was for the following code
Code: Select all
<bean class="org.broadleafcommerce.common.web.BroadleafThymeleafViewResolver">
<property name="templateEngine" ref="blWebTemplateEngine" />
<property name="order" value="1" />
<property name="cache" value="${thymeleaf.view.resolver.cache}" />
<property name="fullPageLayout" value="layout/fullPageLayout" />
<property name="characterEncoding" value="UTF-8" />
<property name="layoutMap">
<map>
<entry key="account/" value="layout/accountLayout" />
<entry key="catalog/" value="NONE" />
<entry key="checkout/" value="layout/checkoutLayout" />
<entry key="checkout/confirmation" value="layout/fullPageNoNavLayout" />
<entry key="layout/" value="NONE" />
<entry key="content/NONE" value="NONE" />
</map>
</property>
</bean>
As i said in initial question i could not see the mapping for authentication, cart, contactus in this bean but they still get resolved, say if i return a String from controller like "authentication/login" it gets rendered.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Wed May 28, 2014 3:11 pm
by phillipuniverse
Right, those mappings are for the layoutMap property which is the rudimentary layout system that we developed. All that says is that if any view is returned that starts with account/, then wrap that view within the layout/accountLayout layout.
This ViewResolver is not actually resolving the template itself, it delegates to the TemplateResolver which I mentioned in my post. So based on the definition that I posted of the blWebTemplateResolver, if you create a file in the site/src/main/webapp/WEB-INF/templates/foo.html, then you can simply return "foo" from your controller:
Code: Select all
@RequestMapping("/showfoo")
public String showFoo(HttpServletRequest request, HttpServletResponse) {
return "foo";
}
and it will show the foo.html template with no other configuration necessary.
If you want to wrap that foo.html template inside of a bar layout, then that's where you would use the layoutMap property to configure that. Again though, we no longer recommend using the layoutMap and instead you should use a more full-featured layout system like the Thymeleaf Layout Dialect.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Thu May 29, 2014 1:28 am
by prabhat.kataria
Initial impression of Thymeleaf Layout Dialect is amazing and I am looking forward to use it. I have a small question regarding it, if i want to use Thymeleaf Layout Dialect with BLC Demo Site code then I think i will have to do a lot of code rework on view pages as we will be removing the layout mappings from bean definition. Is my understanding correct?
Also I assume that layout mapping for authentication, cart, contactus is inside one of the BLC jars files.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Thu May 29, 2014 10:09 am
by phillipuniverse
Yes that is correct. It is definitely on our backlog to refactor the demo site to utilize those layouts but there have been other priorities. that have prevented that.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Wed Aug 27, 2014 2:04 am
by bibroy
One question:
Yes I want to use Thymeleaf Layout Dialect but still I have a question regarding the old code base: what does the following mean -
"The layout to apply is determined by a longest prefix match of the requested file." (this excerpt is from
http://www.broadleafcommerce.com/docs/c ... c-tutorial)
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Wed Aug 27, 2014 8:43 am
by phillipuniverse
So if you return this String path to a template from your Spring controller: "/path/to/my/template" and then if you set up 2 layouts that are set to "/path/to" and "/path/to/my" then when trying to determine the layout for the page the system will use the layout for "/path/to/my" (longest prefix).
Again though, this is completely moot with the Thymeleaf layout dialect. You don't need to use the longest prefix match from Broadleaf at all.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Wed Aug 27, 2014 8:56 am
by bibroy
Thanks...that clarifies.
Re: how views are resolved by BroadleafThymeleafViewResolver
Posted: Sat Aug 30, 2014 11:18 pm
by prabhat.kataria
Phillip, Thymeleaf Layout Dialect is the one mentioned at
https://github.com/ultraq/thymeleaf-layout-dialect right?