符合JSP规范:
A JSP container can include a mechanism for being notified if an included file changes, so the container can recompile the JSP page. However, the JSP 1.2 specification does not have a way of directing the JSP container that included files have changed.
我在在线教程中找到了这个:
If the included file is changed but not the JSP which is including it then the changes will reflect only when we use include action tag. The changes will not reflect if you are using include directive as the JSP is not changed so it will not be translated (during this phase only the file gets included when using directive) for request processing and hence the changes will not reflect.
然后,我写了一个例子来尝试一下:A.jsp、B.jsp和C.jsp,
A.jsp使用JSP include指令包含C.jsp:
<%@ include file="/jsp/C.jsp" %>
B.jsp 使用 JSP include 标记包含 C.jsp:
<jsp:include page="/jsp/C.jsp" />
当我在浏览器中访问A.jsp和B.jsp时,它们都显示正常, 然后我更改了C.jsp,并重新刷新了A.jsp和B.jsp,它们都可以显示C.jsp的更改。但如果 JSP 规范为 true,则 A.jsp 不应显示 C.jsp 的更改。 有什么问题吗?
最佳答案
首先我们要考虑这个,
<%@ include file="/jsp/C.jsp" %>
- include 指令将把页面粘贴到原来的位置 使用此语句。
- 如果您对静态内容进行了更改,则位于
A.jsp
或C.jsp
,它将根据您所做的更改进行渲染。
<jsp:include page="/jsp/C.jsp" />
- 此包含操作标记用于插入静态或动态 内容。
- 因此,更改将按原样反射(reflect),即
C.jsp
将要 通过将请求和响应对象传递给它来执行 将执行的页面包含在A.jsp
中.
编辑:
例如,
在 B.jsp
,
<c:out value="${value}"/>
在 A.jsp
,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="value" value="10"/>
<span>From @include <%@include file="B.jsp" %></span>
<span>From jsp:include <jsp:include page="B.jsp" /></span>
当页面加载时,
From @include 10
From jsp:include
如果你检查并看到,它看起来像,
<span>From @include 10</span>
<span>From jsp:include <c:out value/></span>
这是因为,@include
将替换 B.jsp
的内容进入A.jsp
在渲染开始之前,jsp:include
, A.jsp
和B.jsp
将单独执行。
作为taglib
未导入 B.jsp
,c:out
标记未执行。
更改后B.jsp
至
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${value}"/>
如果您检查并看到渲染的页面,它看起来像,
<span>From @include 10</span>
<span>From jsp:include </span>
变量value
其范围仅在 A.jsp
中.
在 jsp:include
scope is page
在 @include
scope is request
关于java - JSP include指令和JSP include标签的区别之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405189/