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

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

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

静态嵌入

<% @include file="include.jsp" %>。使用这种方法时,

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

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

动态嵌入

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

页面嵌入在运行时完成,子页面拥有独立的作用域。

子页面中的变量需要由父页面传入,传入变量的语法如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>  
<!DOCTYPE html>  
<html>  
<head>  
  <title>JSP - Include Demo</title>
</head>  
<body>
Lorem Ipsum
<jsp:include page="jspf/footer.jsp" flush="true">  
  <jsp:param name="year" value="2014"/>
</jsp:include>
</body>  
</html>  

使用 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 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 等操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值