java - JSP include指令和JSP include标签的区别之一

标签 java jsp servlets

符合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" %>

  1. include 指令将把页面粘贴到原来的位置 使用此语句。
  2. 如果您对静态内容进行了更改,则位于 A.jspC.jsp ,它将根据您所做的更改进行渲染。

<jsp:include page="/jsp/C.jsp" />

  1. 此包含操作标记用于插入静态或动态 内容。
  2. 因此,更改将按原样反射(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.jspB.jsp将单独执行。

作为taglib未导入 B.jspc: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/

相关文章:

mysql - 为什么我会收到此 MySQLIntegrityConstraintViolationException : Column 'first_name' cannot be null when exuting this script?

javascript - 我如何访问 JSP 中的 javascript 变量?

java - JSTL : Why my data are not being printed in JSP?

java.util.logging : Remove date time line

java - JVM——子类方法的执行顺序和@override的使用

java - Servlet的反射(reflection),Httpservletrequest类型正在获取RequestFacade类型并且无法调用方法

java - 为什么 getRemoteHost() 在部署应用程序时返回 IP 地址?

java - 如何在 Java 正则表达式中获取美元符号

java - Hibernate:session.createQuery 和 session 缓存

java - 了解 servlet-api 中的请求对象。是单例吗?