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

  • Understanding DispatcherServlet

    I am trying to use spring-security

    Before all of the configuration

    Code:
    http://localhost:9090/app/login2.xhtml
    request, works as i expected.

    I added a controller:

    Code:
     @Controller
        @RequestMapping("/auth")
        public class LoginController {
        
        
         @RequestMapping(value = "/login", method = RequestMethod.GET)
         public String getLoginPage(@RequestParam(value="error", required=false) boolean error, 
           ModelMap model) {
        return "login2.xhtnml";
        }
        
        }
    I have in web.xml:




    Code:
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                		classpath:META-INF/spring-servlet.xml
          		</param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
         
        <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
        </servlet-mapping>
    With this configuration when i call

    Code:
    http://localhost:9090/app/login2.xhtml
    Error comes

    WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/login2.xhtml] in DispatcherServlet with name 'spring'


    BUT when i change configuration mapping to

    Code:
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/app/*</url-pattern>
        </servlet-mapping>
    Code:
    `http://localhost:9090/app/login2.xhtml`
    works as i expected

    but

    Code:
    http://localhost:9090/app/auth/login
    gives no error, no exception, no redirection, i think dispatcher servlet can not know about this request.

    Code:
    http://localhost:9090/app/app/auth/login
    works with

    Code:
      <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/app/*</url-pattern>
            </servlet-mapping>
    My understanding:

    dispatcher servlet use `"http://localhost:9090/"` as base for searching login2.xhtml
    and use
    Code:
    `"http://localhost:9090/app"`
    for `/auth/login` URL.

    I do not know where to set this, and why they are different.

  • #2
    It depends and you leave out some of the details so it is actually quite hard to tell.

    In general the url is /[application]/[servlet]/[remainder].

    Now if you have a foo.war application would be foo, if you have mapped your servlet to app the remainder will be used do detect a controller. If nothing can be found detection and rendering is passed to the servlet container. So given this.


    Code:
    http://localhost:9090/app/auth/login
    That isn't deteced by the dispatcher servlet because then there should be a servlet mapped to /auth/* or /* but you hvae mapped to /app (and /app/app != /app ).

    Error comes

    WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/login2.xhtml] in DispatcherServlet with name 'spring'
    Read again it isn't an error it is a warning indicating that this servlet cannot handle te request, which makes perfectly sense as you have not mapped a controller to the login2.xhtml.

    Comment


    • #3
      yes, it is a warning but it does not allow the page to come up.

      Additional info: my tomcat server.xml contains a line like

      Code:
      <context path="/app" >

      Now i only want to know

      with

      Code:
      <url-pattern>/*</url-pattern>
      why can dispatcher servlet find
      Code:
      http://localhost:9090/app/auth/login
      why can not dispatcher servlet find
      Code:
      http://localhost:9090/app/login2.xhtml
      I think i resolve the thing if i can understand it.

      Comment


      • #4
        Read my post...

        Originally posted by mdeinum
        ... you have not mapped a controller to the login2.xhtml.

        Comment


        • #5
          I changed web xml to have two dispacther but my normal urls like /app/login2.xhtml are still unhappy about the second dispatcher why does not contain "mapping found for HTTP request".

          Code:
          <servlet>
          <servlet-name>spring</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                  		classpath:META-INF/spring-servlet.xml
            		</param-value>
              </init-param>
          <load-on-startup>1</load-on-startup>
          </servlet>
           
          
          <servlet-mapping>
          <servlet-name>spring</servlet-name>
          <url-pattern>/app/*</url-pattern>
          </servlet-mapping>
          
           
          <servlet>
          <servlet-name>spring2</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                  		classpath:META-INF/spring2-servlet.xml
            		</param-value>
              </init-param>
          <load-on-startup>1</load-on-startup>
          </servlet>
          
          
          <servlet-mapping>
          <servlet-name>spring2</servlet-name>
          <url-pattern>/*</url-pattern>
          </servlet-mapping>

          Comment


          • #6
            That doesn't matter even if you would have another one mapped at *.xhtml it still doesn;t work.. You don't have a Controller mapped to that URL so there is NOTHING that handles that URL. A DispatcherServlet isn't a Controller it dispatches requests to Controllers.

            Comment


            • #7
              Sorry for asking too much, if you are bored please do not reply

              Originally posted by Marten Deinum View Post
              That doesn't matter even if you would have another one mapped at *.xhtml it still doesn;t work.. You don't have a Controller mapped to that URL so there is NOTHING that handles that URL. A DispatcherServlet isn't a Controller it dispatches requests to Controllers.
              Then, why does it work with mapping below, there is no controller in below case too

              Code:
              <servlet-mapping>
              <servlet-name>spring</servlet-name>
              <url-pattern>/app/*</url-pattern>
              </servlet-mapping>

              Comment


              • #8
                I changed web xml to have two dispacther but my normal urls like /app/login2.xhtml are still unhappy about the second dispatcher why does not contain "mapping found for HTTP request".
                Then, why does it work with mapping below, there is no controller in below case too
                You have now lost me, the post before it it didn't work (at least that is what you state) and now you say it does work.. Which is it.

                The case is still the same it doesn't work because there is no mapping to a Controller in neither servlet (unless you have configured a default handler then there is).

                I strongly suggest you read the reference guide (or by my book) and understanding what is happening.

                Comment


                • #9
                  works with one dispatcher servlet and one mapping contains /app/*

                  when i add two dispatcher servlet and different mappings with one /* , it does not work.

                  sorry i could not tell briefly

                  Comment


                  • #10
                    I still don't get your problem, the dispatcherservlet works in the way described earlier. You still need a handler the fact that there is/or is a servlet doesn't matter.

                    /app is your application with a servlet mapped to /app the servlet will react to urls like /app/app not /app (basic servlet mapping rules I suggest the servlet spec for that). If it doesn't match (as explained also earlier) it will pass on control to the normal servlet container and as your xhtml is accesible by anyone it will render the page.

                    Comment

                    Working...
                    X