一业岁、問題引入
在上一節(jié)做完之后鳞仙,頁面看上去有點(diǎn)亂,可讀性查笔时,也不易于維護(hù)棍好,其原因是html標(biāo)簽和java代碼全混在一起了,能否進(jìn)行改進(jìn)?
為了解決這個問題借笙,可以使用JSP動作標(biāo)簽
二扒怖、JSP動作標(biāo)簽
通過JSP動作標(biāo)簽,程序員可以在JSP頁面中把頁面的顯示功能部分封裝起來业稼,使整個頁面更簡潔和易于維護(hù)盗痒。
2.1<jsp:useBean>
裝載一個將在JSP頁面中使用JavaBean,發(fā)揮Java組件重用的優(yōu)勢低散。
語法:
<jsp:useBean id="name" class="package.class" scope="scope">
id:JavaBean的引用名
class:JavaBean的類
scope:JavaBean的范圍
就比如之前jsp頁面中的:
NewsService newsService = new NewsServiceImpl();
可以寫成:
<jsp:useBean id="newsService" class="cn.kgc.Service.implement.NewsServiceImpl"></jsp:useBean>
注:
1俯邓、id就是創(chuàng)建的對象名字,在頁面中如果想用這個對象了熔号,就直接引用這個對象名字就行稽鞭。
2、class是類引镊,前面的id就是這個類的對象朦蕴。但是這個地方不要直接去寫類,要寫相對路徑弟头。
3梦重、這樣做相當(dāng)于把這個對象聲明為這個頁面的全局變量了。
整體代碼如下:
<%@page import="cn.kgc.pojo.News"%>
<%@page import="java.util.List"%>
<%@page import="cn.kgc.Service.implement.NewsServiceImpl"%>
<%@page import="cn.kgc.Service.NewsService"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="newsService" class="cn.kgc.Service.implement.NewsServiceImpl"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>無標(biāo)題文檔</title>
<style type="text/css">
<!--
-->
</style>
<script>
function addNews(){
window.location="newsDetailCreateSimple.jsp";
}
</script>
</head>
<body>
<!--主體-->
<div class="main-content-right">
<!--即時新聞-->
<div class="main-text-box">
<div class="main-text-box-tbg">
<div class="main-text-box-bbg">
<form name ="searchForm" id="searchForm" action="/news/jsp/admin/newsDetailList.jsp" method="post">
<div>
新聞分類:
<select name="categoryId">
<option value="0">全部</option>
<option value='1' >國內(nèi)</option>
<option value='2' >國際</option>
<option value='3' >娛樂</option>
<option value='4' >軍事</option>
<option value='5' >財(cái)經(jīng)</option>
<option value='6' >天氣</option>
</select>
新聞標(biāo)題<input type="text" name="title" id="title" value=''/>
<button type="submit" class="page-btn">GO</button>
<button type="button" onclick="addNews();" class="page-btn">增加</button>
<input type="hidden" name="currentPageNo" value="1"/>
<input type="hidden" name="pageSize" value="10"/>
<input type="hidden" name="totalPageCount" value="2"/>
</div>
</form>
<table cellpadding="1" cellspacing="1" class="admin-list">
<thead >
<tr class="admin-list-head">
<th>新聞標(biāo)題</th>
<th>作者</th>
<th>時間</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<%
List<News> newsList = newsService.getAllNews();
int i= 0;
for(News news:newsList){
i++;
%>
<tr <%if(i%2==0){ %>class="admin-list-td-h2"<%} %>>
<td><a href='newsDetailView.jsp?id=<%=news.getId()%>'><%=news.getTitle() %></a></td>
<td><%=news.getAuthor() %></td>
<td><%=news.getCreateDate() %></td>
<td><a href='adminNewsCreate.jsp?id=2'>修改</a>
<a href="javascript:if(confirm('確認(rèn)是否刪除此新聞亮瓷?')) location='adminNewsDel.jsp?id=2'">刪除</a>
</td>
</tr>
<%} %>
</tbody>
</table>
<div class="page-bar">
<ul class="page-num-ul clearfix">
<li>共7條記錄 1/2頁</li>
<a href="javascript:page_nav(document.forms[0],2);">下一頁</a>
<a href="javascript:page_nav(document.forms[0],2);">最后一頁</a>
</ul>
<span class="page-go-form"><label>跳轉(zhuǎn)至</label>
<input type="text" name="inputPage" id="inputPage" class="page-key" />頁
<button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
</span>
</div>
</div>
</div>
</div>
</div>
</body></html>