You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Jim Cushing (JIRA)" <ji...@apache.org> on 2007/08/29 16:16:34 UTC

[jira] Updated: (WW-2146) Codebehind only looks for templates in the web root, not in the class path

     [ https://issues.apache.org/struts/browse/WW-2146?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jim Cushing updated WW-2146:
----------------------------

    Attachment: CodebehindUnknownHandler.java.patch

Patch to enable Codebehind to look in the classpath for templates

> Codebehind only looks for templates in the web root, not in the class path
> --------------------------------------------------------------------------
>
>                 Key: WW-2146
>                 URL: https://issues.apache.org/struts/browse/WW-2146
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Plugins
>    Affects Versions: 2.0.9
>            Reporter: Jim Cushing
>         Attachments: CodebehindUnknownHandler.java.patch
>
>
> Normally, Struts 2 will look for templates (Freemarker, Velocity) first in the web root, then in the class path. This allows templates to be bundled with a JAR, but overridden in the web root. When using Codebehind, however, it only looks for the templates in the web root, disabling a valueable Struts 2 feature. The following patch to CodebehindUnknownHandler.java fixes this issue, though it has only been tested with Freemarker. It has not been tested with Velocity, and it does attempt to look for JSPs in the classpath, which may not be appropriate.
> ------------------------
> Index: src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
> ===================================================================
> --- src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java	(revision 570808)
> +++ src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java	(working copy)
> @@ -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;
> @@ -107,7 +108,7 @@
>              }
>              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 +149,7 @@
>              }
>              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 +159,7 @@
>              
>              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,7 +210,23 @@
>          }
>          return prefix + ns;
>      }
> +    
> +    private 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 = this.getClass().getResource(path);
> +            if (template != null && LOG.isDebugEnabled()) {
> +                LOG.debug("Loaded template '" + path + "' from class path.");                
> +            }
> +        }
> +        return template;
> +    }
>  
> +
>      /**
>       * Not used
>       */

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.