{"id":898,"date":"2011-05-09T10:38:35","date_gmt":"2011-05-09T08:38:35","guid":{"rendered":"http:\/\/touk.pl\/blog\/?p=898"},"modified":"2023-03-23T10:51:10","modified_gmt":"2023-03-23T09:51:10","slug":"virgo-snaps-with-apache-tiles-integration","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2011\/05\/09\/virgo-snaps-with-apache-tiles-integration\/","title":{"rendered":"Virgo Snaps with Apache Tiles integration"},"content":{"rendered":"<p>After smoke tests become time to try using <strong>Virgo Snaps<\/strong> in more practical way. In modular application it will be useful to run it with templating framework. According to <strong>animal-menu-bar sample<\/strong> it will be better if developer of snap will not know anything about layout of host application and snippets like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\n&lt;jsp:include page=&quot;\/..\/top.jsp&quot;\/&gt;\r\n(...)\r\n&lt;jsp:include page=&quot;\/..\/bottom.jsp&quot;\/&gt;\r\n<\/pre>\n<p>will be not necessary to put in snap pages. So lets modify a bit hosts jsps. Template page could look similar to old <strong>index.jsp<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\n&lt;%@ taglib prefix=&quot;tiles&quot; uri=&quot;http:\/\/tiles.apache.org\/tags-tiles&quot; %&gt;\r\n&lt;jsp:include page=&quot;top.jsp&quot;\/&gt;\r\n\r\n&lt;tiles:insertAttribute name=&quot;body&quot; \/&gt;\r\n\r\n&lt;jsp:include page=&quot;bottom.jsp&quot;\/&gt;\r\n<\/pre>\n<p>Now <strong>index.jsp<\/strong> should not have includes:<\/p>\n<pre class=\"EnlighterJSRAW\"> &lt;p&gt;\r\n The Snap Menu Bar sample is intended to showcase the ability to dynamically change the content of a menu bar using snaps. Each\r\n of the snaps that might be displayed in the menu bar includes a top and bottom JSP page and inherits it's styling from the host\r\n bundle.  Therefore, the snap bundle is only responsible for showing a small subset of content.\r\n &lt;\/p&gt;\r\n<\/pre>\n<p>Template definition should have one template without any attributes (it will be added dynamicly):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">&lt;tiles-definitions&gt;\r\n    &lt;definition name=&quot;defaultTemplate&quot; template=&quot;\/template.jsp&quot;&gt;\r\n    &lt;\/definition&gt;\r\n&lt;\/tiles-definitions&gt;<\/pre>\n<p>To dynamic adding this definitions we must implement our <strong>TilesView<\/strong>. I&#8217;ve used one which is a part of <strong>parancoe<\/strong> &#8211; Open Source Java Web Framework available on Google Code: http:\/\/code.google.com\/p\/parancoe\/source\/browse\/plugins\/parancoe-plugin-tiles\/src\/main\/java\/org\/parancoe\/plugin\/tiles\/CheapTilesView.java?r=f42be9c3c8e2df436d4970cfdaea1aff73d9cfdb and modify it a bit for our purposes. Most interesting part is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\n    protected void renderMergedOutputModel(Map model, HttpServletRequest request,\r\n            HttpServletResponse response)\r\n            throws Exception {\r\n\r\n        try {\r\n            super.renderMergedOutputModel(model, request, response);\r\n        } catch (TilesException te) {\r\n            lazyRegisterThanRender(request, response);\r\n        } catch (Exception ex) {\r\n            ex.printStackTrace();\r\n        }\r\n    }\r\n\r\n    private void lazyRegisterThanRender(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException {\r\n        ServletContext servletContext = getServletContext();\r\n        MutableTilesContainer container = (MutableTilesContainer)\r\n                ServletUtil.getContainer(servletContext);;\r\n        Definition definition = new Definition();\r\n\r\n        String[] arr = parsePath(getUrl());\r\n        String subContextPart = arr[0];\r\n        String mainUrlPart    = arr[1];\r\n\r\n        definition.setName(getUrl());\r\n        definition.setExtends((String) getAttribute(KEY_DEFAULT_TEMPLATE,\r\n                DEFAULT_DEFAULT_TEMPLATE));\r\n        String attributeList = (String) getAttribute(KEY_DEFAULT_ATTRIBUTES,\r\n                DEFAULT_DEFAULT_ATTRIBUTES);\r\n\r\n        String[] attributes = attributeList.split(&quot;,&quot;);\r\n        if (attributes.length == 1) {\r\n            addAttributeWithPathValueToDefinition(attributes[0], subContextPart, mainUrlPart, definition);\r\n        } else {\r\n            for (String attribute : attributes) {\r\n                addAttributeWithPathValueToDefinition(attributes[0], subContextPart, mainUrlPart + &quot;_&quot; + attribute, definition);\r\n            }\r\n        }\r\n\r\n        container.register(definition, request, response);\r\n        container.render(getUrl(), new Object[]{request, response});\r\n    }\r\n<\/pre>\n<p>How can we see here, <em>lazyRegisterThanRender<\/em> will be invoked if Tiles will have problems in our case in resolving view name. This method registering new template which extends default one. It also adds attributes taking its values from view name. <em>parsePath<\/em> parsing path in form: <em>view@snap<\/em> or only: view:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">private String[] parsePath(String path) {\r\n        String[] arr = new String[] {&quot;&quot;, &quot;&quot;};\r\n        int indexOfAt = path.indexOf('@');\r\n        if (indexOfAt &gt; 0) {\r\n            arr[0] = '\/' + path.substring(indexOfAt+1, path.length());\r\n            arr[1] = path.substring(0, indexOfAt);\r\n        } else {\r\n            arr[1] = path;\r\n        }\r\n        return arr;\r\n    }<\/pre>\n<p>In our case will be only body attribute.<\/p>\n<p>To use Tiles we should define <em>tilesConfigurer<\/em> in spring context. But before this we must declare <em>DispatcherServlet<\/em> in <strong>web.xml<\/strong> which will past requests to our controllers in <strong>web.xml<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">&lt;servlet&gt;\r\n    &lt;servlet-name&gt;snap&lt;\/servlet-name&gt;\r\n    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;\r\n    &lt;load-on-startup&gt;2&lt;\/load-on-startup&gt;\r\n&lt;\/servlet&gt;\r\n\r\n&lt;servlet-mapping&gt;\r\n    &lt;servlet-name&gt;snap&lt;\/servlet-name&gt;\r\n    &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;\r\n&lt;\/servlet-mapping&gt;\r\n<\/pre>\n<p>After this simply adding WEB-INF\/snap-servlet.xml context of application will be read:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n    xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n    xmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot;\r\n    xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n    xsi:schemaLocation=&quot;\r\n      http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n      http:\/\/www.springframework.org\/schema\/context http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\r\n          http:\/\/www.springframework.org\/schema\/osgi http:\/\/www.springframework.org\/schema\/osgi\/spring-osgi.xsd&quot;\r\n        default-autowire=&quot;byName&quot;&gt;\r\n\r\n    &lt;bean id=&quot;tilesConfigurer&quot; class=&quot;org.springframework.web.servlet.view.tiles2.TilesConfigurer&quot;&gt;\r\n        &lt;property name=&quot;definitions&quot;&gt;\r\n            &lt;list&gt;\r\n                &lt;value&gt;\/WEB-INF\/tiles.xml&lt;\/value&gt;\r\n            &lt;\/list&gt;\r\n        &lt;\/property&gt;\r\n        &lt;property name=&quot;useMutableTilesContainer&quot; value=&quot;true&quot;\/&gt;\r\n    &lt;\/bean&gt;\r\n\r\n    &lt;bean id=&quot;tilesViewResolver&quot; class=&quot;org.springframework.web.servlet.view.tiles2.TilesViewResolver&quot;&gt;\r\n        &lt;property name=&quot;attributesMap&quot;&gt;\r\n            &lt;map&gt;\r\n                &lt;entry key=&quot;CheapTilesView.DEFAULT_TEMPLATE&quot; value=&quot;defaultTemplate&quot;\/&gt;\r\n                &lt;entry key=&quot;CheapTilesView.DEFAULT_ATTRIBUTES&quot; value=&quot;body&quot;\/&gt;\r\n            &lt;\/map&gt;\r\n        &lt;\/property&gt;\r\n        &lt;property name=&quot;viewClass&quot; value=&quot;CheapTilesView&quot;\/&gt;\r\n    &lt;\/bean&gt;\r\n\r\n    &lt;bean class=&quot;MainController&quot;\/&gt;\r\n\r\n&lt;\/beans&gt;<\/pre>\n<p>After this, we must to add templates definitions: <strong>lib\/tiles-jsp.tld<\/strong> and few entries in <strong>template.mf<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\">Manifest-Version: 1\r\nBundle-SymbolicName: animal.menu.bar\r\nBundle-Version: 1.0\r\nBundle-ManifestVersion: 2\r\nBundle-Name: Multiple Styles Host\r\nWeb-ContextPath: \/animal-menu-bar\r\nImport-Library:\r\n org.springframework.spring;version=&quot;[3.0,3.1)&quot;\r\nImport-Bundle:\r\n com.springsource.org.apache.taglibs.standard;version=&quot;[1.1.2,1.3)&quot;,\r\n com.springsource.javax.servlet.jsp.jstl;version=&quot;[1.1.2, 1.1.3)&quot;,\r\n org.eclipse.virgo.snaps.api;version=&quot;[1.0,2.0)&quot;,\r\n com.springsource.org.apache.tiles;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.core;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.servlet;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.jsp;version=&quot;2.1.3&quot;\r\nImport-Package:\r\n org.eclipse.virgo.snaps.core;version=&quot;[1.0,2.0)&quot;,\r\n javax.servlet;version=&quot;2.5&quot;,\r\n javax.servlet.http;version=&quot;2.5&quot;,\r\n javax.servlet.jsp;version=&quot;2.1&quot;,\r\n org.tuckey.web.filters.urlrewrite;version=&quot;[3.1.0,3.1.0]&quot;,\r\n org.springframework.js.resource;version=&quot;[2.0,2.1)&quot;,\r\n org.springframework.stereotype;version=&quot;[3.0,3.1)&quot;,\r\n org.springframework.web.bind.annotation;version=&quot;[3.0,3.1)&quot;,\r\n org.springframework.web.servlet;version=&quot;[3.0,3.1)&quot;,\r\n org.springframework.web.servlet.view.tiles2;version=&quot;[3.0,3.1)&quot;,\r\n org.apache.tiles;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.context;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.impl;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.jsp.context;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.mgmt;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.renderer.impl;version=&quot;2.1.3&quot;,\r\n org.apache.tiles.servlet.context;version=&quot;2.1.3&quot;\r\n<\/pre>\n<p>Our MainController will look like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Controller\r\npublic class MainController {\r\n    @RequestMapping(&quot;\/&quot;)\r\n    public String rootHandler() {\r\n        return &quot;index&quot;;\r\n    }\r\n    @RequestMapping(&quot;\/index.htm&quot;)\r\n    public String snapHandler(@RequestParam(&quot;snap&quot;) String snap) {\r\n        return &quot;index@&quot; + snap;\r\n    }\r\n}<\/pre>\n<p>So we must change top.jsp in host application:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">                &lt;snaps:snaps var=&quot;snaps&quot;&gt;\r\n                    &lt;c:forEach var=&quot;snap&quot; items=&quot;${snaps}&quot;&gt;\r\n                        &lt;li&gt;&lt;a href=&quot;&lt;c:url value=&quot;index.htm?snap=${snap.properties['link.path']}&quot;\/&gt;&quot;&gt;\r\n                            ${snap.properties['link.text']}&lt;\/a&gt;\r\n                        &lt;\/li&gt;\r\n                    &lt;\/c:forEach&gt;\r\n                &lt;\/snaps:snaps&gt;<\/pre>\n<p>And also in <strong>snap.properties<\/strong> of both snaps link.path so it should be equals to snap subcontext (e.g. <em>cat<\/em>, <em>dog<\/em>).<\/p>\n<p>After all of this we will have some troubles with resources handling so will be necessary to add <strong>urlrewrite filters<\/strong> like it was writed on <a href=\"http:\/\/blog.springsource.com\/2009\/06\/22\/modular-web-applications-with-springsource-slices\/\">Rob&#8217;s blog about <strong>Spring Slices<\/strong><\/a> &#8211; precursor of <strong>Snaps<\/strong>: <\/p>\n<p>Our <strong>repository\/usr<\/strong> should have:<\/p>\n<pre class=\"EnlighterJSRAW\">commons-beanutils-1.8.0.jar\r\ncommons-digester-1.8.1.jar\r\ncom.springsource.org.apache.commons.collections-3.2.1.jar\r\ncom.springsource.org.apache.tiles-2.1.3.jar\r\ncom.springsource.org.tuckey.web.filters.urlrewrite-3.1.0.jar\r\norg.eclipse.virgo.snaps.api.jar\r\norg.eclipse.virgo.snaps.core.jar\r\ntiles-core-2.1.3.jar\r\ntiles-jsp-2.1.3.jar\r\ntiles-servlet-2.1.3.jar<\/pre>\n<p>which we can copy from maven repository.<\/p>\n<p>Patch with these changes is available on <a href=\"https:\/\/github.com\/arkadius\/virgo-snaps-poc\/blob\/master\/tiles_integration.patch\">github<a><\/p>\n","protected":false},"excerpt":{"rendered":"After smoke tests become time to try using Virgo Snaps in more practical way. In modular application it&hellip;\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[44],"class_list":{"0":"post-898","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-osgi"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":28,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"predecessor-version":[{"id":15559,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions\/15559"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}