mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Updated tiles dependency to 2.0-snapshot, added support for tiles and freemarker integration
WW-1418 WW-1450 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@451137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+1
-1
@@ -204,7 +204,7 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.struts.tiles</groupId>
|
||||
<artifactId>tiles-core</artifactId>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* $Id: StrutsSpringObjectFactory.java 439747 2006-09-03 09:22:46Z mrdon $
|
||||
*
|
||||
* Copyright 2006 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.struts2.tiles;
|
||||
|
||||
import org.apache.tiles.listener.TilesListener;
|
||||
import org.apache.tiles.TilesUtilImpl;
|
||||
import org.apache.tiles.TilesUtil;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
/**
|
||||
* Custom TilesListener which can be used to allow freemarker
|
||||
* invocation from tiles components.
|
||||
*
|
||||
* @version $Rev: 397992 $ $Date$
|
||||
*/
|
||||
public class StrutsTilesListener extends TilesListener {
|
||||
|
||||
/**
|
||||
* The key used to identify the freemarker mask.
|
||||
*/
|
||||
public static final String MASK_INIT_PARAM = "freemarker-mask";
|
||||
|
||||
/**
|
||||
* Configured mask;
|
||||
*/
|
||||
private String mask;
|
||||
|
||||
/**
|
||||
* Initialize the tiles system, overriding the TilesUtilImpl
|
||||
* @param servletContextEvent
|
||||
*/
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
||||
ServletContext context = servletContextEvent.getServletContext();
|
||||
mask = context.getInitParameter(MASK_INIT_PARAM);
|
||||
|
||||
if(mask == null) {
|
||||
mask = ".ftl";
|
||||
}
|
||||
|
||||
TilesUtilImpl tilesUtil = new StrutsTilesUtilImpl();
|
||||
TilesUtil.setTilesUtil(tilesUtil);
|
||||
super.contextInitialized(servletContextEvent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* $Id: StrutsSpringObjectFactory.java 439747 2006-09-03 09:22:46Z mrdon $
|
||||
*
|
||||
* Copyright 2006 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.struts2.tiles;
|
||||
|
||||
import org.apache.tiles.TilesUtilImpl;
|
||||
import org.apache.struts2.views.freemarker.FreemarkerResult;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import freemarker.template.TemplateException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Default implementation of TilesUtil.
|
||||
* This class contains default implementation of utilities. This implementation
|
||||
* is intended to be used without Struts.
|
||||
*
|
||||
* TilesUtilImpl implementation used to intercept .ftl requests and
|
||||
* ensure that they are setup properly to take advantage of the
|
||||
* {@link FreemarkerResult}.
|
||||
*
|
||||
* @version $Id$
|
||||
*
|
||||
*/
|
||||
public class StrutsTilesUtilImpl extends TilesUtilImpl {
|
||||
|
||||
/**
|
||||
* The mask used to detect requests which should be intercepted.
|
||||
*/
|
||||
private String mask;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
* Sets the mask to '.ftl'
|
||||
*/
|
||||
public StrutsTilesUtilImpl() {
|
||||
mask = ".ftl";
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional constructor used to specify a specific mask.
|
||||
* @param mask
|
||||
*/
|
||||
public StrutsTilesUtilImpl(String mask) {
|
||||
this.mask = mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enhancement of the default include which allows for freemarker
|
||||
* templates to be intercepted so that the FreemarkerResult can
|
||||
* be used in order to setup the appropriate model.
|
||||
*
|
||||
* @param string the included resource
|
||||
* @param pageContext the current page context
|
||||
* @param b whether or not a flush should occur
|
||||
* @throws IOException
|
||||
* @throws ServletException
|
||||
* @throws Exception
|
||||
*/
|
||||
public void doInclude(String string, PageContext pageContext, boolean b) throws Exception {
|
||||
if(string.endsWith(".ftl")) {
|
||||
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
|
||||
ActionInvocation invocation = ServletActionContext.getActionContext(request).getActionInvocation();
|
||||
FreemarkerResult result = new FreemarkerResult();
|
||||
try {
|
||||
result.doExecute(string, invocation);
|
||||
} catch (TemplateException e) {
|
||||
log.error("Error invoking Freemarker template", e);
|
||||
throw new ServletException("Error invoking Freemarker template.", e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
super.doInclude(string, pageContext, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,12 +28,8 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.dispatcher.ServletDispatcherResult;
|
||||
import org.apache.tiles.ComponentContext;
|
||||
import org.apache.tiles.ComponentDefinition;
|
||||
import org.apache.tiles.ComponentDefinitions;
|
||||
import org.apache.tiles.Controller;
|
||||
import org.apache.tiles.DefinitionsFactory;
|
||||
import org.apache.tiles.TilesUtilImpl;
|
||||
import org.apache.tiles.*;
|
||||
import org.apache.tiles.context.servlet.ServletTilesContext;
|
||||
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.LocaleProvider;
|
||||
@@ -104,6 +100,7 @@ public class TilesResult extends ServletDispatcherResult {
|
||||
HttpServletRequest request = ServletActionContext.getRequest();
|
||||
HttpServletResponse response = ServletActionContext.getResponse();
|
||||
ServletContext servletContext = ServletActionContext.getServletContext();
|
||||
TilesContext tilesContext = new ServletTilesContext(servletContext, request, response);
|
||||
|
||||
this.definitionsFactory =
|
||||
(DefinitionsFactory) servletContext.getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY);
|
||||
@@ -115,8 +112,8 @@ public class TilesResult extends ServletDispatcherResult {
|
||||
}
|
||||
|
||||
// get current component context
|
||||
ComponentContext context = getComponentContext(definition, request);
|
||||
ComponentContext.setContext(context, request);
|
||||
ComponentContext context = getComponentContext(definition, tilesContext);
|
||||
ComponentContext.setContext(context, tilesContext);
|
||||
|
||||
// execute component controller associated with definition, if any
|
||||
Controller controller = getController(definition, request);
|
||||
@@ -124,7 +121,7 @@ public class TilesResult extends ServletDispatcherResult {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Executing Tiles controller [" + controller + "]");
|
||||
}
|
||||
executeController(controller, context, request, response);
|
||||
executeController(controller, context, tilesContext);
|
||||
}
|
||||
|
||||
// determine the path of the definition
|
||||
@@ -163,16 +160,16 @@ public class TilesResult extends ServletDispatcherResult {
|
||||
* Determine the Tiles component context for the given Tiles definition.
|
||||
*
|
||||
* @param definition the Tiles definition to render
|
||||
* @param request current HTTP request
|
||||
* @param tilesContext current TilesContext
|
||||
* @return the component context
|
||||
* @throws Exception if preparations failed
|
||||
*/
|
||||
protected ComponentContext getComponentContext(ComponentDefinition definition, HttpServletRequest request)
|
||||
protected ComponentContext getComponentContext(ComponentDefinition definition, TilesContext tilesContext)
|
||||
throws Exception {
|
||||
ComponentContext context = ComponentContext.getContext(request);
|
||||
ComponentContext context = ComponentContext.getContext(tilesContext);
|
||||
if (context == null) {
|
||||
context = new ComponentContext(definition.getAttributes());
|
||||
ComponentContext.setContext(context, request);
|
||||
ComponentContext.setContext(context, tilesContext);
|
||||
} else {
|
||||
context.addMissing(definition.getAttributes());
|
||||
}
|
||||
@@ -198,14 +195,13 @@ public class TilesResult extends ServletDispatcherResult {
|
||||
*
|
||||
* @param controller the component controller to execute
|
||||
* @param context the component context
|
||||
* @param request current HTTP request
|
||||
* @param response current HTTP response
|
||||
* @param tilesContext current tilesContext
|
||||
* @throws Exception if controller execution failed
|
||||
*/
|
||||
protected void executeController(
|
||||
Controller controller, ComponentContext context, HttpServletRequest request, HttpServletResponse response)
|
||||
Controller controller, ComponentContext context, TilesContext tilesContext)
|
||||
throws Exception {
|
||||
controller.execute(context, request, response, ServletActionContext.getServletContext());
|
||||
controller.execute(tilesContext, context);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user