Spring MVC URL pattern mapping in web.xml?

2019-07-23 19:24发布

问题:

I have below config in web.xml

<servlet>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
         </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>

    <servlet-mapping>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>

I have controller as below.

@Controller  
public class SomeController { 

   @RequestMapping("/somePath")
    public String showExtendedUi() {
        return "somePage";
    }


}  

Now client would call the controller by sending url params as belo:

http://localhost:8080/myApp/somePath?param1=456&param2=456

But the controller method is not being called.

Is my URL correct?

回答1:

Your controller method is not being invoked because you have mapped mvc-dispatcher to *.do Change servlet mapping to

<servlet-mapping>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>


回答2:

As the URL pattern for Dispatcher Servlet is configured as *.do, The controller would be called only only by url requests of pattern "something.do".

so your url http://localhost:8080/myApp/somePath.do?param1=456&param2=456 would work if all other configurations are made properly.