Results 1 to 8 of 8

Thread: welcome-file-list not being fully honoured

  1. #1

    Default welcome-file-list not being fully honoured

    Got a tricky problem that has had me stumped for a while.
    The welcome-file-list section seems to by pass the springs DispatcherServlet so that all the variables in my JSP are blank.

    In my web.xml I have:
    Code:
    	<display-name>sm</display-name>
      
     	<!--
    	Beans in these files will makeup the configuration of the root web application context
    	Infrastructure beans: datasources, real JMS, etc.
    	-->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/applicationContext.xml</param-value>
    	</context-param>
    
    	<!--
    	Bootstraps the root web application context before servlet initialization
    	-->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    
    	<!--
    	Deploys the 'sm' dispatcher servlet whose configuration.
    	Business beans that make up the application 
    	-->
    	<servlet>
    		<servlet-name>sm</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<!--
    	Page matcher
    	-->
    
    	<servlet-mapping>
    		<servlet-name>sm</servlet-name>
    		<url-pattern>*.html</url-pattern>
    	</servlet-mapping>
    
    	<servlet-mapping>
    		<servlet-name>sm</servlet-name>
    		<url-pattern>*.xml</url-pattern>
    	</servlet-mapping>
    
    	<!--
    	Filters
    	-->
    	
    	<filter>
    		<filter-name>openSessionInViewFilter</filter-name>
    		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    	</filter>
     
    	<filter-mapping>
    		<filter-name>openSessionInViewFilter</filter-name>
    		<url-pattern>*.html</url-pattern>
    	</filter-mapping>
    
    	<filter-mapping>
    		<filter-name>openSessionInViewFilter</filter-name>
    		<url-pattern>*.xml</url-pattern>
    	</filter-mapping>
    
    	<!--
    	Tab libs
    	-->
    	<jsp-config>
    		<taglib>
    			<taglib-uri>/spring</taglib-uri>
    			<taglib-location>/WEB-INF/spring.tld</taglib-location>
    		</taglib>
    	</jsp-config>
    
    	<!--
    	Options
    	-->
    	
    	<welcome-file-list>
    		<welcome-file>/WEB-INF/views/index.jsp</welcome-file>
    	</welcome-file-list>
    
    </web-app>
    When I enter my default url http://localhost:8080/sm/ all the ${var}s are blank. If I enter the full path explicitly http://localhost:8080/sm/index.html then everything works.

    Does anyone have any ideas? Is this the way to do it or should I create a static index.html that re-directs the user to the actual required index page.

    Regards,
    David.

  2. #2

    Default

    All the research I have done on the internet says that you should make the welcome-file point to a file that contains a redirection to the actual page you want. Seems like a waste to me!

    My solution was to place an empty index.html file in the document root. I think Tomcat is looking for a real/physical file to satisfy the welcome-file directive. If there is no file the request fails but if there is an index.html (even an empty one) Tomcat sees it and so lets the request through. Once through it is picked up by the dispatcher and sent to the controller.

    If anyone knows a better way please let me know.

  3. #3

    Default

    This (in part) confirms what I have found out but when I try to add their solution there is a clash between using Spring as DispatcherServlet for the webapp and having an index page.

    http://wiki.metawerx.net/wiki/HowToU...ourMainWebPage

    Perhaps there is a Tomcat guru out there who knows what to do?

  4. #4
    Join Date
    Aug 2005
    Location
    London (the English one!)
    Posts
    378

    Default

    Hi

    Could you confirm whether you have a sm-servlet.xml file or not?

    Your setup seems to fail in my environment, the DispatcherServlet is trying to load sm-servlet which does not exist but the applicationContext.xml was loaded by the ContextLoaderListener.

    How can I avoid to load the same file TWICE and hence creating 2 sets of beans...

    Thanks

    Benoit

  5. #5

    Default

    Apologies for the late reply.

    web.xml
    Code:
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    sm-servlet.xml:
    Code:
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-2.5.xsd
    	">
    
    	<context:component-scan base-package="com.quantel"/>
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    
    	<!-- Resolves logical view names to JSP views -->
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    		<property name="prefix" value="/WEB-INF/views/"/>
    		<property name="suffix" value=".jsp"/>
    	</bean>
    </beans>
    I'm trying to get
    http://localhost:8080/app/
    http://localhost:8080/app/index.html
    to be serviced by the same controller.

  6. #6
    Join Date
    Jul 2008
    Posts
    157

    Default

    It's funny cause all this problem is solved when you use WebFlow and a flow mapping as your welcome file.
    My web.xml:
    Code:
    	<servlet-mapping>
    		<servlet-name>myservelt</servlet-name>
    		<url-pattern>/app/*</url-pattern>
    	</servlet-mapping>
    
    	<welcome-file-list>
    		<!-- Redirects to a custom page for dispatcher handling -->
    		<welcome-file>app/welcome</welcome-file>
    	</welcome-file-list>
    Then if you have a flow whose id is 'welcome' then both http://server/appcontext and http://server/appcontext/app/welcome will be mapped to the same flow.
    That is because FlowHandlerMapping calls FlowUrlHandler.getFlowId(request) which calls request.getPathInfo() returning "/welcome" as the required mapping when the welcome file is used.

    On the other hand, AbstractUrlHandlerMapping (which is the base for most MVC controller handler mappings) calls UrlPathHelper.getLookupPathForRequest(request) where you can find this method:
    Code:
    	/**
    	 * Return the path within the servlet mapping for the given request,
    	 * i.e. the part of the request's URL beyond the part that called the servlet,
    	 * or "" if the whole URL has been used to identify the servlet.
    	 * <p>Detects include request URL if called within a RequestDispatcher include.
    	 * <p>E.g.: servlet mapping = "/test/*"; request URI = "/test/a" -> "/a".
    	 * <p>E.g.: servlet mapping = "/test"; request URI = "/test" -> "".
    	 * <p>E.g.: servlet mapping = "/*.test"; request URI = "/a.test" -> "".
    	 * @param request current HTTP request
    	 * @return the path within the servlet mapping, or ""
    	 */
    	public String getPathWithinServletMapping(HttpServletRequest request) {
    		String pathWithinApp = getPathWithinApplication(request);	// <- This is "/"
    		String servletPath = getServletPath(request);			// <-This is "/app
    		if (pathWithinApp.startsWith(servletPath)) {
    			// Normal case: URI contains servlet path.
    			return pathWithinApp.substring(servletPath.length());
    		}
    		else {
    			// Special case: URI is different from servlet path.
    			// Can happen e.g. with index page: URI="/", servletPath="/index.html"
    			// Use servlet path in this case, as it indicates the actual target path.
    			return servletPath;
    		}
    	}
    And then it will return servletPath ("/app") when the welcome file path should be sending it to "/welcome". I wonder if the solution would simply be to change the method to return request.getPathInfo() instead of servletPath, like this:
    Code:
    		else {
    			// Special case: URI is different from servlet path.
    			// Can happen e.g. with index page: URI="/", servletPath="/index.html"
    			String pathInfo = request.getPathInfo();
    			if (pathInfo != null) {
    				return pathInfo;
    			}
    			return servletPath;
    		}

  7. #7
    Join Date
    Jul 2010
    Posts
    1

    Default Put an Explicit url-pattern in the servlet-mappings

    I have had this problem a few times as well. I found that under tomcat you can put an explicit url-pattern on the dispatcher servlet and it will pick that up rather than having to put the file in.

    I know this is not the best solution but it works well enough (as long as you don't need to do lots of them).

    For instance if you need http://www.example.com/news/ to map to http://www.example.com/news/index.html (/news/index.html) against your context.

    Your welcome file would be:

    Code:
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    And your servlet mapping might look something like:

    Code:
    <servlet>
        <servlet-name>someAppDisp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>someAppDisp</servlet-name>
        <url-pattern>/news/index.html</url-pattern>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    Last edited by Orian71; Jul 31st, 2010 at 08:09 AM. Reason: Make Code Examples

  8. #8
    Join Date
    Jul 2008
    Posts
    157

    Default

    Well, in fact, this issue has already been fixed in the 3.0.3 release.

Tags for this Thread

Posting Permissions

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