2016-11-13 181 views
0

我创建使用Spring MVC的Hello World示例,但有一件事我没有在服务器URL映射明白,我没有在web.xml中有以下:URL模式的servlet映射

<servlet> 
    <servlet-name>HelloWeb</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>HelloWeb</servlet-name> 
    <url-pattern>/test/*</url-pattern> 
</servlet-mapping> 
现在

如果我要拨打以下控制器:

@Controller 
@RequestMapping("/hello") 
public class HelloWorld { 
    @RequestMapping(method = RequestMethod.GET) 
    public String printWelcome(ModelMap model){ 
     model.addAttribute("message","hello world"); 
     return "index"; 
    } 
} 

,将工作使用下面的链接: http://localhost:8080/test/hello

但是当我改变服务器URL模式为“/ *”并尝试: http://localhost:8080/hello

它不起作用,它不应该与我的servlet匹配吗? as * matches everything

回答

0

当您注册一个servlet “/ *”,那么它将覆盖所有的servlet映射如果有的话。因此应该避免。这会覆盖默认的servlet映射,因此所有默认的url处理也会被覆盖,因此任何特定的url匹配都会失败。你的情况是/你好。

就你的情况而言,最初你注册了/ test/*,这个注册了你所有的URL和/ test,因此他们被识别出来了。

0

它不适用于/*,因为您尚未为该模式注册/创建控制器。

它工作了http://localhost:8080/hello因为,你有控制器@RequestMapping("/hello")

就在RequestMapping改变@RequestMapping("/")的url-pattern的/*

+0

其实这个工作“http:// localhost:8080 /测试/你好”不是你提到的那个。 –