web.xml中url-pattern /和/*之间的区别

web.xml中url-pattern /和/*之间的区别:

1.

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

2.

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1.servlet的< url-pattern > / </ url-pattern > 不会拦截*.jsp,也就不会进入spring的DispatcherServlet类。

2.filter的< url-pattern > /* </ url-pattern >会拦截,就是说所有的这个项目的请求都会被捕捉,过滤。

注:为避免静态资源被拦截需要在spring.xml文件中配置<mvc:resources location="/images/" mapping="/images/**"/>。


总之,关于web.xml的url映射:

< url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url

< url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)


案列分析springmvc中dispatcherServlet配置/和/*的区别:

1. 首先/这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配。
2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个DispatcherServlet有/和/*,先匹配的是/*这个
3. 当配置相同的情况下:
<一>/:使用/配置路径,直接访问到jsp,不经springDispatcherServlet
<二> /*:配置/*路径,不能访问到多视图的jsp

比如:

当我在客户端调用URL:/user/list然后返回user.jsp视图,当配置的是/:DispathcherServlet拿到这个请求然后返回对应的controller,
然后依据Dispather Type为Forward类型转发到user.jsp视图,即就是请求user.jsp视图(/user/user.jsp),此时Dispather没有拦截/user/user.jsp,
因为此时你配置的是默认的/,就顺利的交给ModleAndView去处理显示了。
当配置的是/*:DispathcherServlet拿到这个请求然后返回对应的controller,然后通过Dispather Type通过Forward转发到user.jsp视图,
即就是请求user.jsp视图(/user/user.jsp),此时Dispather已经拦截/user/user.jsp,Dispatcher会把他当作Controller去匹配,没有匹配到就会报404错误。

注:在配置视图的时候尽量用/这种方式。


我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。

  • 9
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
我用Spring5的aop应用时报这个错误Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\ecliple\web5\WEB-INF\applicationContext.xml]; nested exception is java.nio.file.NoSuchFileException: WEB-INF\applicationContext.xml,他说我的applicationContext.xml文件不存在,可是我明明有这个文件,另外我的web.xml需要更改吗<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation </param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <display-name>Struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> <!--2.5版本的写法 --> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
最新发布
05-30
这个错误提示说找不到你的 applicationContext.xml 文件,可能是文件路径设置有误或者是文件名写错了。你需要检查一下 applicationContext.xml 文件是否存在于 WEB-INF 目录下,并且在 contextConfigLocation 配置的路径是否正确。 同时,你的 web.xml 文件的 contextConfigLocation 配置也需要修改一下,将参数值改为 /WEB-INF/applicationContext.xml (去掉后面的空格),这样才能正确加载 applicationContext.xml 文件。 修改后的 web.xml 文件应该如下所示: ``` <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <display-name>Struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> <!--2.5版本的写法 --> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰太狼_cxh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值