TilesConfigurer.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.servlet.view.tiles;
  17. import org.apache.struts.tiles.DefinitionsFactory;
  18. import org.apache.struts.tiles.DefinitionsFactoryConfig;
  19. import org.apache.struts.tiles.DefinitionsFactoryException;
  20. import org.apache.struts.tiles.TilesUtil;
  21. import org.apache.struts.tiles.TilesUtilImpl;
  22. import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
  23. import org.springframework.beans.factory.InitializingBean;
  24. import org.springframework.util.StringUtils;
  25. import org.springframework.web.context.support.WebApplicationObjectSupport;
  26. /**
  27.  * Helper class to configure Tiles for the Spring Framework. See
  28.  * <a href="http://jakarta.apache.org/struts">http://jakarta.apache.org/struts</a>
  29.  * for more information about Tiles, which basically is a templating mechanism
  30.  * for JSP-based web applications.
  31.  *
  32.  * <p>The TilesConfigurer simply configures a Tiles DefinitionsFactory using a
  33.  * set of files containing definitions, to be accessed by TilesView instances.
  34.  * TilesViews can be managed by any ViewResolver.
  35.  *
  36.  * <p>A typical TilesConfigurer bean definition looks as follows:
  37.  *
  38.  * <pre>
  39.  * &lt;bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
  40.  *   &lt;property name="definitions">
  41.  *     &lt;list>
  42.  *       &lt;value>/WEB-INF/defs/general.xml&lt;/value>
  43.  *       &lt;value>/WEB-INF/defs/widgets.xml&lt;/value>
  44.  *       &lt;value>/WEB-INF/defs/administrator.xml&lt;/value>
  45.  *       &lt;value>/WEB-INF/defs/customer.xml&lt;/value>
  46.  *       &lt;value>/WEB-INF/defs/templates.xml&lt;/value>
  47.  *     &lt;/list>
  48.  *   &lt;/property>
  49.  * &lt;/bean></pre>
  50.  *
  51.  * The values in the list are the actual files containing the definitions.
  52.  *
  53.  * @author Alef Arendsen
  54.  * @author Juergen Hoeller
  55.  * @see TilesView
  56.  * @see org.springframework.web.servlet.ViewResolver
  57.  */
  58. public class TilesConfigurer extends WebApplicationObjectSupport implements InitializingBean {
  59. /** factory class for Tiles */
  60. private Class factoryClass = I18nFactorySet.class;
  61. /** validate the Tiles definitions? */
  62. private boolean validateDefinitions = true;
  63. /** definition URLs mapped to descriptions */
  64. private String[] definitions;
  65. /**
  66.  * Set the factory class for Tiles. Default is I18nFactorySet.
  67.  * @see org.apache.struts.tiles.xmlDefinition.I18nFactorySet
  68.  */
  69. public void setFactoryClass(Class factoryClass) {
  70. this.factoryClass = factoryClass;
  71. }
  72. /**
  73.  * Validate the Tiles definitions? Default is false.
  74.  * @param validateDefinitions <code>true</code> to validate,
  75.  * <code>false</code> otherwise
  76.  */
  77. public void setValidateDefinitions(boolean validateDefinitions) {
  78. this.validateDefinitions = validateDefinitions;
  79. }
  80. /**
  81.  * Set the Tiles definitions, i.e. the list of files.
  82.  * @param definitions the files containing the definitions
  83.  */
  84. public void setDefinitions(String[] definitions) {
  85. this.definitions = definitions;
  86. }
  87. /**
  88.  * Initialize the Tiles definition factory.
  89.  * Delegates to createDefinitionsFactory for the actual creation.
  90.  * @throws DefinitionsFactoryException if an error occurs
  91.  * @see #createDefinitionsFactory
  92.  */
  93. public void afterPropertiesSet() throws DefinitionsFactoryException {
  94. logger.info("TilesConfigurer: initializion started");
  95. // initialize the configuration for the definitions factory
  96. DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig();
  97. factoryConfig.setFactoryClassname(this.factoryClass.getName());
  98. factoryConfig.setParserValidate(this.validateDefinitions);
  99. if (this.definitions != null) {
  100. String defs = StringUtils.arrayToCommaDelimitedString(this.definitions);
  101. logger.info("TilesConfigurer: adding definitions [" + defs + "]");
  102. factoryConfig.setDefinitionConfigFiles(defs);
  103. }
  104. // initialize the definitions factory
  105. DefinitionsFactory factory = createDefinitionsFactory(factoryConfig);
  106. getServletContext().setAttribute(TilesUtilImpl.DEFINITIONS_FACTORY, factory);
  107. logger.info("TilesConfigurer: initialization completed");
  108. }
  109. /**
  110.  * Create the Tiles DefinitionsFactory.
  111.  * @param factoryConfig the configuration for the DefinitionsFactory
  112.  * @return the DefinitionsFactory
  113.  * @throws DefinitionsFactoryException if an error occurs
  114.  */
  115. protected DefinitionsFactory createDefinitionsFactory(DefinitionsFactoryConfig factoryConfig)
  116. throws DefinitionsFactoryException {
  117. return TilesUtil.createDefinitionsFactory(getServletContext(), factoryConfig);
  118. }
  119. }