12
Jun

Using of Struts Tiles + Spring Framework

Posted by eugene as Spring Framework

One of the features of Struts Framework, besides of MVC for the web was a tag Tiles library which is going with it. Tiles possess with lots of possibilities, but most important that they help to make the inheritance of pages.

Yes, as well as the objects, define the basic techniques in the root, and in the ancestors override something and add the new one. Herewith, the library can be used separately from the Struts, it is perfectly integrated into the Spring Framework.
How can the JSP page structure look like with no tiles?

[header include]
[page body]
[footer include]

Well, there’s nothing complicated, just a typical PHP style. Now, let’s say in the header we have some changes such as a picture section, connects additional css, JavaScript, or something else.

That’s where it all begins: in the header is written terrible logic of covering the entire project, which depending on the address, what is beginning to turn on / off at inklyude transferred to different settings to make it all work, etc. Getting a bit uncomfortable.

Tiles would have looked in another way in it, the main template:

Header begin
Picture with [address]
[insert add. code]
Header end
[insert page body]
footer

and then, on the other pages, we write the header that inherit the parent, and that it replaces the address of the image, the body of the page, and sometimes extra сode.

The default values of these fields are configured in the configuration file, tiles, and can be empty.

That’s how it would look like in the form of JSP code:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
      <title><tiles:getAsString name="title"/></title>
      <tiles:getAsString name="head" />
  </head>
  <body onload="<tiles:getAsString name="onLoad"/>">
<div id="header">
      MySuperPuperSite!
<h1><tiles:getAsString name="title"/></h1>
</div>
 
      <tiles:getAsString name="body" />
<div id="footer">
      copyrights, about the author or what there is needed on the bottom of the page
      </div>
 
  </body>
</html>

This is now

The page that wants to inherit it as follows:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<tiles:insert definition="layout.root" flush="false">
  <tiles:put name="title">About site</tiles:put>
  <tiles:put name="body" direct="true">
     Any text
  </tiles:put>
</tiles:insert>

You can view this as the inheritance of pages, we are overriding methods only in the object, but here are bunches of html.

Setting of tiles in spring

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer"
          lazy-init="false">
<property name="factoryClass">
       <value>org.apache.struts.tiles.xmlDefinition.I18nFactorySet</value>
   </property>
<property name="definitions">
<list>
           <value>/WEB-INF/tiles.xml</value>
       </list>
   </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    lazy-init="false">
<property name="requestContextAttribute"><value>requestContext</value></property>
<property name="viewClass">
        <value>org.springframework.web.servlet.view.tiles.TilesView</value>
    </property>
</bean>

Add custom jsp tag configuration in web.xml:

<jsp-config>
    <taglib>
       <taglib-uri>/tags/struts-tiles</taglib-uri>
       <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
    </taglib>
</jsp-config>

Setting up links of the Tiles names to files

There is one catch: jsp obtained directly won’t work if they are specified in theModelAndView controller. Here is only indicated the tile identifier, and the pages themselves are configured in WEB-INF/tiles.xml (it is in our case, but generally in a filethat is specified in the value of the bean definitions tilesConfigurer).

It must be a solution I’ve found some of the following form:

<?xml version="1.0"?>
 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
    <definition name="layout.error" path="/tiles/common/errorLayout.jsp"/>
    <definition name="layout.simple" path="/tiles/common/simpleLayout.jsp">
<put name="head" value=""/>
    </definition>
    <definition name="common.header" path="/tiles/common/header.jsp"/>
    <definition name="common.footer" path="/tiles/common/footer.jsp"/>
    <definition name="/index" path="/index.jsp"/>
    <definition name="user/login" path="/user/login.jsp"/>
    <definition name="404" path="/errors/404.jsp"/>
</tiles-definitions>

where name is the name of the tile, and path is the path to the existing jsp. And we have to prescribe all the pages in this way that is very uncomfortable, especially annoying when the name and path are actually the same (for both the index and user / login, differs only in that I was too lazy to write .jsp in name).

In general, something like that.


No Comments