1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package org.apache.struts.tiles2.actions; 23 24 import jakarta.servlet.http.HttpServletRequest; 25 import jakarta.servlet.http.HttpServletResponse; 26 27 import org.apache.struts.action.Action; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.tiles.TilesContainer; 32 import org.apache.tiles.access.TilesAccess; 33 import org.apache.tiles.request.ApplicationContext; 34 import org.apache.tiles.request.Request; 35 import org.apache.tiles.request.jakarta.servlet.ServletRequest; 36 import org.apache.tiles.request.jakarta.servlet.ServletUtil; 37 import org.slf4j.Logger; 38 import org.slf4j.LoggerFactory; 39 40 /** 41 * <p>An <strong>Action</strong> that dispatches to a Tiles Definition 42 * that is named by the request parameter whose name is specified 43 * by the <code>parameter</code> property of the corresponding 44 * ActionMapping. 45 * This action is useful in following situations: 46 * <li> 47 * <ul>To associate an Url to a definition</ul> 48 * <ul>To use Struts <html:link> tag on a definition</ul> 49 * </li> 50 * <p>To configure the use of this action in your 51 * <code>struts-config.xml</code> file, create an entry like this:</p> 52 * 53 * <code> 54 * <action path="/saveSubscription" 55 * type="org.apache.struts.tiles2.actions.DefinitionDispatcherAction" 56 * parameter="def"/> 57 * <forward name="success" path="anything" //> 58 * <forward name="error" path="path.to.error.page" //> 59 * </code> 60 * 61 * <p>which will use the value of the request parameter named "def" 62 * to pick the appropriate definition name. 63 * <p> The value for success doesn't matter. The forward will forward to 64 * appropriate definition. 65 * <p> The value for error should denote a valid jsp path or definition name. 66 * 67 * @version $Rev$ $Date$ 68 */ 69 public class DefinitionDispatcherAction extends Action { 70 private static final long serialVersionUID = 9052008321390723214L; 71 72 /** 73 * The {@code Log} instance for this class. 74 */ 75 private transient final Logger log = 76 LoggerFactory.getLogger(DefinitionDispatcherAction.class); 77 78 /** 79 * Process the specified HTTP request, and create the corresponding HTTP 80 * response (or forward to another web component that will create it), 81 * with provision for handling exceptions thrown by the business logic. 82 * 83 * @param mapping The ActionMapping used to select this instance 84 * @param form The optional ActionForm bean for this request (if any) 85 * @param req The HTTP request we are processing 86 * @param res The HTTP response we are creating 87 * 88 * @throws Exception if the application business logic throws 89 * an exception 90 * @return The forward object.. 91 * @since Struts 1.1 92 */ 93 public ActionForward execute( 94 ActionMapping mapping, 95 ActionForm form, 96 HttpServletRequest req, 97 HttpServletResponse res) 98 throws Exception { 99 100 // Identify the request parameter containing the method name 101 // If none defined, use "def" 102 String parameter = mapping.getParameter(); 103 if (parameter == null) { 104 parameter = "def"; 105 } 106 107 // Identify the method name to be dispatched to 108 String name = req.getParameter(parameter); 109 if (name == null) { 110 log.error("Can't get parameter '{}'.", parameter); 111 112 return mapping.findForward("error"); 113 } 114 115 // Try to dispatch to requested definition 116 // Read definition from factory, but we can create it here. 117 ApplicationContext applicationContext = ServletUtil 118 .getApplicationContext(req.getSession().getServletContext()); 119 Request request = new ServletRequest(applicationContext, 120 req, res); 121 TilesContainer container = TilesAccess.getContainer(applicationContext); 122 if (container != null 123 && container.isValidDefinition(name, request)) { 124 container.render(name, request); 125 } else { 126 log.error("Can't get definition '{}'.", name); 127 return mapping.findForward("error"); 128 } 129 130 return mapping.findForward("success"); 131 } 132 }