JSP 使用 ${pageContext.request.contextPath} 无效

${pageContext.request.contextPath}

  通过 ${pageContext.request.contextPath} 可在 JSP 中取得当前的项目绝对路径,比如当前项目是 http://localhost:8080/demo, 则 ${pageContext.request.contextPath} 代表的就是 /demo,其中 / 代表 http://localhost:8080,所以一般使用 ${pageContext.request.contextPath} 定位资源。

JSP 中 isELIgnored 属性

  isELIgnored 属性表示是否忽略 EL 表达式,如果值为 true,那么 JSP 中的 EL 表达式被当成字符串处理。比如下面这个表达式 ${2000 / 20}, 在 isELIgnored=“true” 时输出为字符串 ${2000 / 20},当 isELIgnored=“false” 时输出为 100。

问题原因

  Maven 项目默认使用 web-app 2.3 版本(各版本 web-app 标签),据说是为了扩展性,到了 web-app 2.5 才默认支持 EL 表达式,于是导致使用 ${pageContext.request.contextPath} 无效。

解决方案

1.JSP 中添加属性 isELIgnored=“false”

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>

2.使用 web-app 4.0 版本

<?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">

</web-app>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值