Announcement Announcement Module
Collapse
No announcement yet.
Velocity and jsp together in spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Velocity and jsp together in spring

    Hi,
    I came across a problem while using velocity and jsp together with spring.
    I want to use both .jsp and .vm files for view.

    I am copying the confguration in xx-servlet.xml here.

    <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.veloci ty.VelocityConfigurer">
    <property name="resourceLoaderPath" value="/WEB-INF/velocity/"/>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>


    <bean id="velocityResolver" class="org.springframework.web.servlet.view.veloci ty.VelocityViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".vm"/>
    </bean>


    what i need is index.htm should open index.jsp in /web-inf/jsp/index.jsp .In that jsp I am giving one submit button which should open index.vm from web-inf/index.vm.

    the problem is index.htm always refers to /web-inf/velocity/index.vm.

    could anyone please tell me where am wrong? cant we use 2 kinds of viewresolvers at a time?

    thanks
    Nidhi

  • #2
    You can use 2 viewresolvers at a time however NOT these 2 ViewResolvers. Both ViewResolvers are UrlBasedViewResolvers and will generate an url for a view always, even if the view doesn't exist. So no chaining will occur. So it is going to either use always the VelocityViewResolver or the InternalResourceViewResolver.

    A work around could be using the XmlViewResolver or the ResourceBundleViewResolver. With those you can specify the type, url etc. for each view. So mixing view technologies should work with that.

    Comment


    • #3
      thanks..it works.

      i have one another issue..

      in controller class am extending SimpleFormController and overriding onsubmit method and return stmt is like this.

      return new ModelAndView("test","ClientName","TestClient")

      in velocity template I gave Hello $ClientName .

      Its supposed to print value as " Hello TestClient" . Bt its not rendering the value.

      Please help me

      thanks
      Nidhi

      Comment


      • #4
        Code:
        Map model = errors.getModel();
        model.put("ClientName", "TestClient");
        return new ModelAndView("test", model);

        Comment


        • #5
          hi
          It didnt work out..I am getting the variable as such in template.
          In jsp i am able to get the values.
          Could you pls send me a sample appln for Velocity configuration in spring.


          thanks
          Nidhi

          Comment


          • #6
            sample application using velocity as view in spring application

            hi
            is there is link for sample application available on net using velocity as view in spring application.

            so that will helpful to start our project

            i need ur help
            thanks
            rashmi

            Comment


            • #7
              I am not sure whether examples as a whole is avaibale or not.
              If u r specific abt ur doubts, i can definitely help u.

              Nidhi

              Comment


              • #8
                hi nidhi,

                thanks for ur reply ,
                i developed a application but i needed ur valuable guidence
                it not showing any error but only index page is working
                here is my application structure
                +- WEB-INF
                |
                +- lib
                | ... |
                +- velocity
                | |
                | +- mainTemplate.vm
                | +- secondaryTemplate.vm
                |
                +- velocityspring-servlet.xml
                +- velocityspring-views.xml
                +- velocity.properties

                web.xml is
                Code:
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
                <web-app>          
                             
                             
                    <display-name>Spring velocity Example </display-name>
                    <description>Spring velocity Example application</description>
                    
                    
                    <context-param>
                        <param-name>contextConfigLocation</param-name>
                        <param-value>/WEB-INF/applicationContext.xml</param-value>
                    </context-param>
                    
                    <!--
                    - Loads the root application context of this web app at startup,
                    - by default from "/WEB-INF/applicationContext.xml".
                    - Note that you need to fall back to Spring's ContextLoaderServlet for
                    - J2EE servers that do not follow the Servlet 2.4 initialization order.
                    -
                    - Use WebApplicationContextUtils.getWebApplicationContext(servletContext)
                    - to access it anywhere in the web application, outside of the framework.
                    -->
                    <!--<listener>
                        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                    </listener>
                    -->
                    
                    <listener>
                        <listener-class>org.springframework.web.context.ContextLoaderServlet</listener-class>
                    </listener>
                    
                    <servlet>
                    <servlet-name>applicationContext</servlet-name>
                    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
                    <load-on-startup>1</load-on-startup>
                    </servlet>
                
                    
                    <servlet>
                        <servlet-name>velocityspring</servlet-name>
                        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                        <load-on-startup>1</load-on-startup>
                        </servlet>
                    <servlet-mapping>
                        <servlet-name>velocityspring</servlet-name>
                        <url-pattern>*.htm</url-pattern>
                    </servlet-mapping>
                    <session-config>
                        <session-timeout>
                            30
                        </session-timeout>
                    </session-config>
                    <welcome-file-list>
                	<welcome-file>
                            index.jsp
                        </welcome-file>
                    </welcome-file-list>
                    
                     <taglib>
                        <taglib-uri>http://displaytag.sf.net</taglib-uri>
                        <taglib-location>/WEB-INF/tld/displaytag-el-12.tld</taglib-location>
                    </taglib>
                   
                    </web-app>
                velocityspring-views.xml
                Code:
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                
                <beans>
                
                
                <!--
                  Views can be hierarchical, here's an example of a parent view that 
                  simply defines the class to use and sets a default template which
                  will normally be overridden in child definitions.
                -->
                <bean id="parentVelocityView" class="org.springframework.web.servlet.view.velocity.VelocityView">
                    <property name="templateName"><value>mainTemplate.vm</value></property>        
                </bean>
                
                <!--
                  - The main view for the home page.  Since we don't set a template name, the value
                  from the parent is used.
                -->
                <bean id="welcomeView" parent="parentVelocityView">
                    <property name="attributes">
                        <props>
                            <prop key="title">My Velocity Home Page</prop>
                        </props>
                    </property>     
                </bean>  
                
                <!--
                  - Another view - this one defines a different velocity template.
                -->
                <bean id="secondaryView" parent="parentVelocityView">
                    <property name="templateName"><value>secondaryTemplate.vm</value></property>  
                    <property name="attributes">
                            <props>
                                <prop key="title">My Velocity Secondary Page</prop>
                            </props>
                    </property>    
                </bean>  
                
                </beans>


                velocityspring-servlet.xml

                Code:
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                <beans>
                    
                    
                    
                    
                  
                    
                   
                    <bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
                    <property name="cache"><value>true</value></property>
                    <property name="location"><value>/WEB-INF/velocityspring-views.xml</value></property>
                </bean>
                
                
                <bean 
                    id="velocityConfig" 
                    class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"
                    singleton="true">
                    <property name="configLocation"><value>/WEB-INF/velocity.properties</value></property>          
                </bean>
                
                    
                   
                     
                 </beans>
                velocity.properties

                Code:
                #
                # velocity.properties - example configuration
                #
                
                
                # uncomment the next two lines to load templates from the 
                # classpath (WEB-INF/classes)
                #resource.loader=class
                #class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                
                # comment the next two lines to stop loading templates from the
                # file system
                resource.loader=file
                file.resource.loader.class=org.apache.velocity.runtime.resource.loader.FileResourceLoader
                
                
                # additional config for file system loader only.. tell Velocity where the root
                # directory is for template loading.  You can define multiple root directories
                # if you wish, I just use the one here.  See the text below for a note about
                # the ${webapp.root}
                file.resource.loader.path=${webapp.root}/WEB-INF/velocity
                
                
                # caching should be on in production, the following is a development
                # setting only.  Change to 'class.resource.loader.cache=false' for classpath
                # loading
                file.resource.loader.cache=false
                
                # override default logging to direct velocity messages
                # to our application log for example.  Assumes you have 
                # defined a log4j.properties file
                runtime.log.logsystem.log4j.category=com.mycompany.myapplication
                applicationContext.xml

                Code:
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                
                <beans>
                    
                  
                    
                </beans>
                secondaryTemplate.vm

                Code:
                ## $title is set in the view definition file for this view.
                <html>
                    <head><title>$title</title></head>
                    <body>
                        <h1>This is $title!!</h1>
                    </body>
                </html>
                plz guide me wher i did mistake
                i m very new to using velocity in spring but i have to make simple demo application
                in if user enter his/her name in second page i have to show welcome $user
                can u give example how to write a controller for that or i can write controller like in any other spring MVC application and what r the lib we must have to set for running the application
                i m using velocity-1.5.jar and velocity-dep-1.5.jar

                Comment


                • #9
                  hi

                  plz help me to come out frm my problem


                  i m getting error

                  Code:
                  root cause 
                  
                  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parentVelocityView' defined in ServletContext resource [/WEB-INF/velocityspring-views.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'templateName' of bean class [org.springframework.web.servlet.view.velocity.VelocityView]: Bean property 'templateName' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
                  	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1066)
                  	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857)
                  	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378)
                  	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
                  	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
                  	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283)
                  	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
                  	org.springframework.web.servlet.view.XmlViewResolver.initFactory(XmlViewResolver.java:138)
                  	org.springframework.web.servlet.view.XmlViewResolver.initApplicationContext(XmlViewResolver.java:89)
                  	org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
                  	org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:84)
                  	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:277)
                  	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:395)
                  	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
                  	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
                  	org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283)
                  	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
                  	org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
                  	org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:306)
                  	org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:251)
                  	org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:220)
                  	org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:112)
                  	javax.servlet.GenericServlet.init(GenericServlet.java:211)
                  	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                  	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                  	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                  	org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                  	org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                  	org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
                  	org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
                  	java.lang.Thread.run(Thread.java:595)
                  i change my index.jsp

                  Code:
                  <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
                  <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
                  <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
                  
                  <%-- Redirected because we can't set the welcome page to a virtual URL. --%>
                  <c:redirect url="/login.htm"/>
                  and my velocityspring-servlet.xml is

                  Code:
                  <?xml version="1.0" encoding="UTF-8"?>
                  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
                  <beans>
                      
                       
                      <bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
                      <property name="cache"><value>true</value></property>
                      <property name="location"><value>/WEB-INF/velocityspring-views.xml</value></property>
                      </bean>
                  
                  
                  <bean 
                      id="velocityConfig" 
                      class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"
                      singleton="true">
                      <property name="configLocation"><value>/WEB-INF/velocity.properties</value></property>          
                  </bean>
                  
                  
                  <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                          <property name="mappings">
                              <props>
                              <prop key="/login.htm">loginController</prop>
                              <prop key="/checkpassword.htm">checkController</prop>
                              <prop key="/incorrectpassword.htm">incorrectpasswordController</prop>
                              
                              </props>
                          </property>
                      </bean>
                      
                  
                   <bean id="loginController" class="sp.LoginController"/>
                       <bean id="incorrectpasswordController" class="sp.incorrectpasswordController" />
                      
                       <bean id="newController" class="sp.newController"/>
                       
                       <bean id="checkController" class="sp.CheckController"/>
                          
                     
                       
                   </beans>
                  my LoginController.java is

                  Code:
                  /*
                   * LoginController.java
                   *
                   * Created on May 10, 2007, 3:29 PM
                   *
                   * To change this template, choose Tools | Template Manager
                   * and open the template in the editor.
                   */
                  
                  package sp;
                  
                  import javax.servlet.http.HttpServletRequest;
                  import javax.servlet.http.HttpServletResponse;
                  import org.apache.velocity.Template;
                  import org.apache.velocity.app.Velocity;
                  import org.apache.velocity.context.Context;
                  import org.apache.velocity.servlet.VelocityServlet;
                  import org.springframework.web.servlet.ModelAndView;
                  import org.springframework.web.servlet.mvc.Controller;
                  import org.springframework.web.servlet.mvc.AbstractController;
                  /**
                   *
                   * @author Shwetap
                   */
                  public class LoginController extends AbstractController{ 
                      protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
                          return new ModelAndView("secondaryTemplate");
                      }
                      
                  }
                  plz help me to solve my problem i need help

                  Comment

                  Working...
                  X