Page 1 of 1

How is urls mapped to templates

Posted: Mon Oct 29, 2012 1:04 pm
by om.singh
in the demo application we have navigation urls defined in the load_content_data.sql.

For example - /hot-sauces

this URL does not have any entry for display_template hence it will land up on

Code: Select all

<property name="fullPageLayout" value="layout/fullPageLayout" />
in applicationContext-Servlet.xml.

But how is the templateName value

Code: Select all

<div th:substituteby="${templateName}" />
derived from this URL in the fullPageLayout.html

Regards
Om

Re: How is urls mapped to templates

Posted: Tue Oct 30, 2012 9:07 am
by om.singh
How does the url - maps to a spring web controller and then the view.

It seems all the page requests are using CategoryController - which in turns load the default view of catalog/category.html

Any suggestion or guidence of some one has figured out the way thymeleaf templates are being mapped to request will be a great help.

I want to map my own controller in some cases and use another thymeleaf page template.

Regards
Om

Re: How is urls mapped to templates

Posted: Mon Nov 05, 2012 10:24 am
by aazzolini
Category pages will indeed be handled by the CategoryController. This is done using URL matching vs the category URL in the database.

If you want to add your own page, you simply create your controller and annotate how you normally would. Let's say you'd like to add a controller that will serve "/testfunctionality". In your controller method, you would for example return "mystore/test". This path corresponds to "/WEB-INF/templates/mystore/test.html" This is because we have "/WEB-INF/templates/" configured as a prefix and ".html" as a suffix. By default, this is done in a Broadleaf application context file and looks like this:

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="cacheable" value="false"/>
      <property name="characterEncoding" value="UTF-8" />
   </bean>   


If you declare that bean yourself, you can override these settings.

"mystore/test" should be considered a template fragment, which will by default be included in the standard layout, which as you've identified is at "layout/fullPageLayout". If you'd like to use a different template for this fragment, you can add that definition to the layoutMap object. Say you added

Code: Select all

<entry key="testfunctionality" value="NONE" />


This would then not utilize a layout at all for your page. You could, of course, point it to a different layout as well.

Layouts are served via longest-prefix matching.