[英]How to include a variable in a jsp tag?
I'm trying to dynamically include a jsp file with the following code: 我正在尝试使用以下代码动态包含一个jsp文件:
<%@include file="menus/top/${user.roleId}.jsp" %>
Here, the variable user.roleId
is an int
which is being set in my struts2 action. 在这里,变量
user.roleId
是一个在我的struts2动作中设置的int
。 I'm able to display it with the following: 我可以显示以下内容:
<s:property value="user.roleId" />
I want the files menus/top/1.jsp
, or menus/top/2.jsp
etc to be included dynamically, depending on the roleId of the currently logged in user. 我希望根据当前登录用户的roleId动态包含文件
menus/top/1.jsp
或menus/top/2.jsp
等。 But I'm getting the following exception with the include tag: 但是我收到包含include标记的以下异常:
Exception Name: org.apache.jasper.JasperException: File "menus/top/${user.roleId}.jsp" not found
What am I doing wrong? 我究竟做错了什么?
<%@include %>
is a static include directive. <%@include %>
是静态 include指令。 It's thus used at compile time, when the JSP is compiled into a class. 因此,在将JSP编译为类时,将在编译时使用它。 This implies that runtime variables can't be used inside this directive.
这意味着该指令中不能使用运行时变量。
You're looking for <jsp:include>
, which includes a resource dynamically, at runtime. 您正在寻找
<jsp:include>
,它在运行时动态包含资源。 Read this tutorial for more details. 阅读本教程以获取更多详细信息。
use <c:import>
tag. 使用
<c:import>
标记。 As it is used for dynamic include in JSTL. 由于它用于JSTL中的动态包含。
where c
: 其中
c
:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="menus/top/${user.roleId}.jsp" %>
Use struts2 include tag instead 改用struts2 include标签
<s:include file="menus/top/%{user.roleId}.jsp"/>
Documentation clearly says include directive is not processed and hence, can't have expressions that needs runtime evaluation. 文档明确指出include指令未处理,因此不能包含需要运行时评估的表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.