Blog系统开发 14. 小细节 (2) 分页问题displaytag Part(2)
上一节谈了displaytag的基本用法,这一节就结合项目来看一下。按照常理,可能使用到displaytag的地方,
集中在管理页面里,譬如对博文的管理或者是针对comments的管理;相对而言category似乎并不需要使用分页;
很少会有人开几十个不同的分类吧 XD。
好吧,我们来比较一下,displaytag和原来的代码有什么不同。
这是原先的页面 网页的code:
对比一下使用displayTag的页面显示
再看一下新的网页的写法:
集中在管理页面里,譬如对博文的管理或者是针对comments的管理;相对而言category似乎并不需要使用分页;
很少会有人开几十个不同的分类吧 XD。
好吧,我们来比较一下,displaytag和原来的代码有什么不同。
这是原先的页面 网页的code:
1
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=UTF-8
"
%>
2 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3 "http://www.w3.org/TR/html4/loose.dtd" >
4 <% @page import = " java.util.List " %>
5 <% @page import = " java.text.SimpleDateFormat " %>
6 <% @page import = " com.blog.Blog " %>
7 <% @page import = " com.blog.Category " %>
8 <% @page import = " com.blog.Comment " %>
9 < script type ="text/javascript" language ="JAVASCRIPT" >
10 function del(){
11 var msg = " 您真的确定要删除吗?\n\n请确认! " ;
12 if (confirm(msg)){
13 return true ;
14 } else {
15 return false ;
16 }
17 }
18 </ script >
19 < div id ="mainContent" >
20 <% Category category = (Category) request.getAttribute( " category " );
21 if (category! = null ){
22 out.print( " <h3><a href=' " + request.getContextPath() + " '>JAVA试验田</a>» " + category.getName() + " </h3> " );
23 }
24 %>
25 < table >
26 < tr >
27 < th > 主题 </ th >
28 < th ></ th >
29 < th ></ th >
30 </ tr >
31
32 <% List blogs = (List) request.getAttribute( " blogs " );
33 if (blogs ! = null ){
34 SimpleDateFormat fDate = new SimpleDateFormat( " yyyy-MM-dd " );
35 SimpleDateFormat fTime = new SimpleDateFormat( " HH:mm " );
36 for ( int i = 0 ;i < blogs.size();i ++ ){
37 Blog blog = (Blog)blogs.get(i); %>
38 < tr >
39 < td >< a href ="<%=request.getContextPath()%>/BlogServlet?method=get&bid=<%=blog.getId()%>" > <% = blog.getTitle() %> </ a > ( <% = fDate.format(blog.getDate()) %> <% = fTime.format(blog.getDate()) %> ) </ td >
40 < td > < a href ="<%=request.getContextPath()%>/BlogServlet?method=edit&bid=<%=blog.getId()%>" >< img src ="<%=request.getContextPath()%>/admin/img/edit.gif" border =0 alt ="编辑" /></ a ></ td >
41 < td > < a href ="<%=request.getContextPath()%>/BlogServlet?method=delete&bid=<%=blog.getId()%>" onclick ="javascript:return del()" >< img src ="<%=request.getContextPath()%>/admin/img/delete.gif" border =0 alt ="删除" /></ a ></ td >
42
43 </ tr >
44
45 <% }
46 }
47 %>
48 </ table >
49 <!-- end #mainContent --> </ div >
50 <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --> < br class ="clearfloat" />
51
2 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
3 "http://www.w3.org/TR/html4/loose.dtd" >
4 <% @page import = " java.util.List " %>
5 <% @page import = " java.text.SimpleDateFormat " %>
6 <% @page import = " com.blog.Blog " %>
7 <% @page import = " com.blog.Category " %>
8 <% @page import = " com.blog.Comment " %>
9 < script type ="text/javascript" language ="JAVASCRIPT" >
10 function del(){
11 var msg = " 您真的确定要删除吗?\n\n请确认! " ;
12 if (confirm(msg)){
13 return true ;
14 } else {
15 return false ;
16 }
17 }
18 </ script >
19 < div id ="mainContent" >
20 <% Category category = (Category) request.getAttribute( " category " );
21 if (category! = null ){
22 out.print( " <h3><a href=' " + request.getContextPath() + " '>JAVA试验田</a>» " + category.getName() + " </h3> " );
23 }
24 %>
25 < table >
26 < tr >
27 < th > 主题 </ th >
28 < th ></ th >
29 < th ></ th >
30 </ tr >
31
32 <% List blogs = (List) request.getAttribute( " blogs " );
33 if (blogs ! = null ){
34 SimpleDateFormat fDate = new SimpleDateFormat( " yyyy-MM-dd " );
35 SimpleDateFormat fTime = new SimpleDateFormat( " HH:mm " );
36 for ( int i = 0 ;i < blogs.size();i ++ ){
37 Blog blog = (Blog)blogs.get(i); %>
38 < tr >
39 < td >< a href ="<%=request.getContextPath()%>/BlogServlet?method=get&bid=<%=blog.getId()%>" > <% = blog.getTitle() %> </ a > ( <% = fDate.format(blog.getDate()) %> <% = fTime.format(blog.getDate()) %> ) </ td >
40 < td > < a href ="<%=request.getContextPath()%>/BlogServlet?method=edit&bid=<%=blog.getId()%>" >< img src ="<%=request.getContextPath()%>/admin/img/edit.gif" border =0 alt ="编辑" /></ a ></ td >
41 < td > < a href ="<%=request.getContextPath()%>/BlogServlet?method=delete&bid=<%=blog.getId()%>" onclick ="javascript:return del()" >< img src ="<%=request.getContextPath()%>/admin/img/delete.gif" border =0 alt ="删除" /></ a ></ td >
42
43 </ tr >
44
45 <% }
46 }
47 %>
48 </ table >
49 <!-- end #mainContent --> </ div >
50 <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --> < br class ="clearfloat" />
51
对比一下使用displayTag的页面显示
再看一下新的网页的写法:
1
<%
@taglib uri
=
"
http://displaytag.sf.net
"
prefix
=
"
display
"
%>
2 <% @page import = " java.util.List " %>
3 <% @page import = " java.text.SimpleDateFormat " %>
4 <% @page import = " com.blog.Blog " %>
5 <% @page import = " com.blog.Category " %>
6 <% @page import = " com.blog.Comment " %>
7 < script type ="text/javascript" language ="JAVASCRIPT" >
8 function del(){
9 var msg = " 您真的确定要删除吗?\n\n请确认! " ;
10 if (confirm(msg)){
11 return true ;
12 } else {
13 return false ;
14 }
15 }
16 </ script >
17 < div id ="mainContent" >
18 <% Category category = (Category) request.getAttribute( " category " );
19 if (category ! = null ) {
20 out.print( " <h3><a href=' " + request.getContextPath() + " '>JAVA试验田</a>» " + category.getName() + " </h3> " );
21 }
22 %>
23 < display:table name ="blogs" id ="blog" pagesize ="10" requestURI ="/Blog/BlogServlet" >
24 < display:column property ="title" title ="主题" />
25 < display:column property ="date" format ="{0,date,yyyy-MM-dd HH:mm}" title ="日期" />
26 < display:column title ="操作" >
27 < a href ="<%=request.getContextPath()%>/BlogServlet?method=edit&bid=${blog.id}" >< img src ="<%=request.getContextPath()%>/admin/img/edit.gif" border =0 alt ="编辑" /></ a >
28 < a href ="<%=request.getContextPath()%>/BlogServlet?method=delete&bid=${blog.id}" onclick ="javascript:return del()" >< img src ="<%=request.getContextPath()%>/admin/img/delete.gif" border =0 alt ="删除" /></ a >
29 </ display:column >
30 </ display:table >
31 <!-- end #mainContent --> </ div >
32 <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --> < br class ="clearfloat" />
33
34
23行指定了displayTag显示每页10条数据。
2 <% @page import = " java.util.List " %>
3 <% @page import = " java.text.SimpleDateFormat " %>
4 <% @page import = " com.blog.Blog " %>
5 <% @page import = " com.blog.Category " %>
6 <% @page import = " com.blog.Comment " %>
7 < script type ="text/javascript" language ="JAVASCRIPT" >
8 function del(){
9 var msg = " 您真的确定要删除吗?\n\n请确认! " ;
10 if (confirm(msg)){
11 return true ;
12 } else {
13 return false ;
14 }
15 }
16 </ script >
17 < div id ="mainContent" >
18 <% Category category = (Category) request.getAttribute( " category " );
19 if (category ! = null ) {
20 out.print( " <h3><a href=' " + request.getContextPath() + " '>JAVA试验田</a>» " + category.getName() + " </h3> " );
21 }
22 %>
23 < display:table name ="blogs" id ="blog" pagesize ="10" requestURI ="/Blog/BlogServlet" >
24 < display:column property ="title" title ="主题" />
25 < display:column property ="date" format ="{0,date,yyyy-MM-dd HH:mm}" title ="日期" />
26 < display:column title ="操作" >
27 < a href ="<%=request.getContextPath()%>/BlogServlet?method=edit&bid=${blog.id}" >< img src ="<%=request.getContextPath()%>/admin/img/edit.gif" border =0 alt ="编辑" /></ a >
28 < a href ="<%=request.getContextPath()%>/BlogServlet?method=delete&bid=${blog.id}" onclick ="javascript:return del()" >< img src ="<%=request.getContextPath()%>/admin/img/delete.gif" border =0 alt ="删除" /></ a >
29 </ display:column >
30 </ display:table >
31 <!-- end #mainContent --> </ div >
32 <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --> < br class ="clearfloat" />
33
34