一、導入公共頁面的2種方法
1.使用jsp java代碼
會自動找到需要的頁面合併到代碼的位置
java源碼展示位合併,2個頁面合併爲一個
只有使用一個java類
1.關鍵代碼
<%@include file="../comment/hearder.jsp"%>
2.所有代碼
<%--
一、<%@ 的3種方法
2.include 包含頁面(一般用於設置公共的頭或者尾部)
這個jsp就是導入公共頁面的實例,使用<%@include
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<%@include file="../comment/hearder.jsp"%>
<body>
<h1>這是body體</h1>
</body>
<%@include file="../comment/footer.jsp"%>
</html>
3.效果
2.使用jsp 標籤(推薦使用)
會自動找到需要的頁面**引用**到代碼的位置
使用多個java類
java源碼爲引用,沒有直接輸出out
1.關鍵代碼
<jsp:include page="/comment/hearder.jsp"/>
2.所有代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:include page="/comment/hearder.jsp"/>
<h1>body身體</h1>
<jsp:include page="/comment/footer.jsp"/>
</body>
</html>
3.效果
二、問題與解決
1.是用 2種方法的異常
原因:代碼寫錯如下
<jsp:include page="..//comment/footer.jsp"> </jsp:include>
解決:改成正確代碼:
<jsp:include page="/comment/footer.jsp"/>
注意:[代碼塊中的java代碼會被原封不動地轉化成jsp.java裏面的java代碼,別用其他註釋進去,會報錯