Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment:


Reformatted the example code

...

Here

...

is

...

a

...

simple

...

example

...

of

...

tiles

...

use

...

with

...

spring's

...

TilesConfigurer.

...

There

...

is

...

really

...

no

...

need

...

for

...

spring

...

but

...

tiles

...

definitions

...

must

...

somehow

...

be

...

initialized.

...

If

...

you

...

do

...

not

...

use

...

Spring

...

you

...

could

...

use

...

the

...

following

...

context

...

listener

...

that

...

is

...

exatcly

...

how

...

Spring

...

configures

...

tiles

...

definitions:

Code Block


package com.opensymphony.webwork.views.tiles;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.struts.tiles.DefinitionsFactoryConfig;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.TilesUtil;
import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;

/*\*
 \* Modified from spring's source
 \*
 \* here's how a smaple web xml should look like:
&nbsp;\* <web-app>
&nbsp;*&nbsp;&nbsp;*   <context-param>
&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;*     <param-name>tilesDefinitions</param-name>
&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;*     <param-value>/WEB-INF/tiles.xml</param-value>
&nbsp;*&nbsp;&nbsp;*   </context-param>
&nbsp;*&nbsp;&nbsp;
&nbsp;*&nbsp;&nbsp;*  
*   <listener>
&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*      <listener-class>com.opensymphony.webwork.views.tiles.TilesConfigurer</listener-class>
&nbsp;*&nbsp;&nbsp;*   </listener>
&nbsp;\* </web-app>
&nbsp;\*
&nbsp;\* To use the difinitionsdefinitions specified you would use a dispatcher result (since
&nbsp;\* tiles jsp is just another jsp) to render tiles view.
&nbsp;*/
public class TilesConfigurer implements ServletContextListener {

&nbsp;&nbsp;&nbsp;    private boolean initialized = false;
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;   
    public void contextInitialized (ServletContextEvent evt) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       
        if (\!initialized) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            factoryConfig.setFactoryClassname(I18nFactorySet.class.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            factoryConfig.setParserValidate(true);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            factoryConfig.setDefinitionConfigFiles(evt.getServletContext().getInitParameter("tilesDefinitions"));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            try {
                TilesUtil.createDefinitionsFactory(evt.getServletContext(), factoryConfig);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            } catch (DefinitionsFactoryException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;            }
            initialized = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;        }
       
    }

    public void contextDestroyed (ServletContextEvent evt) {
&nbsp;&nbsp;&nbsp;    }
&nbsp;&nbsp;&nbsp;   
}
&nbsp;