Results 1 to 3 of 3

Thread: Velocity and Classpath Mega-woes

  1. #1
    Join Date
    Aug 2005
    Location
    San Francisco, CA
    Posts
    43

    Default Velocity and Classpath Mega-woes

    'lo all,

    I'm using velocity to render pages in the web tier using spring under jboss 4.0.1 deployed as a simple war file. Here is the velocity specific app config:

    Code:
      <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="configLocation">
          <value>WEB-INF/velocity.properties</value>
        </property>
      </bean>
      
      <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm"/>
        <!--
        <property name="numberToolAttribute"><value>numbertool</value></property>
        <property name="dateToolAttribute"><value>datetool</value></property>
        -->
      </bean>
    Here is velocity.properties (located under WEB-INF):

    Code:
    # Any property that is not listed here will have it's default
    # value used.  The default values are located in &#58;
    #  *  src/java/org/apache/velocity/runtime/default/velocity.defaults
    #  *  http&#58;//jakarta.apache.org/velocity/developer-guide.html
    #
    
    runtime.log = ./velocity.log
    runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem
    
    resource.loader=file
    
    #class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
    #class.resource.loader.cache=false
    #class.resource.loader.modificationCheckInterval=2
    #class.resource.loader.path=/WEB-INF/views
    
    file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
    file.resource.loader.path=/WEB-INF/views
    file.resource.loader.cache=true
    file.resource.loader.modificationCheckInterval=2
    Here is the error:

    Code:
    org.springframework.context.ApplicationContextException&#58; Cannot find Velocity template for URL &#91;admin-home.vm&#93;&#58; Did you specify the correct resource loader path?; nested exception is org.apache.velocity.exception.ResourceNotFoundException&#58; Unable to find resource 'admin-home.vm'
    org.apache.velocity.exception.ResourceNotFoundException&#58; Unable to find resource 'admin-home.vm'
    Here is the controller that is trying to invoke the velocity view:

    Code:
    public class AdminHomeController extends AbstractController &#123;
    
      private AccountUserDao accountUserDao = null;
      
      public void setAccountUserDao&#40;AccountUserDao accountUserDao&#41; &#123;
        this.accountUserDao = accountUserDao;
      &#125;
      
      protected ModelAndView handleRequestInternal&#40;HttpServletRequest request, HttpServletResponse response&#41; throws Exception &#123;
        
        long userId = RequestUtils.getRequiredLongParameter&#40;request, "userId"&#41;;
        
        // debug
        File f = new File&#40;"."&#41;;
        logger.debug&#40;"current dir absolute path &#40;'.'&#41;&#58; " + f.getAbsolutePath&#40;&#41;&#41;;
        ClassLoader classLoader = this.getClass&#40;&#41;.getClassLoader&#40;&#41;;
        URL url = classLoader.getResource&#40;"."&#41;;
        logger.debug&#40;"classLoader current dir&#58; " + url.getPath&#40;&#41; &#41;;
        ///
        
        AccountUserVO accountUser = accountUserDao.load&#40;userId&#41;;
        
        return new ModelAndView&#40;"admin-home"&#41;.addObject&#40;"accountUser", accountUser&#41;;
      &#125;
    Here is the logger output for the debug section w/in the controller class above:

    Code:
    22&#58;28&#58;49,414 DEBUG &#91;AdminHomeController&#93; current dir absolute path &#40;'.'&#41;&#58; C&#58;\development\jboss-4.0.1\bin\.
    22&#58;28&#58;50,926 DEBUG &#91;AdminHomeController&#93; classLoader current dir&#58; /C&#58;/development/jboss-4.0.1/server/default/tmp/deploy/tmp933smbiz-exp.war/
    Does the war need some additional bit of info to tell JBoss what the "current directory" should be?

    [/code]

  2. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    You can't rely on file system resources within a web application. I would recommend putting your templates in the classes/ directory, perhaps in another folder called templates, and using the ClassPathResourceLoader.

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  3. #3
    Join Date
    May 2007
    Posts
    1

    Talking Thanks so much!!!

    I googled for this problem and found this answer.

    Now the *.vm files are put into the WEB-INF/classes folder and the web application works fine!!!!

    Thanks again!

Similar Threads

  1. Velocity and Springframework
    By Mo in forum Web Flow
    Replies: 0
    Last Post: Jun 13th, 2005, 12:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •