mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
Adding ability to load templates from classpath, in addition to web context
WW-2146 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@573816 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+21
-3
@@ -21,6 +21,7 @@
|
||||
package org.apache.struts2.codebehind;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -32,6 +33,7 @@ import javax.servlet.ServletContext;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.struts2.util.ClassLoaderUtils;
|
||||
|
||||
import com.opensymphony.xwork2.Action;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
@@ -107,7 +109,7 @@ public class CodebehindUnknownHandler implements UnknownHandler {
|
||||
}
|
||||
String path = string(pathPrefix, actionName, "." , ext);
|
||||
try {
|
||||
if (servletContext.getResource(path) != null) {
|
||||
if (locateTemplate(path) != null) {
|
||||
actionConfig = buildActionConfig(path, namespace, actionName, resultsByExtension.get(ext));
|
||||
break;
|
||||
}
|
||||
@@ -148,7 +150,7 @@ public class CodebehindUnknownHandler implements UnknownHandler {
|
||||
}
|
||||
String path = string(pathPrefix, actionName, "-", resultCode, "." , ext);
|
||||
try {
|
||||
if (servletContext.getResource(path) != null) {
|
||||
if (locateTemplate(path) != null) {
|
||||
result = buildResult(path, resultCode, resultsByExtension.get(ext), actionContext);
|
||||
break;
|
||||
}
|
||||
@@ -158,7 +160,7 @@ public class CodebehindUnknownHandler implements UnknownHandler {
|
||||
|
||||
path = string(pathPrefix, actionName, "." , ext);
|
||||
try {
|
||||
if (servletContext.getResource(path) != null) {
|
||||
if (locateTemplate(path) != null) {
|
||||
result = buildResult(path, resultCode, resultsByExtension.get(ext), actionContext);
|
||||
break;
|
||||
}
|
||||
@@ -209,6 +211,22 @@ public class CodebehindUnknownHandler implements UnknownHandler {
|
||||
}
|
||||
return prefix + ns;
|
||||
}
|
||||
|
||||
URL locateTemplate(String path) throws MalformedURLException {
|
||||
URL template = servletContext.getResource(path);
|
||||
if (template != null) {
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Loaded template '" + path + "' from servlet context.");
|
||||
}
|
||||
} else {
|
||||
template = ClassLoaderUtils.getResource(path, getClass());
|
||||
if (template != null && LOG.isDebugEnabled()) {
|
||||
LOG.debug("Loaded template '" + path + "' from class path.");
|
||||
}
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Not used
|
||||
|
||||
+19
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
package org.apache.struts2.codebehind;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
@@ -28,6 +30,7 @@ import org.apache.struts2.StrutsTestCase;
|
||||
import org.apache.struts2.config.NullResult;
|
||||
import org.apache.struts2.dispatcher.ServletDispatcherResult;
|
||||
|
||||
import com.mockobjects.dynamic.C;
|
||||
import com.mockobjects.dynamic.Mock;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
@@ -73,6 +76,22 @@ public class CodebehindUnknownHandlerTest extends StrutsTestCase {
|
||||
assertEquals("/foo/", handler.determinePath("/", "foo"));
|
||||
}
|
||||
|
||||
public void testLocateTemplate() throws MalformedURLException {
|
||||
URL url = new URL("file:/foo.xml");
|
||||
mockServletContext.expectAndReturn("getResource", C.args(C.eq("/foo.xml")), url);
|
||||
assertEquals(url, handler.locateTemplate("/foo.xml"));
|
||||
mockServletContext.verify();
|
||||
|
||||
}
|
||||
|
||||
public void testLocateTemplateFromClasspath() throws MalformedURLException {
|
||||
mockServletContext.expectAndReturn("getResource", C.args(C.eq("struts-plugin.xml")), null);
|
||||
URL url = handler.locateTemplate("struts-plugin.xml");
|
||||
assertNotNull(url);
|
||||
assertTrue(url.toString().endsWith("struts-plugin.xml"));
|
||||
mockServletContext.verify();
|
||||
}
|
||||
|
||||
public static class SomeResult implements Result {
|
||||
|
||||
public String location;
|
||||
|
||||
Reference in New Issue
Block a user