详解spring mvc ant path的使用方法
概要:
任何一个web都需要解决url与请求处理器之间的映射,spring mvc也是一样,但spring mvc就像spring所作的一切一样(灵活,可以配置各种东西,但是也造成了很多复杂性),肯定不会只有一种方法来映射url和 controller之间的关系,并且在实际上,允许你自己创建映射规则和实现,而不仅仅依赖url映射。
1、spring path match
spring mvc中的路径匹配要比标准的web.xml要灵活的多。默认的策略实现了 org.springframework.util.antpathmatcher,就像名字提示的那样,路径模式是使用了apache ant的样式路径,apache ant样式的路径有三种通配符匹配方法(在下面的表格中列出)这些可以组合出很多种灵活的路径模式。
2、apache ant type
下面就来描述一下apache ant匹配规则,其实还是挺简单的。
ant通配符描述
| ? | 匹配任何单字符 |
| * | 匹配0或者任意数量的字符 |
| ** | 匹配0或者更多的目录 |
3、举例说明:
1、 /views/products/**/*.cfm
# matches
/views/products/index.cfm
/views/products/se10/index.cfm
/views/products/se10/details.cfm
/views/products/st80/index.cfm
/views/products/st80/details.cfm
# does not match:
/views/index.cfm
/views/aboutus/index.cfm
/views/aboutus/managementteam.cfm
2、 /views/**/*.cfm
# matches:
/views/index.cfm
/views/aboutus/index.cfm
/views/aboutus/managementteam.cfm
/views/products/index.cfm
/views/products/se10/index.cfm
/views/products/se10/details.cfm
/views/products/st80/index.cfm
/views/products/st80/details.cfm
# does not match:
/views/index.htm
/views/readme.txt
3、 /views/index??.cfm
# matches:
/views/index01.cfm
/views/index02.cfm
/views/indexaa.cfm
# does not match:
/views/index01.htm
/views/index1.cfm
/views/indexa.cfm
/views/indexother.cfm
/views/anotherdir/index01.cfm
如果知道这些我们就能够在spring mvc灵活的配置url映射了。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!