Announcement Announcement Module
Collapse
No announcement yet.
UrlBasedViewResolver Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • UrlBasedViewResolver

    I have a question about the URLBasedViewResolver.

    I have tried creating a resolver in the [appname]-servlet.xml file as below:

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBas edViewResolver">
    <property name="prefix" value="/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    However, when trying to start the server, I am receiving the error:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'viewResolver' defined in ServletContext resource [/WEB-INF/videoshop-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Property 'viewClass' is required

    I have had a quick look on google, and have found a jira stating that the viewClass property has been missed from the example within the Spring MVC tutorial.

    I have also tried looking online for examples of the viewClass property, but cannot find any.

    Can anybody here help, and possibly provide an example please?

    Many thanks for your time.
    Last edited by lcmorley; Jan 3rd, 2010, 05:00 AM.

  • #2
    I have the following:
    Code:
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    InternalResourceViewResolver is a subclass of UrlBasedViewResolver which may do what you want and obviate the need to specify the viewClass.

    If you need to use UrlBasedViewResolver, then try adding a property to your configuration called viewClass with the value of org.springframework.web.servlet.view.JstlView. e.g.:

    Code:
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="prefix" value="/jsp/"/>
      <property name="suffix" value=".jsp"/>
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    </bean>
    N.B. Please use the code tags when posting code or configuration fragments.

    Hope this helps,

    PUK

    Comment


    • #3
      Thanks for the quick response PUK.

      I just chose the UrlBasedViewResolver as it was the first thing I came accross. I am still learning Spring MVC, so any help is appreciated.

      Many thanks again.

      Comment


      • #4
        I'm fairly new too.

        If you're using JSPs as your views, then use the InternalResourceViewResolver and forget UrlBasedViewResolver for now.

        As your JSPs comprise part of your source code, you want to protect them from being served in their raw form (a JSP is processed into HTML output). In my previous post you might notice my JSPs are placed under the WEB-INF directory. Anything under WEB-INF cannot be directly served to clients.

        Are you using URLs with an extension (e.g. htm), or extension-less URLs? In other words, what is your url-pattern in web.xml?

        Also, what version of Spring are you learning with?

        PUK

        Comment


        • #5
          Originally posted by PUK_999 View Post
          Are you using URLs with an extension (e.g. htm), or extension-less URLs? In other words, what is your url-pattern in web.xml?

          Also, what version of Spring are you learning with?
          PUK
          Strange you should ask the question about extensions, the background is that I have produced a test web app using JSP's, AJAX etc, and I am now attempting to port it over to use the Spring MVC architecture.

          I was originally using extensionless url's, but have now started using .htm. Is it possible to still use extensionless url's, if so, I would probably prefer that.

          As regards the AJAX stuff, that is my next obstacle, so may be posting questions about that next.

          The version of Spring is 3.0.0-RELEASE.

          Cheers.

          Comment


          • #6
            Originally posted by lcmorley View Post
            I was originally using extensionless url's, but have now started using .htm. Is it possible to still use extensionless url's, if so, I would probably prefer that.
            I was originally using .htm in my application (just because the Spring tutorial I followed did!) but I moved to extension-less URLs and am so glad I did. /clients/edit/34 looks much better than clientEdit.htm?clientId=34 in my opinion. It also fits in really nicely with Springs annotated controllers which can use URI templates which automatically extracts the 34 to a clientId variable for you!

            The Spring PetClinic example (access using SVN at https://src.springframework.org/svn/...tclinic/trunk/) also uses extension-less URLs so I'd advise you to take a look at that but I'll try to give a very brief explanation here.

            In web.xml you change the mapping from .htm to /app/*:

            Code:
                <servlet-mapping>
                    <servlet-name>dispatcher</servlet-name>
                    <url-pattern>/app/*</url-pattern>
                </servlet-mapping>
            You also include a new filter that allows incoming URLs to be rewritten before they are processed by servlets:

            Code:
                <filter>
                    <filter-name>UrlRewriteFilter</filter-name>
                    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
                </filter>
                <filter-mapping>
                    <filter-name>UrlRewriteFilter</filter-name>
                    <url-pattern>/*</url-pattern>
                </filter-mapping>
            N.B. I use Maven so I have included the following in pom.xml to get the UrlRewriteFilter in my project:

            Code:
                    <dependency>
                        <groupId>org.tuckey</groupId>
                        <artifactId>urlrewritefilter</artifactId>
                        <version>3.1.0</version>
                        <scope>runtime</scope>
                    </dependency>
            The UrlRewriteFilter needs a configuration file to tell it how to manipulate the URLs:

            urlrewrite.xml
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            
            <urlrewrite default-match-type="wildcard">
                <rule>
                    <from>/static/**</from>
                    <to last="true">/static/$1</to>
                </rule>
                <rule>
                    <from>/</from>
                    <to last="true">/app/index</to>
                </rule>
                <rule>
                    <from>/**</from>
                    <to last="true">/app/$1</to>
                </rule>
                <outbound-rule>
                    <from>/app/**</from>
                    <to>/$1</to>
                </outbound-rule>
            </urlrewrite>
            What this is basically saying is that incoming requests for /<something> will be routed to /app/<something>. The exception is for static resources which don't get routed under /app. So, static resources will be served by the container's default servlet which is very efficient.

            As regards the AJAX stuff, that is my next obstacle, so may be posting questions about that next.
            What I know about AJAX can be written on a postage stamp

            PUK

            Comment


            • #7
              I've an addendum to make. The PetClinic link I sent you does use extension-less URLs but doesn't use the UrlRewriteFilter. It simple uses this entry in web.xml:

              Code:
              	<servlet-mapping>
              		<servlet-name>default</servlet-name>
              		<url-pattern>/static/*</url-pattern>
              	</servlet-mapping>
              	<servlet-mapping>
              		<servlet-name>petclinic</servlet-name>
              		<url-pattern>/</url-pattern>
              	</servlet-mapping>
              As I understand it though, there's a question on the portability of it. (See this thread of mine from last month).

              The PetClinic that is created from within Roo (another Spring project that is supposed to make creating/maintaing applications easier) however does use the UrlRewriteFilter!

              I decided to use the filter for my peace of mind regarding portability and I can see that such a filter could be handy in the future.

              PUK

              Comment


              • #8
                Thanks for the very detailed and informative reply PUK.

                I will take a look now.

                Cheers again.

                Comment

                Working...
                X