mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-2667 REST should not extend Codebehind
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@663603 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+66
-20
@@ -84,7 +84,7 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
* to use in place of "struts-default".
|
||||
*/
|
||||
protected static final String DEFAULT_PARENT_PACKAGE = "struts.configuration.classpath.defaultParentPackage";
|
||||
|
||||
|
||||
/**
|
||||
* A setting to disable action scanning.
|
||||
*/
|
||||
@@ -109,10 +109,25 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
*/
|
||||
private boolean forceLowerCase = true;
|
||||
|
||||
protected static final String CLASS_SUFFIX = "struts.codebehind.classSuffix";
|
||||
/**
|
||||
* Default suffix that can be used to indicate POJO "Action" classes.
|
||||
*/
|
||||
private static final String ACTION = "Action";
|
||||
protected String classSuffix = "Action";
|
||||
|
||||
protected static final String CHECK_IMPLEMENTS_ACTION = "struts.codebehind.checkImplementsAction";
|
||||
|
||||
/**
|
||||
* When testing a class, check that it implements Action
|
||||
*/
|
||||
protected boolean checkImplementsAction = true;
|
||||
|
||||
protected static final String CHECK_ANNOTATION = "struts.codebehind.checkAnnotation";
|
||||
|
||||
/**
|
||||
* When testing a class, check that it has an @Action annotation
|
||||
*/
|
||||
protected boolean checkAnnotation = true;
|
||||
|
||||
/**
|
||||
* Helper class to scan class path for server pages.
|
||||
@@ -126,7 +141,7 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
* @see #needsReload
|
||||
*/
|
||||
private boolean initialized = false;
|
||||
|
||||
|
||||
private boolean disableActionScanning = false;
|
||||
|
||||
private PackageLoader packageLoader;
|
||||
@@ -165,12 +180,12 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
return ClassLoaderUtil.getResource(path, getClass());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Inject("actionPackages")
|
||||
public void setActionPackages(String packages) {
|
||||
this.actionPackages = packages;
|
||||
}
|
||||
|
||||
|
||||
public void setServletContext(ServletContext ctx) {
|
||||
this.servletContext = ctx;
|
||||
}
|
||||
@@ -184,7 +199,27 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
public void setDisableActionScanning(String disableActionScanning) {
|
||||
this.disableActionScanning = "true".equals(disableActionScanning);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check that the class implements Action
|
||||
*
|
||||
* @param checkImplementsAction True to check
|
||||
*/
|
||||
@Inject(value=CHECK_IMPLEMENTS_ACTION, required=false)
|
||||
public void setCheckImplementsAction(String checkImplementsAction) {
|
||||
this.checkImplementsAction = "true".equals(checkImplementsAction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the class has an @Action annotation
|
||||
*
|
||||
* @param checkImplementsAction True to check
|
||||
*/
|
||||
@Inject(value=CHECK_ANNOTATION, required=false)
|
||||
public void setCheckAnnotation(String checkAnnotation) {
|
||||
this.checkAnnotation = "true".equals(checkAnnotation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a default parent package for the actions.
|
||||
*
|
||||
@@ -214,10 +249,20 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
public void setDefaultPagePrefix(String defaultPagePrefix) {
|
||||
this.defaultPagePrefix = defaultPagePrefix;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Default suffix that can be used to indicate POJO "Action" classes.
|
||||
*
|
||||
* @param defaultPagePrefix the defaultPagePrefix to set
|
||||
*/
|
||||
@Inject(value=CLASS_SUFFIX, required=false)
|
||||
public void setClassSuffix(String classSuffix) {
|
||||
this.classSuffix = classSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to use a lowercase letter as the initial letter of an action.
|
||||
*
|
||||
*
|
||||
* @param force If false, actions will retain the initial uppercase letter from the Action class.
|
||||
* (<code>view.action</code> (true) versus <code>View.action</code> (false)).
|
||||
*/
|
||||
@@ -268,16 +313,16 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
// Match Action implementations and classes ending with "Action"
|
||||
public boolean matches(Class type) {
|
||||
// TODO: should also find annotated classes
|
||||
return (Action.class.isAssignableFrom(type) ||
|
||||
return ((checkImplementsAction && Action.class.isAssignableFrom(type)) ||
|
||||
type.getSimpleName().endsWith(getClassSuffix()) ||
|
||||
type.getAnnotation(org.apache.struts2.config.Action.class) != null);
|
||||
(checkAnnotation && type.getAnnotation(org.apache.struts2.config.Action.class) != null));
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
protected String getClassSuffix() {
|
||||
return ACTION;
|
||||
return classSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -295,8 +340,8 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
String actionPackage = cls.getPackage().getName();
|
||||
String actionNamespace = null;
|
||||
String actionName = null;
|
||||
|
||||
org.apache.struts2.config.Action actionAnn =
|
||||
|
||||
org.apache.struts2.config.Action actionAnn =
|
||||
(org.apache.struts2.config.Action) cls.getAnnotation(org.apache.struts2.config.Action.class);
|
||||
if (actionAnn != null) {
|
||||
actionName = actionAnn.name();
|
||||
@@ -312,7 +357,7 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
LOG.debug("ClasspathPackageProvider: Processing class "+name);
|
||||
}
|
||||
name = name.substring(pkg.length() + 1);
|
||||
|
||||
|
||||
actionNamespace = "";
|
||||
actionName = name;
|
||||
int pos = name.lastIndexOf('.');
|
||||
@@ -360,6 +405,7 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ResultTypeConfig defaultResultType = packageLoader.getDefaultResultType(pkgConfig);
|
||||
ActionConfig actionConfig = new ActionConfig.Builder(actionPackage, actionName, cls.getName())
|
||||
.addResultConfigs(new ResultMap<String,ResultConfig>(cls, actionName, defaultResultType))
|
||||
@@ -388,10 +434,10 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
parent = loadPackageConfig(actionNamespace, actionPackage, null);
|
||||
actionNamespace = ns.value();
|
||||
actionPackage = actionClass.getName();
|
||||
|
||||
// See if the namespace has been overridden by the @Action annotation
|
||||
|
||||
// See if the namespace has been overridden by the @Action annotation
|
||||
} else {
|
||||
org.apache.struts2.config.Action actionAnn =
|
||||
org.apache.struts2.config.Action actionAnn =
|
||||
(org.apache.struts2.config.Action) actionClass.getAnnotation(org.apache.struts2.config.Action.class);
|
||||
if (actionAnn != null && !actionAnn.DEFAULT_NAMESPACE.equals(actionAnn.namespace())) {
|
||||
// we pass null as the namespace in case the parent package hasn't been loaded yet
|
||||
@@ -401,7 +447,7 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
PackageConfig.Builder pkgConfig = packageLoader.getPackage(actionPackage);
|
||||
if (pkgConfig == null) {
|
||||
pkgConfig = new PackageConfig.Builder(actionPackage);
|
||||
@@ -429,7 +475,7 @@ public class ClasspathPackageProvider implements PackageProvider {
|
||||
}
|
||||
|
||||
System.out.println("class:"+actionClass+" parent:"+parent+" current:"+(pkgConfig != null ? pkgConfig.getName() : ""));
|
||||
|
||||
|
||||
return pkgConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,5 +34,5 @@
|
||||
<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />
|
||||
|
||||
<package name="codebehind-default" extends="struts-default">
|
||||
</package>
|
||||
</package>
|
||||
</struts>
|
||||
|
||||
@@ -34,11 +34,6 @@
|
||||
<name>Struts 2 REST Plugin</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.struts</groupId>
|
||||
<artifactId>struts2-codebehind-plugin</artifactId>
|
||||
<version>${pom.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.xstream</groupId>
|
||||
<artifactId>xstream</artifactId>
|
||||
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* 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.rest;
|
||||
|
||||
import org.apache.struts2.config.ClasspathPackageProvider;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.ResolverUtil.ClassTest;
|
||||
|
||||
/**
|
||||
* Checks for actions ending in Controller indicating a Rest controller
|
||||
*/
|
||||
public class ControllerClasspathPackageProvider extends ClasspathPackageProvider {
|
||||
|
||||
/**
|
||||
* A setting to disable action scanning.
|
||||
*/
|
||||
protected static final String DISABLE_REST_CONTROLLER_SCANNING = "struts.configuration.rest.disableControllerScanning";
|
||||
|
||||
@Override
|
||||
protected ClassTest createActionClassTest() {
|
||||
return new ClassTest() {
|
||||
// Match Action implementations and classes ending with "Controller"
|
||||
public boolean matches(Class type) {
|
||||
return (type.getSimpleName().endsWith("Controller"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getClassSuffix() {
|
||||
return "Controller";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignore setting to disable action scanning from the codebehind plugin.
|
||||
*
|
||||
* @param disableActionScanning True to disable
|
||||
*/
|
||||
@Override
|
||||
@Inject(value=DISABLE_ACTION_SCANNING, required=false)
|
||||
public void setDisableActionScanning(String disableActionScanning) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables controller scanning.
|
||||
*
|
||||
* @param disableActionScanning True to disable
|
||||
*/
|
||||
@Inject(value=DISABLE_REST_CONTROLLER_SCANNING, required=false)
|
||||
public void setDisableRestControllerScanning(String disableActionScanning) {
|
||||
super.setDisableActionScanning(disableActionScanning);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,13 +24,11 @@
|
||||
<!DOCTYPE struts PUBLIC
|
||||
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
|
||||
"http://struts.apache.org/dtds/struts-2.0.dtd">
|
||||
|
||||
|
||||
<struts>
|
||||
|
||||
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="rest" class="org.apache.struts2.rest.RestActionProxyFactory" />
|
||||
<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="rest" class="org.apache.struts2.rest.RestActionMapper" />
|
||||
|
||||
<bean type="com.opensymphony.xwork2.config.PackageProvider" name="rest" class="org.apache.struts2.rest.ControllerClasspathPackageProvider" />
|
||||
|
||||
<bean class="org.apache.struts2.rest.ContentTypeHandlerManager" />
|
||||
|
||||
@@ -43,13 +41,14 @@
|
||||
<constant name="struts.mapper.class" value="rest" />
|
||||
<constant name="struts.mapper.idParameterName" value="id" />
|
||||
<constant name="struts.action.extension" value="xhtml,,xml,json" />
|
||||
|
||||
|
||||
<!-- Disable the scanning by the codebehind plugin to prevent duplicates -->
|
||||
<constant name="struts.configuration.classpath.disableActionScanning" value="true" />
|
||||
|
||||
<constant name="struts.configuration.rest.disableControllerScanning" value="false" />
|
||||
<constant name="struts.codebehind.classSuffix" value="Controller"/>
|
||||
<constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
|
||||
<constant name="struts.codebehind.action.checkAnnotation" value="false"/>
|
||||
|
||||
<constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />
|
||||
|
||||
|
||||
<package name="rest-default" extends="struts-default">
|
||||
<result-types>
|
||||
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult">
|
||||
@@ -108,7 +107,7 @@
|
||||
<param name="excludeMethods">input,back,cancel,browse,index,show,edit,editNew</param>
|
||||
</interceptor-ref>
|
||||
</interceptor-stack>
|
||||
|
||||
|
||||
</interceptors>
|
||||
|
||||
<default-interceptor-ref name="restDefaultStack"/>
|
||||
|
||||
Reference in New Issue
Block a user