1. 程式人生 > >bootstrap 實現導航欄不變 內容變

bootstrap 實現導航欄不變 內容變

當我們在建web專案時會建立多個頁面,如果每一個頁面都需要通過同樣的導航欄索引呈現不同的內容,我們可以建一個導航頁,在其他頁面中加入這個導航頁,能最大實現重用率

這裡介紹兩種實現方法

靜態包含

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

動態包含 

<jsp:include page="temple.jsp" flush="true"/>

首先建一個導航頁


<html>

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="description" content="">
		<meta name="author" content="">
		<link rel="icon" href="../../favicon.ico">

		<title>Dashboard Template for Bootstrap</title>

		<link href="css/bootstrap.min.css" rel="stylesheet">

		<link href="css/ie10-viewport-bug-workaround.css" rel="stylesheet">

		<link href="css/dashboard.css" rel="stylesheet">

		<script src="js/ie-emulation-modes-warning.js"></script>

	</head>

	<body>

		<nav class="navbar navbar-inverse navbar-fixed-top">
			<div class="container-fluid">
				<div class="navbar-header">
					<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
				<span class="sr-only">Toggle navigation</span> <span
					class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
					<a class="navbar-brand" href="#">Project name</a>
				</div>
				<div id="navbar" class="navbar-collapse collapse">
					<ul class="nav navbar-nav navbar-right">
						<li>
							<a href="#">Dashboard</a>
						</li>
						<li>
							<a href="#">Settings</a>
						</li>
						<li>
							<a href="#">Profile</a>
						</li>
						<li>
							<a href="#">Help</a>
						</li>
					</ul>
					<form class="navbar-form navbar-right">
						<input type="text" class="form-control" placeholder="Search...">
					</form>
				</div>
			</div>
		</nav>

		<div class="container-fluid">
			<div class="row">
				<div class="col-sm-3 col-md-2 sidebar">
					<ul class="nav nav-sidebar">
						<li class="active">
							<a href="index.jsp">Overview <span class="sr-only">(current)</span></a>
						</li>
						<li>
							<a href="list.jsp">Reports</a>
						</li>
						<li>
							<a href="login.jsp">Analytics</a>
						</li>
						<li>
							<a href="#">Export</a>
						</li>
					</ul>
					<ul class="nav nav-sidebar">
						<li>
							<a href="">Nav item</a>
						</li>
						<li>
							<a href="">Nav item again</a>
						</li>
						<li>
							<a href="">One more nav</a>
						</li>
						<li>
							<a href="">Another nav item</a>
						</li>
						<li>
							<a href="">More navigation</a>
						</li>
					</ul>
					<ul class="nav nav-sidebar">
						<li>
							<a href="">Nav item again</a>
						</li>
						<li>
							<a href="">One more nav</a>
						</li>
						<li>
							<a href="">Another nav item</a>
						</li>
					</ul>
				</div>
				<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
					</div>
				</div>
			</div>
		</div>

		<!-- Bootstrap core JavaScript
    ================================================== -->
		<!-- Placed at the end of the document so the pages load faster -->
		<script src="js/jquery.min.js"></script>
		<script>
			window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
		</script>
		<script src="js/bootstrap.min.js"></script>
		<script src="js/holder.min.js"></script>
		<script src="js/ie10-viewport-bug-workaround.js"></script>
	</body>

</html>

然後在導航頁上新增連結,在跳轉頁面head中新增如下程式碼,並新增格式

<jsp:include page="index.jsp" flush="true"/>
 <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"></div>
<html>
<head>
<base href="<%=basePath%>">
<jsp:include page="index.jsp" flush="true"/>
<title>My JSP 'update.jsp' starting page</title>
</head>

<body>

 <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
	<form action="loginservlet?op=login" method="post">
		<table>
			<tr>
				<th>姓名:</th>
				<td><input type="text" name="name"/></td>
			</tr>
			<tr>
				<th>密碼:</th>
				<td><input type="text" name="password"/></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<button type="submit">登入</a></td>
			</tr>
		</table>
	</form>
	</div>
</body>
</html>

 實現效果如圖