`

JSP中EL表达式的取值范围

阅读更多

在JSP中,脚本化语言如“<%=contextPath%>”可以从page,request,session及application中取值。
下面的代码可以看到结果是因为contextPath被保存在page中:
<% String contextPath = request.getContextPath(); %>
<form action="<%=contextPath%>/sub/submit.jsp" method="post">  
</form>  

EL表达式如“${contextPath}”只能从request,session及application中取值。
上述代码修改一下就可以支持EL表达式:
<%  
  String contextPath = request.getContextPath();  
  // 将contextPath保存到request中
  request.setAttribute("contextPath", contextPath);
%>
<form action="${contextPath}/sub/submit.jsp" method="post">  
</form> 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics