JSP 中的 include 工作方式与 PHP 中类似吗?我的意思是在执行之前只是静态复制粘贴?
<jsp:include page="header.jsp" />
//vs.
<?php include("header.jsp"); ?>
我唯一担心的是我有例如 Index.java = servlet 它将某些内容重定向或转发到index.jsp,在index.jsp上我称之为:
<c:if test="${sessionScope.user != null }"> //etc.
如果我将这部分代码移至 header.jsp,这将是每个页面上的代码的相同部分,如果它只是将 header.jsp 中的几行硬复制到我在执行之前将其包含在内的 index.jsp .
如果我以不同的方式写它,如果这两个选项相同:
在将 header 移出到单独的文件之前:
索引.jsp:
<!-- some code here -->
<!--header code -->
<!-- rest of code index.jps -->
将标题移出到单独的文件后:
header.jsp:
<!--header code -->
index.jsp:
<!-- some code here -->
<!-- header isn't here it was moved to header.jsp and I include that file
where I can easily change for every page at one place -->
<jsp:include page="header.jsp" />
<!-- rest of code index.jps -->
为了总结我的问题,我想知道我对包含在 JSP 中的行为的期望是否可以,这意味着这两个示例是否工作完全相同。 (“假设文件存在并且可以访问”)。
最佳答案
在实现上存在一些差异,但在行为上答案是是。 您可以阅读更多相关信息here .
关于java - JSP 中的 include 工作方式与 PHP 中的 include 类似吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21208493/