Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4147

Have StrutsPrepareAndExecuteFilter initialize Freemarker rather than first actual Request

    XMLWordPrintableJSON

Details

    Description

      Currently, the first request to the web app triggers getting the Freemarker Configuration, which causes it to be built for the first time.

      Depending on circumstances, this might be a bit slow. It would be nice if when struts was coming up, it forced Freemarker initialization before it the server was declared ready to accept requests.

      The way I hacked around this was to extend StrutsPrepareAndExecuteFilter so that in init(FilterConfig), I get the current FreemarkerManager, and call .getConfiguration(filterConfig.getServletContext()) on it.

      As an aside, it was surprisingly difficult to get the Container to use to inject the FreemarkerManager instance in StrutsPrepareAndExecuteFilter. I'm guessing I'm just missing something.

      I recognize not all struts apps are using freemarker, which is probably why it inits lazily, but it would be great to have an easy hook, and StrutsPrepareAndExecuteFilter is the obvious place since you have access to the ServletContext.

          private FreemarkerManager freemarkerManager;
      
          /**
           * Override to initialize freemarker hooks proactively.
           */
          @Override
          public void init(final FilterConfig filterConfig) throws ServletException {
              super.init(filterConfig);
      
              // Inject the freemarker manager
              prepare.assignDispatcherToThread();
              try {
                  Dispatcher.getInstance().getContainer().inject(this);
              } finally {
                  Dispatcher.setInstance(null);
              }
              
              // Force initialization of the freemarker configuration
              freemarkerManager.getConfiguration(filterConfig.getServletContext());
          }
      
          @Inject
          public void setFreemarkerManager(FreemarkerManager freemarkerManager) {
              this.freemarkerManager = freemarkerManager;
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            perfnorm Jasper Rosenberg
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: