Announcement Announcement Module
Collapse
No announcement yet.
multiple servlet-mappings not working for DisbatcherServlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple servlet-mappings not working for DisbatcherServlet

    I have spring set up with a dipatcher servlet configured in the web.xml as follows:
    Code:
    <servlet>
    		<servlet-name>springMVC</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	     <init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value></param-value>
    		</init-param>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springMVC</servlet-name>
    		<url-pattern>*.html</url-pattern>
    	</servlet-mapping>
         <servlet-mapping>
    		<servlet-name>springMVC</servlet-name>
    		<url-pattern>/resources/*</url-pattern>
    	</servlet-mapping>
    All my urls that end with .html seem to map appropriately but for some reason anything that starts with /myapp/resources/ does not get mapped to the appropriate controller method UNLESS it ends with .html. I tried removing the servlet mapping for the .html and that doesn't fix it either. I changed the servlet mapping to /* and ALL urls end up going to the appropriate controllers but my static content is 404'd. I need to know how to configure not html urls to be served based on the path prefix /resources/ even if the url ends in .gif, .png, .css or whatever. Here is the contoller method giving me the trouble:
    Code:
    @RequestMapping(value = "/resources/{resourceName:.*}", method = RequestMethod.GET)
    	 public void renderResourceContent(
    			 @PathVariable("resourceName" ) String resourceName,
    			 HttpServletResponse resp)
    	 {
    		LOGGER.error("["+resourceName+"]");
    				 
    	 }
    Here is the URL I am trying to resolve to it.
    http://localhost:8080/myapp/resources/logo.png

    Someone please tell me what I am doing wrong!!!

  • #2
    btw here is an interesting log entry from spring:
    Code:
    2010-04-12 01:19:18,341 WARN  [org.springframework.web.servlet.PageNotFound]:962 No mapping found for HTTP request with URI [/myapp/resources/logo.png] in DispatcherServlet with name 'springMVC'
    and here is the log entry where it says that URL is mapped:
    Code:
    2010-04-12 01:14:15,312 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping]:410 Mapped URL path [/resources/{resourceName:.*}] onto handler [net.itxlabs.cms.controller.ResourceController@37fcd402]

    Comment


    • #3
      Maybe I figured it out

      Ok so I have been playing around with the servlet mappings in the web.xml and the request mappings in the controller class and I found somehting odd.

      If I set my controller to be mapped as /resources/*

      and I change my request mapping url from
      Code:
      	@RequestMapping(value = "/resources/{resourceName:.*}", method = RequestMethod.GET)
      	 public void renderResourceContent(
      			 @PathVariable String resourceName,
      			 HttpServletResponse resp)
      	 {
      		LOGGER.error("["+resourceName+"]");
               }
      to
      Code:
      	@RequestMapping(value = "/{resourceName:.*}", method = RequestMethod.GET)
      	 public void renderResourceContent(
      			 @PathVariable String resourceName,
      			 HttpServletResponse resp)
      	 {
      		LOGGER.error("["+resourceName+"]");
               }
      suddenly it starts working (responding to http://localhost:8080/myapp/resources/logo.png). It looks like mapping a spring disbatcher servlet via a path prefix causes the URIs to be stripped of the prefix when evaluating the contoller mappings. Why? Is this why the roo applications have the crazy urlrewrite stuff all over the place? Is there a way for this one URL that I can pass the prefix to the controller? If not I am gonna have to set up 3 disbatcher servlets in order to separate my responding controllers.

      Comment


      • #4
        I just wasted two hours of my life on this until I found your post. Thanks, I changed my RequestMapping and everything works perfectly!

        Comment


        • #5
          Hi guys!

          i do that in dispatcher application context.'
          Code:
          <mvc:resources mapping="/images/**" location="/public/images/" />
               <!--this is handy when you need to reference a file, or any resource outside your war-->
              <mvc:resources mapping="/content/games/**" location="file:${propertiesfilekey}/Games/Images/" />
          i hope this helps someone

          Comment


          • #6
            HI All,

            I am getting the same issue:

            My Controller class looks like this :-

            @Controller
            @RequestMapping("/auth")
            public class AuthController {

            private final static Logger logger = LoggerFactory.getLogger(AuthController.class);

            @Autowired
            AuthService authService;
            @RequestMapping(value = "/login", method = RequestMethod.GET)
            public String login(@RequestParam(value = "login_error", required = false) boolean error, Model model) {
            logger.debug("Received request to show login page, error "+error);
            if (error) {
            model.addAttribute("error", "You have entered an invalid username or password!");
            }
            return "/auth/loginpage";
            }
            }

            Servlet mapping in web.xml file looks like this:-

            <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/user/*</url-pattern>
            </servlet-mapping>

            <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/auth/*</url-pattern>
            </servlet-mapping>

            <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
            </welcome-file-list>

            Content of index.jsp:
            <jsp:forward page="auth/login"></jsp:forward>

            But when I open the URL, it says /auth/login resource not found.


            Any help will be appreciated.

            Thanks,
            Varun.

            Comment


            • #7
              Hello varunge ,

              i think you should check which servlet version you are using. that said you should have servlet-api 2.5 at least in you dev environment. if you are using maven it should be
              Code:
              <dependency>
                    <groupId>javax.servlet</groupId>
                     <artifactId>servlet-api</artifactId>
                        <version>2.5</version>
                        <scope>provided</scope>
                </dependency>
              Next step is to check whether you are indeed using that version in your web.xml
              Code:
              <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee">
              then you can use the dispatcher like this
              Code:
              <servlet-mapping>
              <servlet-name>dispatcherServlet</servlet-name>
              <url-pattern>/user/*</url-pattern>
              </servlet-mapping>
              
              <servlet-mapping>
              <servlet-name>dispatcherServlet</servlet-name>
              <url-pattern>/auth/*</url-pattern>
              </servlet-mapping>
              or even like this :
              Code:
              <servlet-mapping>
              <servlet-name>dispatcherServlet</servlet-name>
              <url-pattern>/user/*</url-pattern>
              <url-pattern>/auth/*</url-pattern>
              </servlet-mapping>
              the second one works and that's what i use. not too sure about the first one but i guess it will be ok

              I hope this helps

              Comment


              • #8
                Thanks for the reply. But I tried with the above mentioned entries in my web.xml file but still getting "The requested resource (/suggestio-1.0-SNAPSHOT/auth/login) is not available." error when I open http://localhost:8080/suggestio-1.0-SNAPSHOT/.

                This is the all I have in my web.xml file:
                ------------------------------------------------------------------------------- WEB.XML STARTS -------------------------------------------------------------------------------
                <?xml version="1.0" encoding="UTF-8"?>
                <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

                <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                /WEB-INF/spring/applicationContext.xml
                /WEB-INF/spring/applicationContext-security.xml
                </param-value>
                </context-param>

                <listener>
                <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
                </listener>

                <!-- Enables Spring Security -->
                <filter>
                <filter-name>springSecurityFilterChain</filter-name>
                <filter-class>
                org.springframework.web.filter.DelegatingFilterPro xy
                </filter-class>
                </filter>

                <filter-mapping>
                <filter-name>springSecurityFilterChain</filter-name>
                <url-pattern>/user/*</url-pattern>
                <dispatcher>FORWARD</dispatcher>
                <dispatcher>REQUEST</dispatcher>
                </filter-mapping>

                <servlet>
                <servlet-name>dispatcherServlet</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
                <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/dispatcherServlet-servlet.xml</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
                </servlet>

                <servlet-mapping>
                <servlet-name>dispatcherServlet</servlet-name>
                <url-pattern>/auth/*</url-pattern>
                </servlet-mapping>

                <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
                </welcome-file-list>


                <session-config>
                <session-timeout>30</session-timeout>
                </session-config>

                </web-app>


                ------------------------------------------------------------------------------- WEB.XML ENDS -------------------------------------------------------------------------------

                When I replace
                <servlet-mapping>
                <servlet-name>dispatcherServlet</servlet-name>
                <url-pattern>/auth/*</url-pattern>
                </servlet-mapping>
                with <servlet-mapping>
                <servlet-name>dispatcherServlet</servlet-name>
                <url-pattern>/</url-pattern>
                </servlet-mapping>

                it starts working. But I need separate servlets for different set of pages.

                This is what is present in the index.jsp to forward the request to login page:
                <jsp:forward page="auth/login"></jsp:forward>

                This is my dispatcher-Servlet xml file:

                <?xml version="1.0" encoding="UTF-8"?>
                <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

                <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                /WEB-INF/spring/applicationContext.xml
                /WEB-INF/spring/applicationContext-security.xml
                </param-value>
                </context-param>

                <listener>
                <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
                </listener>

                <!-- Enables Spring Security -->
                <filter>
                <filter-name>springSecurityFilterChain</filter-name>
                <filter-class>
                org.springframework.web.filter.DelegatingFilterPro xy
                </filter-class>
                </filter>

                <filter-mapping>
                <filter-name>springSecurityFilterChain</filter-name>
                <url-pattern>/user/*</url-pattern>
                <dispatcher>FORWARD</dispatcher>
                <dispatcher>REQUEST</dispatcher>
                </filter-mapping>

                <servlet>
                <servlet-name>dispatcherServlet</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
                <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/dispatcherServlet-servlet.xml</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
                </servlet>

                <servlet-mapping>
                <servlet-name>dispatcherServlet</servlet-name>
                <url-pattern>/auth/</url-pattern>
                </servlet-mapping>

                <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
                </welcome-file-list>


                <session-config>
                <session-timeout>30</session-timeout>
                </session-config>

                </web-app>

                Comment

                Working...
                X