Struts 2
  1. Struts 2
  2. WW-3764

Reading(theme.properties)PropertyFileFromFileSystem doesn't include templatePath value from web.xml

    Details

    • Flags:
      Important

      Description

      There is an option in Struts2 to load the freemarker template from path by setting in web.xml (templatePath).Templates are loaded from the file system if we configure the file path.

      Also there is an option to extend the themes by setting the value for "parent" key in the themes.properties within the template folder

      readProperty() method helps us to read the themes.properties file from the file system and also from the classpath
      Aruguments :-
      Combination of template directory ,template and file name "theme.properties" are passed as propName

      This method doesn't help us to load the theme.properties from the specified templatePath

      private InputStream readProperty(String propName) {
              InputStream is = tryReadingPropertyFileFromFileSystem(propName);
              if (is == null) {
                  is = readPropertyFromClasspath(propName);
              }
              return is;
          }
      
      1. struts2-blank.war
        4.12 MB
        Vijayakannan

        Issue Links

          Activity

          Vijayakannan created issue -
          Hide
          Lukasz Lenart added a comment -

          Did you try to configure struts.ui.templateDir ?

          Show
          Lukasz Lenart added a comment - Did you try to configure struts.ui.templateDir ?
          Hide
          Lukasz Lenart added a comment -

          theme.properties are loaded from struts.ui.templateDir if specified. Or do I miss something ?

          Show
          Lukasz Lenart added a comment - theme.properties are loaded from struts.ui.templateDir if specified. Or do I miss something ?
          Hide
          Vijayakannan added a comment -

          When i try to load the struts default template from application path "/WEB-INF/template" by setting struts.ui.templateDir in stuts.xml, i am geting this Exception.

          SEVERE: Could not open template
          java.io.FileNotFoundException: Template /WEB-INF/template/xhtml/a.ftl not found.
          at freemarker.template.Configuration.getTemplate(Configuration.java:489)
          at freemarker.template.Configuration.getTemplate(Configuration.java:452)
          at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:96)
          at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
          at org.apache.struts2.components.ClosingUIBean.start(ClosingUIBean.java:59)
          at org.apache.struts2.components.Anchor.start(Anchor.java:132)
          at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53)

          Show
          Vijayakannan added a comment - When i try to load the struts default template from application path "/WEB-INF/template" by setting struts.ui.templateDir in stuts.xml, i am geting this Exception. SEVERE: Could not open template java.io.FileNotFoundException: Template /WEB-INF/template/xhtml/a.ftl not found. at freemarker.template.Configuration.getTemplate(Configuration.java:489) at freemarker.template.Configuration.getTemplate(Configuration.java:452) at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:96) at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559) at org.apache.struts2.components.ClosingUIBean.start(ClosingUIBean.java:59) at org.apache.struts2.components.Anchor.start(Anchor.java:132) at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53)
          Hide
          Vijayakannan added a comment -

          Root cause for the above Exception:

          FreemarkerTemplateEngine.java :- List<Template> templates = templateContext.getTemplate().getPossibleTemplates(this);

          The above method call BaseTemplateEngine.java loadProperties(String propName) method to load the theme.properties for the template.

          This method internally calls readProperty(String propName)method to load the properties file from file system /class path but not from application path.

          private Properties loadProperties(String propName) {
          InputStream is = readProperty(propName);
          Properties props = new Properties();
          if (is != null)

          { tryToLoadPropertiesFromStream(props, propName, is); }

          return props;
          }

          private InputStream readProperty(String propName) {
          InputStream is = tryReadingPropertyFileFromFileSystem(propName);
          if (is == null)

          { is = readPropertyFromClasspath(propName); }

          return is;
          }

          Value of propName in my case ="WEB-INF/template/xhtml/theme.properties"

          Please create unit test case for the loadProperties("WEB-INF/template/xhtml/theme.properties") method to understand the problem more details.

          Show
          Vijayakannan added a comment - Root cause for the above Exception: FreemarkerTemplateEngine.java :- List<Template> templates = templateContext.getTemplate().getPossibleTemplates(this); The above method call BaseTemplateEngine.java loadProperties(String propName) method to load the theme.properties for the template. This method internally calls readProperty(String propName)method to load the properties file from file system /class path but not from application path. private Properties loadProperties(String propName) { InputStream is = readProperty(propName); Properties props = new Properties(); if (is != null) { tryToLoadPropertiesFromStream(props, propName, is); } return props; } private InputStream readProperty(String propName) { InputStream is = tryReadingPropertyFileFromFileSystem(propName); if (is == null) { is = readPropertyFromClasspath(propName); } return is; } Value of propName in my case ="WEB-INF/template/xhtml/theme.properties" Please create unit test case for the loadProperties("WEB-INF/template/xhtml/theme.properties") method to understand the problem more details.
          Lukasz Lenart made changes -
          Field Original Value New Value
          Fix Version/s 2.3.x [ 12319176 ]
          Fix Version/s 2.5.x [ 12319198 ]
          Hide
          Vijayakannan added a comment -

          Hope you have understand the above mentioned issue.

          Issue # 2 in the ticket

          struts.xml
          <constant name="struts.ui.templateDir" value="struts-template"></constant>

          web.xml

          <context-param>
          <param-name>TemplatePath</param-name>
          <param-value>file://D://templates</param-value>
          </context-param>

          The purpose of TemplatePath is more powerful when compare to others, since it helps us to load the template from file system.

          when i try to load the template from file system, i am getting FileNotException.

          java.io.FileNotFoundException: Template /struts-template/xhtml/a.ftl not found.
          at freemarker.template.Configuration.getTemplate(Configuration.java:489)
          at freemarker.template.Configuration.getTemplate(Configuration.java:452)
          at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:96)

          Root Cause:

          BaseTemplateEngine.java loadProperties(String propName) method doesn't include TemplatePath which is defined in web.xml.

          private InputStream readProperty(String propName) {
          InputStream is = tryReadingPropertyFileFromFileSystem(propName);
          if (is == null)

          { is = readPropertyFromClasspath(propName); }

          return is;
          }

          In my case , propName is "struts-template/xhtml/theme.properties"

          Please note i have raise two related problem in this ticket.

          Thanks in advance

          Show
          Vijayakannan added a comment - Hope you have understand the above mentioned issue. Issue # 2 in the ticket struts.xml <constant name="struts.ui.templateDir" value="struts-template"></constant> web.xml <context-param> <param-name>TemplatePath</param-name> <param-value> file://D://templates </param-value> </context-param> The purpose of TemplatePath is more powerful when compare to others, since it helps us to load the template from file system. when i try to load the template from file system, i am getting FileNotException. java.io.FileNotFoundException: Template /struts-template/xhtml/a.ftl not found. at freemarker.template.Configuration.getTemplate(Configuration.java:489) at freemarker.template.Configuration.getTemplate(Configuration.java:452) at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:96) Root Cause: BaseTemplateEngine.java loadProperties(String propName) method doesn't include TemplatePath which is defined in web.xml. private InputStream readProperty(String propName) { InputStream is = tryReadingPropertyFileFromFileSystem(propName); if (is == null) { is = readPropertyFromClasspath(propName); } return is; } In my case , propName is "struts-template/xhtml/theme.properties" Please note i have raise two related problem in this ticket. Thanks in advance
          Lukasz Lenart made changes -
          Assignee Lukasz Lenart [ lukaszlenart ]
          Hide
          Vijayakannan added a comment -

          Lukasz Lenart Any updates ?

          Show
          Vijayakannan added a comment - Lukasz Lenart Any updates ?
          Lukasz Lenart made changes -
          Fix Version/s 2.3.4 [ 12320768 ]
          Fix Version/s 2.3.x [ 12319176 ]
          Vijayakannan made changes -
          Issue Type Temp [ 12 ] Improvement [ 4 ]
          Hide
          Lukasz Lenart added a comment -

          No, I'll postpone it till 2.3.5

          Show
          Lukasz Lenart added a comment - No, I'll postpone it till 2.3.5
          Lukasz Lenart made changes -
          Fix Version/s 2.3.5 [ 12321274 ]
          Fix Version/s 2.3.4 [ 12320768 ]
          Hide
          Lukasz Lenart added a comment -

          Could you prepare a small example application as I cannot grasp where the problem is :/

          Show
          Lukasz Lenart added a comment - Could you prepare a small example application as I cannot grasp where the problem is :/
          Lukasz Lenart made changes -
          Fix Version/s 2.3.6 [ 12323249 ]
          Fix Version/s 2.3.5 [ 12321274 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.7 [ 12323448 ]
          Fix Version/s 2.3.6 [ 12323249 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.8 [ 12323480 ]
          Fix Version/s 2.3.7 [ 12323448 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.9 [ 12323841 ]
          Fix Version/s 2.3.8 [ 12323480 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.10 [ 12323903 ]
          Fix Version/s 2.3.9 [ 12323841 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.12 [ 12324067 ]
          Fix Version/s 2.3.11 [ 12323903 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.13 [ 12324132 ]
          Fix Version/s 2.3.12 [ 12324067 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.14 [ 12324256 ]
          Fix Version/s 2.3.13 [ 12324132 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.15 [ 12324267 ]
          Fix Version/s 2.3.14 [ 12324256 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.16 [ 12324546 ]
          Fix Version/s 2.3.15 [ 12324267 ]
          Lukasz Lenart made changes -
          Link This issue is related to WW-4145 [ WW-4145 ]
          Hide
          Lukasz Lenart added a comment -

          When WW-4145 will be solved we can start working on this

          Show
          Lukasz Lenart added a comment - When WW-4145 will be solved we can start working on this
          Lukasz Lenart made changes -
          Fix Version/s 2.3.17 [ 12324780 ]
          Fix Version/s 2.3.16 [ 12324546 ]
          Lukasz Lenart made changes -
          Fix Version/s 2.3.x [ 12319176 ]
          Fix Version/s 2.3.17 [ 12324780 ]
          Lukasz Lenart made changes -
          Description There is an option in Struts2 to load the freemarker template from path by setting in web.xml (templatePath).Templates are loaded from the file system if we configure the file path.

          Also there is an option to extend the themes by setting the value for "parent" key in the themes.properties within the template folder

          readProperty() method helps us to read the themes.properties file from the file system and also from the classpath
          Aruguments :-
          Combination of template directory ,template and file name "theme.properties" are passed as propName

          This method doesn't help us to load the theme.properties from the specified templatePath

          private InputStream readProperty(String propName) {
                  InputStream is = tryReadingPropertyFileFromFileSystem(propName);
                  if (is == null) {
                      is = readPropertyFromClasspath(propName);
                  }
                  return is;
              }

          There is an option in Struts2 to load the freemarker template from path by setting in web.xml (templatePath).Templates are loaded from the file system if we configure the file path.

          Also there is an option to extend the themes by setting the value for "parent" key in the themes.properties within the template folder

          readProperty() method helps us to read the themes.properties file from the file system and also from the classpath
          Aruguments :-
          Combination of template directory ,template and file name "theme.properties" are passed as propName

          This method doesn't help us to load the theme.properties from the specified templatePath

          {code:java}
          private InputStream readProperty(String propName) {
                  InputStream is = tryReadingPropertyFileFromFileSystem(propName);
                  if (is == null) {
                      is = readPropertyFromClasspath(propName);
                  }
                  return is;
              }
          {code}
          Hide
          Lukasz Lenart added a comment -

          Right now theme.properties can be loaded from classpath or from web context, as far I understand this issue is solved, you don't have to specify templatePath if you want load templates from within WEB-INF

          Show
          Lukasz Lenart added a comment - Right now theme.properties can be loaded from classpath or from web context, as far I understand this issue is solved, you don't have to specify templatePath if you want load templates from within WEB-INF
          Hide
          Vijayakannan added a comment -

          let me try it out and update you about this issue. Thanks

          Show
          Vijayakannan added a comment - let me try it out and update you about this issue. Thanks
          Hide
          Vijayakannan added a comment -

          Issue is not yet resolved. We will focus first issue now.
          Steps to reproduce

          1. Configure the struts2-blank.war & execute it and make sure that the application is working fine
          2. Add the below lines in Struts.xml. This will help us to load the template file from application Path instead of loading it from struts2-core JAR. We will face the performance issue if it’s loaded from it. Refer WW-3765
          <constant name="struts.ui.templateDir" value="WEB-INF/template"/>
          3. Extract the struts2-core Jar and copy the “template” folder and place it in WEB-INF folder
          4. Execute the Project and we will get the FileNotFoundException

          Please inform me if you are not understand the issue clearly

          Show
          Vijayakannan added a comment - Issue is not yet resolved. We will focus first issue now. Steps to reproduce 1. Configure the struts2-blank.war & execute it and make sure that the application is working fine 2. Add the below lines in Struts.xml. This will help us to load the template file from application Path instead of loading it from struts2-core JAR. We will face the performance issue if it’s loaded from it. Refer WW-3765 <constant name="struts.ui.templateDir" value="WEB-INF/template"/> 3. Extract the struts2-core Jar and copy the “template” folder and place it in WEB-INF folder 4. Execute the Project and we will get the FileNotFoundException Please inform me if you are not understand the issue clearly
          Hide
          Vijayakannan added a comment -

          Attached the Sample Project WAR

          Show
          Vijayakannan added a comment - Attached the Sample Project WAR
          Vijayakannan made changes -
          Attachment struts2-blank.war [ 12654739 ]
          Hide
          Lukasz Lenart added a comment -

          There was a slight miscommunication here - I meant it is solved as an issue, see WW-4260, but it will be included in incoming 2.3.18 version, 2.3.16.x is still affected. Can you test with the latest snapshot version?

          Show
          Lukasz Lenart added a comment - There was a slight miscommunication here - I meant it is solved as an issue, see WW-4260 , but it will be included in incoming 2.3.18 version, 2.3.16.x is still affected. Can you test with the latest snapshot version?
          Hide
          Vijayakannan added a comment -

          can i have the URL to download the latest snapshot version?

          Show
          Vijayakannan added a comment - can i have the URL to download the latest snapshot version?
          Show
          Lukasz Lenart added a comment - https://repository.apache.org/content/groups/public/org/apache/struts/struts2-assembly/2.3.18-SNAPSHOT/
          Hide
          Vijayakannan added a comment -

          Still java.io.FileNotFoundException

          2014-07-09 19:09:13,965 DEBUG (org.apache.struts2.components.UIBean:72) - Rendering template /WEB-INF/template/xhtml/a-close
          2014-07-09 19:09:13,966 ERROR (org.apache.struts2.components.template.FreemarkerTemplateEngine:34) - Could not load the FreeMarker template named 'a-close':
          2014-07-09 19:09:13,966 ERROR (org.apache.struts2.components.template.FreemarkerTemplateEngine:34) - Attempted: /WEB-INF/template/xhtml/a-close.ftl
          2014-07-09 19:09:13,967 ERROR (org.apache.struts2.components.template.FreemarkerTemplateEngine:34) - The TemplateLoader provided by the FreeMarker Configuration was a: org.apache.struts2.views.freemarker.FreemarkerThemeTemplateLoader
          2014-07-09 19:09:13,967 ERROR (org.apache.struts2.components.Anchor:38) - error when rendering
          java.io.FileNotFoundException: Template /WEB-INF/template/xhtml/a-close.ftl not found.
          at freemarker.template.Configuration.getTemplate(Configuration.java:580)

          Show
          Vijayakannan added a comment - Still java.io.FileNotFoundException 2014-07-09 19:09:13,965 DEBUG (org.apache.struts2.components.UIBean:72) - Rendering template /WEB-INF/template/xhtml/a-close 2014-07-09 19:09:13,966 ERROR (org.apache.struts2.components.template.FreemarkerTemplateEngine:34) - Could not load the FreeMarker template named 'a-close': 2014-07-09 19:09:13,966 ERROR (org.apache.struts2.components.template.FreemarkerTemplateEngine:34) - Attempted: /WEB-INF/template/xhtml/a-close.ftl 2014-07-09 19:09:13,967 ERROR (org.apache.struts2.components.template.FreemarkerTemplateEngine:34) - The TemplateLoader provided by the FreeMarker Configuration was a: org.apache.struts2.views.freemarker.FreemarkerThemeTemplateLoader 2014-07-09 19:09:13,967 ERROR (org.apache.struts2.components.Anchor:38) - error when rendering java.io.FileNotFoundException: Template /WEB-INF/template/xhtml/a-close.ftl not found. at freemarker.template.Configuration.getTemplate(Configuration.java:580)
          Vijayakannan made changes -
          Comment [ Let me put the question in other way.

          I like to load default struts freemaker template from /WEB-INF. Do we have option for it?
          ]
          Hide
          Lukasz Lenart added a comment -

          Your deleted comment was valid - let me think about that, I must check few things - it isn't about specifying the exact path but rather a prefix /WEB-INF

          Show
          Lukasz Lenart added a comment - Your deleted comment was valid - let me think about that, I must check few things - it isn't about specifying the exact path but rather a prefix /WEB-INF
          Lukasz Lenart made changes -
          Link This issue relates to WW-1832 [ WW-1832 ]
          Hide
          Lukasz Lenart added a comment -

          It works! Just copied all the templates under WEB-INF/template and set <constant name="struts.ui.templateDir" value="WEB-INF/template"/> - notice missing leading /

          Show
          Lukasz Lenart added a comment - It works! Just copied all the templates under WEB-INF/template and set <constant name="struts.ui.templateDir" value="WEB-INF/template"/> - notice missing leading /
          Hide
          Lukasz Lenart added a comment -

          Just one remark - plugins that provides templates under template won't work anymore, you must copy its templates as well

          Show
          Lukasz Lenart added a comment - Just one remark - plugins that provides templates under template won't work anymore, you must copy its templates as well
          Hide
          Lukasz Lenart added a comment -
          Show
          Lukasz Lenart added a comment - You can check the source of BaseTemplateEngine it now supports loading properties based on SevletContext https://github.com/apache/struts/blob/aa744b811f9c41b80cc30ad6cf41ccaa75da5323/core/src/main/java/org/apache/struts2/components/template/BaseTemplateEngine.java#L85
          Hide
          Lukasz Lenart added a comment -
          Show
          Lukasz Lenart added a comment - Small demo to show how to do it https://github.com/apache/struts-examples/tree/develop/themes_override
          Lukasz Lenart made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Hide
          Vijayakannan added a comment -

          Yes, It works
          Hope last time i didn't use the 2.3.18 Jar(Somehow i missed it)

          Refer Log below

          2014-07-10 21:46:48,095 DEBUG (org.apache.struts2.components.UIBean:72) - Rendering template /WEB-INF/template/xhtml/a-close
          2014-07-10 21:46:48,104 DEBUG (org.apache.struts2.components.template.FreemarkerTemplateEngine:72) - Rendering template /WEB-INF/template/simple/a-close.ftl

          Show
          Vijayakannan added a comment - Yes, It works Hope last time i didn't use the 2.3.18 Jar(Somehow i missed it) Refer Log below 2014-07-10 21:46:48,095 DEBUG (org.apache.struts2.components.UIBean:72) - Rendering template /WEB-INF/template/xhtml/a-close 2014-07-10 21:46:48,104 DEBUG (org.apache.struts2.components.template.FreemarkerTemplateEngine:72) - Rendering template /WEB-INF/template/simple/a-close.ftl
          Hide
          Lukasz Lenart added a comment -

          2.3.18 isn't released yet, working on that

          Show
          Lukasz Lenart added a comment - 2.3.18 isn't released yet, working on that
          Lukasz Lenart made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          868d 15h 7m 1 Lukasz Lenart 10/Jul/14 08:43
          Resolved Resolved Closed Closed
          187d 4h 15m 1 Lukasz Lenart 13/Jan/15 11:58

            People

            • Assignee:
              Lukasz Lenart
              Reporter:
              Vijayakannan
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 24h
                24h
                Remaining:
                Remaining Estimate - 24h
                24h
                Logged:
                Time Spent - Not Specified
                Not Specified

                  Development