/ jsp

Spring MVC jsp 嵌入子页面的两种方式

使用 jsp 渲染页面时,可以在页面中嵌入(include)其他 jsp 页面。嵌入子页面有两种方式:

  • 静态嵌入:<% @include file="include.jsp" %>
  • 动态嵌入:<jsp:include page="include.jsp" />


<% @include file="include.jsp" %>

静态嵌入支持 jsphtmlxml 以及纯文本。

静态嵌入在编译时完成,相当于直接将子页面的文本插入到 include 标签所在的位置。子页面可直接使用父页面中的变量。


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



<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<!-- 动态嵌入子页面 -->
<jsp:include page="jspf/footer.jsp" flush="true">
    <jsp:param name="year" value="2014"/>

使用 jsp:include 时必须设置 flush 属性为 true

The page attribute defines the file containing your JSP logic. The flush attribute tells the server to send the current page's output buffer (the part of the page that's already been processed) to the browser before processing the included file. According to version 1.1 of the JSP specification, the flush attribute must be set to true.

Note: Because the output buffer must be flushed before processing a jsp:include tag, you cannot use certain behaviors after the tag. These behaviors include forwarding to another page, setting cookies, or setting other HTTP headers.

根据 jsp 文档,使用 jsp:include 标签时,服务器会先将该标签之前的部分推送到浏览器端,因此,在 jsp:include 之后,无法再进行页面跳转、设置 cookie、更改 http header 等操作。