简体   繁体   中英

Spring MVC URL pattern mapping in web.xml?

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?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM