|
Struts example source code file (DefaultResultMapBuilder.java)
The Struts DefaultResultMapBuilder.java source code/* * $Id: DefaultResultMapBuilder.java 1076544 2011-03-03 07:19:37Z lukaszlenart $ * * 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.convention; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.config.ConfigurationException; import com.opensymphony.xwork2.config.entities.PackageConfig; import com.opensymphony.xwork2.config.entities.ResultConfig; import com.opensymphony.xwork2.config.entities.ResultTypeConfig; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.util.finder.ClassLoaderInterface; import com.opensymphony.xwork2.util.finder.ClassLoaderInterfaceDelegate; import com.opensymphony.xwork2.util.finder.ResourceFinder; import com.opensymphony.xwork2.util.finder.Test; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import javax.servlet.ServletContext; import java.io.IOException; import java.net.URL; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * <p> * This class implements the ResultMapBuilder and traverses the web * application content directory looking for reasonably named JSPs and * other result types as well as annotations. This naming is in this * form: * </p> * * <pre> * /resultPath/namespace/action-<result>.jsp * </pre> * * <p> * If there are any files in these locations than a result is created * for each one and the result names is the last portion of the file * name up to the . (dot). * </p> * * <p> * When results are found, new ResultConfig instances are created. The * result config that is created has a number of thing to be aware of: * </p> * * <ul> * <li>The result config contains the location parameter, which is * required by most result classes to figure out where to find the result. * In addition, the config has all the parameters from the default result-type * configuration.</li> * </ul> * * <p> * After loading the files in the web application, this class will then * use any annotations on the action class to override what was found in * the web application files. These annotations are the {@link Result} * and {@link Results} annotations. These two annotations allow an action * to supply different or non-forward based results for specific return * values of an action method. * </p> * * <p> * The result path used by this class for locating JSPs and other * such result files can be set using the Struts2 constant named * <strong>struts.convention.result.path or using the * {@link org.apache.struts2.convention.annotation.ResultPath} * annotation. * </p> * * <p> * This class will also locate and configure Results in the classpath, * including velocity and FreeMarker templates inside the classpath. * </p> * * <p> * All results that are conigured from resources are given a type corresponding * to the resources extension. The extensions and types are given in the * table below: * </p> * * <table> * <tr> | Extension | Type | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
.jsp | dispatcher * <tr> | .jspx | dispatcher * <tr> | .html | dispatcher * <tr> | .htm | dispatcher * <tr> | .vm | velocity * <tr> | .ftl | freemarker
* </table>
*/
public class DefaultResultMapBuilder implements ResultMapBuilder {
private static final Logger LOG = LoggerFactory.getLogger(DefaultResultMapBuilder.class);
private final ServletContext servletContext;
private Set<String> relativeResultTypes;
private ConventionsService conventionsService;
private boolean flatResultLayout = true;
/**
* Constructs the SimpleResultMapBuilder using the given result location.
*
* @param servletContext The ServletContext for finding the resources of the web application.
* @param container The Xwork container
* @param relativeResultTypes The list of result types that can have locations that are relative
* and the result location (which is the resultPath plus the namespace) prepended to them.
*/
@Inject
public DefaultResultMapBuilder(ServletContext servletContext, Container container,
@Inject("struts.convention.relative.result.types") String relativeResultTypes) {
this.servletContext = servletContext;
this.relativeResultTypes = new HashSet<String>(Arrays.asList(relativeResultTypes.split("\\s*[,]\\s*")));
this.conventionsService = container.getInstance(ConventionsService.class, container.getInstance(String.class, ConventionConstants.CONVENTION_CONVENTIONS_SERVICE));
}
/**
* @param flatResultLayout If 'true' result resources will be expected to be in the form
* ${namespace}/${actionName}-${result}.${extension}, otherwise in the form
* ${namespace}/${actionName}/${result}.${extension}
*/
@Inject("struts.convention.result.flatLayout")
public void setFlatResultLayout(String flatResultLayout) {
this.flatResultLayout = "true".equals(flatResultLayout);
}
/**
* {@inheritDoc}
*/
public Map<String, ResultConfig> build(Class actionClass,
org.apache.struts2.convention.annotation.Action annotation, String actionName,
PackageConfig packageConfig) {
// Get the default result location from the annotation or configuration
String defaultResultPath = conventionsService.determineResultPath(actionClass);
// Add a slash
if (!defaultResultPath.endsWith("/")) {
defaultResultPath = defaultResultPath + "/";
}
// Check for resources with the action name
final String namespace = packageConfig.getNamespace();
if (namespace != null && namespace.startsWith("/")) {
defaultResultPath = defaultResultPath + namespace.substring(1);
} else if (namespace != null) {
defaultResultPath = defaultResultPath + namespace;
}
if (LOG.isTraceEnabled()) {
LOG.trace("Using final calculated namespace [#0]", namespace);
}
// Add that ending slash for concatentation
if (!defaultResultPath.endsWith("/")) {
defaultResultPath += "/";
}
String resultPrefix = defaultResultPath + actionName;
//results from files
Map<String, ResultConfig> results = new HashMapOther Struts examples (source code examples)Here is a short list of links related to this Struts DefaultResultMapBuilder.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.