How to integrate Struts 2 with Tiles 3

How do we integrate Struts 2 with Tiles 3? The struts2-tiles-plugin platform is currently (2.3.4.1) working with the old version of tiles (version 2.0.6), this can be a little annoying.

This is an independent answer to help others with their integration.

+6
source share
3 answers

Thanks to Ken, a new plugin was added to Struts 2 to support the Tiles 3 result type, it should be available with the upcoming new version - Struts 2.3.9

https://cwiki.apache.org/confluence/display/WW/Tiles+3+Plugin

+5
source

The solution is to add the necessary dependencies, load the fragments with the appropriate listener, and create a custom result type. Fortunately, these steps are pretty simple, after which you can follow the normal snippets of 2 examples to define patterns.

1) Dependencies (start with the main struts project, but in this example I will use conventions, so it's best to add struts2-conventions-plugin, it will include struts2-core et al.):

  • DO NOT enable struts2-tiles-plugin
  • groupId : org.apache.tiles, artifiactId : add-on fragments, version : 3.0.1
  • groupId : org.slf4j, artifiactId : jcl-over-slf4j, version : 1.5.8
  • groupId : org.slf4j, artifiactId : slf4j-jdk14, version : 1.5.8

Note A higher version of slf4j dependencies may work, I have not tested this.

2) load tiles with the appropriate listener

This example includes the full web.xml, lines 3-5 - this is the only thing that should be new to someone familiar with struts2.

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener> <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

3) create a custom result type

We need to define a custom result type for use with our actions:

 package com.quaternion.result; import com.opensymphony.xwork2.ActionInvocation; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.dispatcher.ServletDispatcherResult; import org.apache.tiles.TilesContainer; import org.apache.tiles.access.TilesAccess; import org.apache.tiles.request.ApplicationContext; import org.apache.tiles.request.servlet.ServletRequest; import org.apache.tiles.request.servlet.ServletUtil; public class TilesResult extends ServletDispatcherResult { public TilesResult() { super(); } public TilesResult(String location) { super(location); } @Override public void doExecute(String location, ActionInvocation invocation) throws Exception { //location = "test.definition"; //for test setLocation(location); ServletContext context = ServletActionContext.getServletContext(); ApplicationContext applicationContext = ServletUtil.getApplicationContext(context); TilesContainer container = TilesAccess.getContainer(applicationContext); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ServletRequest servletRequest = new ServletRequest(applicationContext, request, response); container.render(location, servletRequest); } } 

4) We also need to tell struts2 about our result type:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.ui.theme" value="simple" /> <package name="tiles-package" namespace="" extends="struts-default"> <result-types> <result-type default="true" name="tiles-result" class="com.quaternion.result.TilesResult"/> </result-types> </package> </struts> 

Given that we can now use tiles in our projects, suppose we created a tile definition called "test.definition", we can say that our action uses this tile by doing the following:

 package com.quaternion.demo.action.test; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; @ParentPackage("tiles-package") @Result(type="tiles-result", location="test.definition") public class QuaternionResultTest extends ActionSupport{} 

Whatever it is, it will allow you to customize any version of struts2 with 3+ tiles, see http://tiles.apache.org/framework/index.html for more configuration information.

+7
source
 <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> 

use the mentioned doctype in the tiles.xml file

0
source

All Articles