书接上回,前面说了那么多,到底ZK有啥好处呢?这个就只可意会不可言传了,呵呵。
举个例子,假设有个列表需要显示,在页面中可以使用table或者是ul li来实现,table的代码如下:
<table cellspacing=
"0" cellpadding=
"0" width=
"90%" border=
"0"
class=
"table_cont">
<tbody>
<%
//判断列表是否为空
if(activeList== null || activeList.isEmpty() || activeList.size()<1)
{
//为空,显示提示信息
out.print( "<tr><td colspan=\"4\" style=\"width:400px;\">目前 "+strProvinceName+ " 还没有活动</td></tr>");
}
else
{
//不为空,显示列表内容
out.println( "<tr>\n<th style=\"width:40%;\">名称</th><th style=\"width:30%;\">活动日期</th><th style=\"width:10%;\">状态</th><th style=\"width:20%;\">操作</th>\n</tr>");
Iterator<Active> iter = activeList.iterator();
Active active = null;
while(iter.hasNext())
{
active = (Active)iter.next();
out.println( "<tr>");
out.print( "<td style=\"width:40%;\">"+active.getTitle()+ "</td>");
if(active.getCreateDt()!= null)
{
out.print( "<td style=\"width:30%;\">"+active.getCreateDt().toString().substring(0,10)+ "</td>");
}
else
{
out.print( "<td style=\"width:10%;\"> </td>");
}
if(active.getStatus()==1)
{
out.print( "<td>上线</td>");
}
else if(active.getStatus()==2)
{
out.print( "<td>下线</td>");
}
out.print( "<td style=\"width:20%;\"><a href=\"activeUpdate.jsp?province="+province+ "&activeId="+active.getId()+ "\">修改</a> ");
out.print( "<a href=\"activeDel.jsp?province="+province+ "&activeId="+active.getId()+ "\">删除</a></td>");
out.println( "</tr>");
}
}
%>
</tbody>
</table>
<tbody>
<%
//判断列表是否为空
if(activeList== null || activeList.isEmpty() || activeList.size()<1)
{
//为空,显示提示信息
out.print( "<tr><td colspan=\"4\" style=\"width:400px;\">目前 "+strProvinceName+ " 还没有活动</td></tr>");
}
else
{
//不为空,显示列表内容
out.println( "<tr>\n<th style=\"width:40%;\">名称</th><th style=\"width:30%;\">活动日期</th><th style=\"width:10%;\">状态</th><th style=\"width:20%;\">操作</th>\n</tr>");
Iterator<Active> iter = activeList.iterator();
Active active = null;
while(iter.hasNext())
{
active = (Active)iter.next();
out.println( "<tr>");
out.print( "<td style=\"width:40%;\">"+active.getTitle()+ "</td>");
if(active.getCreateDt()!= null)
{
out.print( "<td style=\"width:30%;\">"+active.getCreateDt().toString().substring(0,10)+ "</td>");
}
else
{
out.print( "<td style=\"width:10%;\"> </td>");
}
if(active.getStatus()==1)
{
out.print( "<td>上线</td>");
}
else if(active.getStatus()==2)
{
out.print( "<td>下线</td>");
}
out.print( "<td style=\"width:20%;\"><a href=\"activeUpdate.jsp?province="+province+ "&activeId="+active.getId()+ "\">修改</a> ");
out.print( "<a href=\"activeDel.jsp?province="+province+ "&activeId="+active.getId()+ "\">删除</a></td>");
out.println( "</tr>");
}
}
%>
</tbody>
</table>
效果图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/afc113bc5556dbf2e53d60dc732284ed.jpeg)
可以看出其中有一些的判断,然后还要担心“<%”和“%>”是否匹配,“{”和“}”是否匹配等等情况。如果是ZK的话,这些不必要的担心就可以省去了。
页面(list.zul)代码:
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
<? page title ="列表" contentType ="text/html;charset=UTF-8" ?>
< zk >
< window style ="heigth:100%; border:0; text-align:center;" id ="winList" >
< style src ="../styles/global.css" > </ style >
< div style ="padding-top:20px; vertical-align:bottom;" >
< label value ="列表" /> < separator />
</ div >
< div style ="padding:0px 0px 0px 0px;text-align:center;width:80%;" >
< label id ="lblTips" visible ="false" style ="color:#FF0000;" />
< listbox id ="blacklistList" style ="width:100%;" >
< listhead style ="text-align:center;" >
< listheader label ="用户号码" style ="width:15%;" />
< listheader label ="用户类别" style ="width:15%;" />
< listheader label ="说明" style ="width:35%;" />
< listheader label ="添加日期" style ="width:15%;" />
< listheader label ="操作" style ="width:20%;" />
</ listhead >
</ listbox >
</ div >
< zscript language ="Java" >
<![CDATA[
import com.zk.list;
list ui = new list();
ui.setWinMain(winlList);
ui.showAllBlacklist();
]]>
</ zscript >
</ window >
</ zk >
<? page title ="列表" contentType ="text/html;charset=UTF-8" ?>
< zk >
< window style ="heigth:100%; border:0; text-align:center;" id ="winList" >
< style src ="../styles/global.css" > </ style >
< div style ="padding-top:20px; vertical-align:bottom;" >
< label value ="列表" /> < separator />
</ div >
< div style ="padding:0px 0px 0px 0px;text-align:center;width:80%;" >
< label id ="lblTips" visible ="false" style ="color:#FF0000;" />
< listbox id ="blacklistList" style ="width:100%;" >
< listhead style ="text-align:center;" >
< listheader label ="用户号码" style ="width:15%;" />
< listheader label ="用户类别" style ="width:15%;" />
< listheader label ="说明" style ="width:35%;" />
< listheader label ="添加日期" style ="width:15%;" />
< listheader label ="操作" style ="width:20%;" />
</ listhead >
</ listbox >
</ div >
< zscript language ="Java" >
<![CDATA[
import com.zk.list;
list ui = new list();
ui.setWinMain(winlList);
ui.showAllBlacklist();
]]>
</ zscript >
</ window >
</ zk >
逻辑处理代码(list.java):
public
void showAllBlacklist()
{
try
{
// 得到列表
BlacklistIF blacklistIf = ServiceLocator.getBlacklistIF();
List<Blacklist> blacklistList = blacklistIf.findAllBlacklist(-1);
// 判断列表是否为空
if(blacklistList.size()>0 && !blacklistList.isEmpty())
{
this.showList(blacklistList);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private void showList(List<Blacklist> blacklistList)
{
// 得到列表组件,用于显示群发安排列表
Listbox listbox = (Listbox)winMain.getFellow( "blacklistList");
// 列表的行组件
Listitem listitem = new Listitem();
// 列表的列组件
Listcell listcell = new Listcell();
for( int i=0;i<blacklistList.size();i++)
{
// 用户号码
listcell.setLabel(blacklistList.get(i).getMobile());
listitem.appendChild(listcell);
// 用户类别
listcell = new Listcell();
switch(blacklistList.get(i).getType())
{
case 1:
listcell.setLabel( "黑名单");
break;
case 2:
listcell.setLabel( "黄名单");
break;
case 3:
listcell.setLabel( "绿名单");
break;
case 4:
listcell.setLabel( "非彩信用户");
break;
}
listitem.appendChild(listcell);
// 说明
listcell = new Listcell();
if(blacklistList.get(i).getDescription().equals(""))
{
listcell.setLabel( "暂无");
}
else
{
listcell.setLabel(blacklistList.get(i).getDescription());
}
listitem.appendChild(listcell);
// 添加日期
listcell = new Listcell();
listcell.setLabel(blacklistList.get(i).getCreateDate().toString().substring(0,10));
listitem.appendChild(listcell);
// 显示操作按钮
Hbox hbox = new Hbox();
// 彩信产品的id,添加事件监听时要用final修饰的变量
final int id = Integer.parseInt(blacklistList.get(i).getId().toString().trim());
// 添加一个查看按钮
Button button = new Button();
button.setLabel( "查看");
// 为查看按钮添加一个
button.addEventListener(Events.ON_CLICK, new EventListener()
{
public void onEvent(Event arg0) throws Exception {
showBlacklistDetail(String.valueOf(id));
}
});
button.setVisible( false);
hbox.appendChild(button);
// 添加一个删除按钮
button = new Button();
button.setLabel( "删除");
// 为删除按钮添加一个
button.addEventListener(Events.ON_CLICK, new EventListener()
{
public void onEvent(Event arg0) throws Exception {
delBlacklist(String.valueOf(id));
}
});
hbox.appendChild(button);
listcell = new Listcell();
listcell.appendChild(hbox);
listitem.appendChild(listcell);
// 将当前行在列表中显示
listbox.appendChild(listitem);
listitem = new Listitem();
listcell = new Listcell();
}
}
{
try
{
// 得到列表
BlacklistIF blacklistIf = ServiceLocator.getBlacklistIF();
List<Blacklist> blacklistList = blacklistIf.findAllBlacklist(-1);
// 判断列表是否为空
if(blacklistList.size()>0 && !blacklistList.isEmpty())
{
this.showList(blacklistList);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private void showList(List<Blacklist> blacklistList)
{
// 得到列表组件,用于显示群发安排列表
Listbox listbox = (Listbox)winMain.getFellow( "blacklistList");
// 列表的行组件
Listitem listitem = new Listitem();
// 列表的列组件
Listcell listcell = new Listcell();
for( int i=0;i<blacklistList.size();i++)
{
// 用户号码
listcell.setLabel(blacklistList.get(i).getMobile());
listitem.appendChild(listcell);
// 用户类别
listcell = new Listcell();
switch(blacklistList.get(i).getType())
{
case 1:
listcell.setLabel( "黑名单");
break;
case 2:
listcell.setLabel( "黄名单");
break;
case 3:
listcell.setLabel( "绿名单");
break;
case 4:
listcell.setLabel( "非彩信用户");
break;
}
listitem.appendChild(listcell);
// 说明
listcell = new Listcell();
if(blacklistList.get(i).getDescription().equals(""))
{
listcell.setLabel( "暂无");
}
else
{
listcell.setLabel(blacklistList.get(i).getDescription());
}
listitem.appendChild(listcell);
// 添加日期
listcell = new Listcell();
listcell.setLabel(blacklistList.get(i).getCreateDate().toString().substring(0,10));
listitem.appendChild(listcell);
// 显示操作按钮
Hbox hbox = new Hbox();
// 彩信产品的id,添加事件监听时要用final修饰的变量
final int id = Integer.parseInt(blacklistList.get(i).getId().toString().trim());
// 添加一个查看按钮
Button button = new Button();
button.setLabel( "查看");
// 为查看按钮添加一个
button.addEventListener(Events.ON_CLICK, new EventListener()
{
public void onEvent(Event arg0) throws Exception {
showBlacklistDetail(String.valueOf(id));
}
});
button.setVisible( false);
hbox.appendChild(button);
// 添加一个删除按钮
button = new Button();
button.setLabel( "删除");
// 为删除按钮添加一个
button.addEventListener(Events.ON_CLICK, new EventListener()
{
public void onEvent(Event arg0) throws Exception {
delBlacklist(String.valueOf(id));
}
});
hbox.appendChild(button);
listcell = new Listcell();
listcell.appendChild(hbox);
listitem.appendChild(listcell);
// 将当前行在列表中显示
listbox.appendChild(listitem);
listitem = new Listitem();
listcell = new Listcell();
}
}
效果图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/297f44618377ca24ed7b340781bc0078.jpeg)
可以看到,这样就达到了逻辑处理和页面显示的代码分离,使得页面显示的代码更加清晰,而逻辑处理类的作用也更加明显。
转载于:https://blog.51cto.com/jerrysun/315141