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

  • Dispatcher mapping problem

    Hi there,

    I wonder if any one has tried successfully mapping "/*" with spring's DispatcherServlet?

    Basically I want the following to work:
    Code:
    <servlet>
    		<servlet-name>webapp</servlet-name>
    		<servlet-class>
    			org.springframework.web.servlet.DispatcherServlet
    		</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>webapp</servlet-name>
    		<url-pattern>/*</url-pattern>
    	</servlet-mapping>
    I tried many configuration, and got those handler mapping to controller executed, but the ViewResolver don't seem able to resolve url correctly. I can see log reporting url can't be found that include the contextpath in front. Anyone has issue with this that can give me some pointer?

    Thanks,
    TBS.

  • #2
    Hello out there? So no one has experienced what I posted here? been few days and see no replies yet. I am still have no solution.

    Comment


    • #3
      Hi, Can some one please give some advice with this problem?
      Much appreciated.
      -Z

      Comment


      • #4
        We use it without any problem.

        Also if you want an answer including a bit more information (actuall error message, stacktraces, configuration etc.).

        Comment


        • #5
          mdeinum, Thks for taking interest.

          Please note that I am familiar with SpringMVC in general. I have created webapp with urls mapping/setup like these without any problems:
          Code:
          http://hostname/webappname/springdispathername/<mypath>
          http://hostname/webappname/<mypath>.dispatcher
          It's just bother me that I can't get mapping style working like following:
          Code:
          http://hostname/webappname/<mypath>
          When I run a webapp (eg: http://localhost:8080/myspringmvc/index) that has this config, I get a HTTP Status 404 page with the following logging:
          Code:
          09:52:25 INFO  org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/myspringmvc][log] - Initializing Spring FrameworkServlet 'webapp'
          09:52:25 INFO  org.springframework.web.servlet.DispatcherServlet[initServletBean] - FrameworkServlet 'webapp': initialization started
          09:52:25 INFO  org.springframework.web.context.support.XmlWebApplicationContext[prepareRefresh] - Refreshing [email protected]779f5e: display name [WebApplicationContext for namespace 'webapp-servlet']; startup date [Mon Aug 20 09:52:25 EDT 2007]; root of context hierarchy
          09:52:26 INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader[loadBeanDefinitions] - Loading XML bean definitions from ServletContext resource [/WEB-INF/webapp-servlet.xml]
          09:52:26 INFO  org.springframework.web.context.support.XmlWebApplicationContext[obtainFreshBeanFactory] - Bean factory for application context [[email protected]779f5e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bcd14a
          09:52:26 INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory[preInstantiateSingletons] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bcd14a: defining beans [controllerMapping,filenameViewController,internalViewResolver]; root of factory hierarchy
          09:52:26 INFO  org.springframework.web.servlet.DispatcherServlet[initServletBean] - FrameworkServlet 'webapp': initialization completed in 270 ms
          09:52:26 WARN  org.springframework.web.servlet.PageNotFound[noHandlerFound] - No mapping for [/myspringmvc/WEB-INF/view/index.jsp] in DispatcherServlet with name 'webapp'
          Here are more details of the project files:

          WEB-INF/view/index.jsp
          Code:
          <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
              pageEncoding="ISO-8859-1"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          <title>Insert title here</title>
          </head>
          <body>
          This is a demo for SpringMVC.
          <a href="/myapp/demo">/myspringmvc/demo</a>
          </body>
          </html>
          WEB-INF/webapp-servlet.xml
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xmlns:util="http://www.springframework.org/schema/util" xmlns:lang="http://www.springframework.org/schema/lang"
          	xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
          	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
          	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
          	
          	<bean id="controllerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          		<property name="urlMap">
          			<util:map>
          				<entry key="index" value-ref="filenameViewController" />
          				<entry key="demo" value-ref="filenameViewController" />
          			</util:map>
          		</property>
          	</bean>
          	<bean id="filenameViewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController">
          	</bean>
          	<bean id="internalViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
          		<property name="prefix" value="WEB-INF/view/"></property>
          		<property name="suffix" value=".jsp"></property>
          	</bean>
          
          </beans>
          WEB-INF/web.xml
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
          	<display-name>myspringmvc</display-name>
          
          	<servlet>
          		<servlet-name>webapp</servlet-name>
          		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          	</servlet>
          	<servlet-mapping>
          		<servlet-name>webapp</servlet-name>
          		<url-pattern>/*</url-pattern>
          	</servlet-mapping>
          
          </web-app>
          project directory structure
          Code:
          myspringmvc
          myspringmvc/.classpath
          myspringmvc/.project
          myspringmvc/.settings
          myspringmvc/.settings/org.eclipse.jdt.core.prefs
          myspringmvc/.settings/org.eclipse.jst.common.project.facet.core.prefs
          myspringmvc/.settings/org.eclipse.wst.common.component
          myspringmvc/.settings/org.eclipse.wst.common.project.facet.core.xml
          myspringmvc/build
          myspringmvc/build/classes
          myspringmvc/build/classes/log4j.properties
          myspringmvc/pom.xml
          myspringmvc/src
          myspringmvc/src/main
          myspringmvc/src/main/log4j.properties
          myspringmvc/web
          myspringmvc/web/META-INF
          myspringmvc/web/META-INF/MANIFEST.MF
          myspringmvc/web/WEB-INF
          myspringmvc/web/WEB-INF/lib
          myspringmvc/web/WEB-INF/view
          myspringmvc/web/WEB-INF/view/demo.jsp
          myspringmvc/web/WEB-INF/view/index.jsp
          myspringmvc/web/WEB-INF/web.xml
          myspringmvc/web/WEB-INF/webapp-servlet.xml
          So is there configuration that I missed?
          Thanks.

          Comment


          • #6
            re:

            take a look at my recent thread: http://forum.springframework.org/showthread.php?t=42820. well, i changed to the mapping "/*" and started having the same problem as you. my controller mapping is:

            <bean name="/login.htm" class="myapp.web.LoginController">

            using the url http://domain/myapp/login.htm i had these two situations:

            1 - using the mapping "<url-pattern>*.htm</url-pattern>" it worked well and i got the spring log:

            Code:
            ...JstlView] - <Rendering view with name 'login' with model {(...)} and static attributes {}>
            ...view.JstlView] - <Forwarded to resource [WEB-INF/jsp/login.jsp] in InternalResourceView 'login'>
            ...DispatcherServlet] - <Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@149f041>
            ...DispatcherServlet] - <Successfully completed request>
            2 - when i changed to "<url-pattern>/*</url-pattern>" it did not work and i got the logs:

            Code:
            ...JstlView] - <Rendering view with name 'login' with model {(...)} and static attributes {}>
            ...DispatcherServlet] - <Testing handler map [[email protected]b9dfa] in DispatcherServlet with name 'myapp'>
            ...SimpleUrlHandlerMapping] - <Looking up handler for [/WEB-INF/jsp/login.jsp]>
            ...DispatcherServlet] - <Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@2c5f8a] in DispatcherServlet with name 'myapp'>
            ...BeanNameUrlHandlerMapping] - <Looking up handler for [/WEB-INF/jsp/login.jsp]>
            ...DispatcherServlet] - <No handler found in getLastModified>
            ...DispatcherServlet] - <DispatcherServlet with name 'myapp' received request for [/myapp/WEB-INF/jsp/login.jsp]>
            ...DispatcherServlet] - <Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest@7af3e0>
            ...DispatcherServlet] - <Testing handler map [[email protected]b9dfa] in DispatcherServlet with name 'myapp'>
            ...SimpleUrlHandlerMapping] - <Looking up handler for [/WEB-INF/jsp/login.jsp]>
            ...DispatcherServlet] - <Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@2c5f8a] in DispatcherServlet with name 'myapp'>
            ...BeanNameUrlHandlerMapping] - <Looking up handler for [/WEB-INF/jsp/login.jsp]>
            ...WARN [org.springframework.web.servlet.PageNotFound] - <No mapping for [/myapp/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'myapp'>
            ...DispatcherServlet] - <Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest@7af3e0>
            ...DispatcherServlet] - <Successfully completed request>
            note that after "Rendering view with name 'login'" it tries to locate a mapping for "myapp" and do not find it. so maybe the problem is that the mappings in myapp-servlet.xml are missing something.

            Comment


            • #7
              I think the problem might be that it creates some kind of loop, due to the mapping of /*. I checked our configuration and we use a *.htm notation.

              Can you create a small sample project that I can use for debugging? I will take a closer look.

              Comment


              • #8
                Thanks for give it a shot guys. As I have mentioned earlier, I don't have problem getting it work with a extension mapping. But I am curious as why won't a "/*" mapping won't work.

                Attached is a eclipse project that contains files I presented in this post. It's very simple project that depends only on spring and logging and jstl jars.

                The project is configured with Maven Eclipse Plugin that auto download all jars and it can be install by this update site: http://m2eclipse.codehaus.org/update

                And the project is runable by a Tomcat runtime server inside Eclipse. I am using Tomcat 5.5.

                Please do let me know if anyone of you find a solution.

                Comment


                • #9
                  Hum... attachment doesn't seem to work.

                  See if you guys can download it here
                  http://cl1p.net/myspringmvc

                  Or email at
                  thebugslayer at gmail dot com.

                  Thanks.
                  Last edited by thebugslayer; Aug 20th, 2007, 04:10 PM.

                  Comment


                  • #10
                    Im not so sure you would find /* mapping to be useful; wouldnt that also push all requests for simple static items such as style sheets and images through the servlet as well? Doesnt seem like it is what you are looking for; I bet you would find extension mapping to be your answer (e.g. *.htm, you can even map *.jsp to the spring dispatcher, and it will override the server's implicit mapping for jsp files if you like).

                    Comment


                    • #11
                      As an afterthought, after looking back at the servlet spec, I see it is possible to define a servlet as the 'default' for the web application, by using the '/' mapping:

                      10.2 Specification of Mappings
                      In the web application deployment descriptor, the following syntax is used to define mappings:
                      A string beginning with a / character and ending with a /* postfix is used as a path
                      mapping.
                      A string beginning with a *. prefix is used as an extension mapping.
                      All other strings are used as exact matches only
                      A string containing only the / character indicates that servlet specified by the mapping becomes the "default" servlet of the application.
                      10.2.1 Implicit Mappings
                      If the container has an internal JSP container, the *.jsp extension is implicitly mapped to it so
                      that JSP pages may be executed on demand. If the web application defines a *.jsp mapping, its
                      mapping takes precedence over this implicit mapping.

                      Comment


                      • #12
                        Yes, it's working! Thanks jstehler!!!

                        By mapping dispatcher to default "/" will do the trick.

                        Also as side note, the config for internalViewResolver's prefix should have set to following (if not, you won't able to nested path correctly.):
                        Code:
                        <property name="prefix" value="/WEB-INF/view/"></property>
                        Thanks for the helps everyone.

                        Comment


                        • #13
                          And jstehler is right, mapping everything to the spring dispatcher will give you problem when retrieving static contents. But for a webapp that only do processing entirely through controllers, this is a good to know trick.

                          Thanks again.
                          -Z

                          Comment


                          • #14
                            Hi..I am a newbie on JSP and Spring.

                            After reading this post. I guess my problem is the other way around. I have a few static images in side the WAR I want to display. Very simple need. The images are in /images/ like /images/image1.jpg. Note: /jsp is in the same level as /images and /WEB-INF.

                            In my jsp, i do <IMG src='<c:url value="/images/image1.jpg"/>'>

                            It is always a broken image in the browser. Here is what I got in the log
                            - getHandlerInternal() |Looking up handler for [/images/image1.jpg]
                            - org.springframework.web.servlet.PageNotFound | noHandlerFound() |No mapping for [/webapp/images/image.jpg] in DispatcherServlet with name 'webapp'

                            Seem like the DispatcherServlet is trying to map the URI to the controller and no mapping is there for the images/image.jpg.

                            My mapping is

                            <servlet-mapping>
                            <servlet-name>butlerwebgateway</servlet-name>
                            <url-pattern>*.htm</url-pattern>
                            </servlet-mapping>

                            How may I display just a static image from inside the war file in a specific directory?

                            Thanks so much

                            Comment


                            • #15
                              jochuan,

                              Since your mapped Spring dispatcher with "*.html" pattern, then requesting images would not go through it, and therefore should just fetch by your WebContainer by direct URL.

                              Example:

                              http://localhost:8080/yourapp/image/image1.jpg

                              fetch image, while:

                              http://localhost:8080/yourapp/image/image1.htm

                              will go through Spring.

                              Comment

                              Working...
                              X