All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts2.views.tiles.TilesResult Maven / Gradle / Ivy

There is a newer version: 6.6.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.views.tiles;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.result.ServletDispatcherResult;
import org.apache.struts2.tiles.StrutsTilesAnnotationProcessor;
import org.apache.struts2.tiles.annotation.TilesDefinition;
import org.apache.tiles.api.Definition;
import org.apache.tiles.api.TilesContainer;
import org.apache.tiles.api.TilesException;

import com.opensymphony.xwork2.ActionInvocation;
import org.apache.tiles.api.access.TilesAccess;
import org.apache.tiles.api.mgmt.MutableTilesContainer;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.Request;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;

/**
 * Renders a view using struts-tiles. In your web.xml file, you need to add a TilesListener.
 * 

* <listener> * <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> * </listener> *

* In struts.xml, use type="tiles" on your <result>. *

* <action name="editUser" class="userAction" method="edit"> * <result name="success" type="tiles">userForm</result> * <result name="input" type="tiles">userList</result> * </action> *

* Making this result type the default for the current package. *

* <result-types> * <result-type name="tiles" * class="org.apache.struts2.views.tiles.TilesResult" default="true" /> * </result-types> *

* You have to configure tiles itself. Therefore you can add tiles.xml either * to resources or WEB-INF. You may also use annotations like {@link TilesDefinition}. */ public class TilesResult extends ServletDispatcherResult { private static final long serialVersionUID = -3806939435493086244L; private static final Logger LOG = LogManager.getLogger(TilesResult.class); public TilesResult() { super(); } public TilesResult(String location) { super(location); } /** * Dispatches to the given location. Does its forward via a RequestDispatcher. If the * dispatch fails a 404 error will be sent back in the http response. * * @param location the location to dispatch to. * @param invocation the execution state of the action * @throws Exception if an error occurs. If the dispatch fails the error will go back via the * HTTP request. */ public void doExecute(String location, ActionInvocation invocation) throws Exception { StrutsTilesAnnotationProcessor annotationProcessor = new StrutsTilesAnnotationProcessor(); TilesDefinition tilesDefinition = null; Object action = invocation.getAction(); String actionName = invocation.getInvocationContext().getActionName(); if (StringUtils.isEmpty(location)) { LOG.trace("location not set -> action must have one @TilesDefinition"); tilesDefinition = annotationProcessor.findAnnotation(action, null); location = StringUtils.isNotEmpty(tilesDefinition.name()) ? tilesDefinition.name() : actionName; LOG.debug("using new location name '{}' and @TilesDefinition '{}'", location, tilesDefinition); } setLocation(location); ServletContext servletContext = ServletActionContext.getServletContext(); ApplicationContext applicationContext = ServletUtil.getApplicationContext(servletContext); TilesContainer container = TilesAccess.getContainer(applicationContext); HttpServletRequest httpRequest = ServletActionContext.getRequest(); HttpServletResponse httpResponse = ServletActionContext.getResponse(); Request request = new ServletRequest(applicationContext, httpRequest, httpResponse); boolean definitionValid = false; try { LOG.debug("checking if tiles definition exists '{}'", location); definitionValid = container.isValidDefinition(location, request); } catch (TilesException e) { LOG.warn("got TilesException while checking if definiton exists, ignoring it", e); } if (!definitionValid) { if (tilesDefinition == null) { LOG.trace("tilesDefinition not found yet, searching in action"); tilesDefinition = annotationProcessor.findAnnotation(action, location); } if (tilesDefinition != null) { Definition definition = annotationProcessor.buildTilesDefinition(location, tilesDefinition); if (container instanceof MutableTilesContainer) { LOG.debug("registering tiles definition with name '{}'", definition.getName()); ((MutableTilesContainer)container).register(definition, request); } else { LOG.error("cannot register tiles definition as tiles container is not mutable!"); } } else { LOG.warn("could not find @TilesDefinition for action: {}", actionName); } } container.render(location, request); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy