绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程

以系统的一个“添加商品”的功能为例加以说明,系统页面为add.jsp,如图一所示:


图一  添加商品界面

系统的代码目录结构及add.jsp代码如图二所示:


图二   系统的代码目录结构及add.jsp代码

${pageContext.request.contextPath}用于解决使用相对路径时出现的问题,它的作用是取出所部署项目的名字。

对于图片文件,如图二所示${pageContext.request.contextPath}/bookcover/101.jpg”使用的是绝对路径,${pageContext.request.contextPath}返回的是”/test”【注意:很多地方写的返回值是test/”,这是不对的】,拼接之后路径就变成:“/test/bookcover/101.jpg”。如果使用相对路径则为:“../../bookcover/101.jpg”(相对于add.jsp页面)。其他的CSS文件、Js文件、Jsp文件与图片文件相似,使用方法一样。

JavaWeb项目中,Jsp页面的form表单的action属性也常常会使用${pageContext.request.contextPath}来表示请求路径。如图二中【没有使用框架】的form表单的action属性为:action="${pageContext.request.contextPath }/servlet/addBookServlet"。要理解这个请求地址,必须先了解web.xml【注意:web.xml必须放在WEB-INF文件夹下,原因见博客:http://blog.csdn.net/sun9528/article/details/72423112】的写法:

①完全匹配:以“/”开头,以字母(非“*”)结束

   如:上面提到的本项目中:<url-pattern>/servlet/addBookServlet</url-pattern>

②目录匹配:以“/”开头且以“/*”结尾

   如:<url-pattern>/test/*</url-pattern>

   <url-pattern>/*</url-pattern>

③扩展名匹配:以“*.”开头,以扩展名结束

   如:<url-pattern>*.do</url-pattern>

④“/”用来表明对应的Servlet为应用默认的Servlet。在这种情况下Servlet路径是请求的URI去掉上下文路径并且路径信息为null

本项目中“商品添加”功能的web.xml配置如下:

  <servlet>
	    <servlet-name>AddBookServlet</servlet-name>
	    <servlet-class>com.itheima.web.servlet.AddBookServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
	    <servlet-name>AddBookServlet</servlet-name>
	    <url-pattern>/servlet/addBookServlet</url-pattern>
  </servlet-mapping>

当点击了页面提交表单的命令之后,一个请求发送到servlet容器,servlet容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url。访问的是http://localhost:8080/test/servlet/addBookServlet,我的应用上下文是test,容器会将http://localhost:8080/test去掉,剩下的/servlet/addBookServlet部分拿来做servlet的映射匹配。很明显可以通过图三的形式在web.xml中找到请求的动作类(url-pattern--->servlet-name--->servlet-class)。动作类为:com.itheima.web.servlet.AddBookServlet。然后由动作类在进行一些后台操作。


图三 url-pattern--->servlet-name--->servlet-class

 



<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 明确指定只扫描Controller --> <context:component-scan base-package="com.itheihei" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/images/**" location="/images/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:annotation-driven/> </beans><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 排除Controller,只扫描Service/Repository等 --> <context:component-scan base-package="com.itheihei"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>@Controller public class AccountController { @Autowired AccountService accountService; @RequestMapping("findAllAccounts") public String findAllAccounts() { System.out.println("方法成功执行"); return "list"; } } <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="${pageContext.request.contextPath}/findAllAccounts">查看所有用户信息</a> </body> </html><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> <!-- 将所有请求映射到 dispatcherServlet --> </servlet-mapping> </web-app>为什么会404
最新发布
05-14
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值