您的位置:首页 > 其它

简单Blog项目笔记之八:文章的显示

2012-12-13 15:23 253 查看
也是分列表显示和详细显示两种

一:列表显示

用用户名查出所有对应的文章

然后正常分页显示(分页要是弄个类就好了, 之前文章评论的分页也是直接写进来的)

// 以下是对文章的全部查询
hql = "from ArticleInfo";//设置对文章全部查询HQL语句
String account = request.getParameter("account");//页面中的account参数

if (null != account) {//判断account参数是否为空
try {
account = new String(account.getBytes("ISO8859_1"),"gbk");
System.out.println("ssssssss "+account);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hql = "from ArticleInfo where author = '" + account + "'";
request.setAttribute("account", account);
}
objectDao = new ObjectDao<ArticleInfo>();//持久化类objectDao对象的实例化
List<ArticleInfo> list = objectDao.queryList(hql);//执行查询的HQL语句
//对分页进行操作
int showNumber = 10;
Integer count = 0;
if (null != request.getParameter("count")) {
count = new Integer(request.getParameter("count"));
}
list = objectDao.queryList(hql);
int maxPage = list.size();
if (maxPage % showNumber == 0) {
maxPage = maxPage / showNumber;
} else {
maxPage = maxPage / showNumber + 1;
}
if (0 == count) {
list = objectDao.queryList(hql, showNumber, count);
} else {
count--;
list = objectDao.queryList(hql, showNumber, count * showNumber);
}
request.setAttribute("count", count);
request.setAttribute("list", list);
request.setAttribute("maxPage", maxPage);
// 文章所对应的发布人
hql = "select author from ArticleInfo group by author";
List authorList = objectDao.queryListObject(hql);
request.setAttribute("authorList", authorList);
return "admin_articleQuery";


前台遍历显示

iterator的id是article 所以每一项的property也要加#

<s:iterator value="%{#request.list}" id="article">
<tr align="center">
<td height="25"><s:property value="#article.title"/></td>
<td><s:property value="#article.typeName"/></td>
<td><s:property value="#article.author"/></td>
<td><s:property value="#article.sendTime"/></td>
<td><s:property value="#article.visit"/></td>
<td><s:a href="articleInfo_admin_articleQueryOne.htm?id=%{#article.id}">详细查询</s:a></td>
</tr>
</s:iterator>


二:详细显示

通过列表显示中的一行超链接跳转

<td><s:a href="articleInfo_admin_articleQueryOne.htm?id=%{#article.id}">详细查询</s:a></td>


然后实例化, 显示其中的每一项
Integer id = new Integer(request.getParameter("id"));//获取id参数
hql = "from ArticleInfo where id =" + id;//根据id参数查询的hql语句
objectDao = new ObjectDao<ArticleInfo>();//实例化持久化类
articleInfo = objectDao.queryFrom(hql);//执行查询
String account = (String) request.getSession().getAttribute("account");//获取account参数
if(null==account){//如果	account对象为空
account=articleInfo.getAuthor();//获取用户名
hql = "from UserInfo where account = '" + account + "'";//根据用户名查询的hql语句
ObjectDao<UserInfo>	objectDao1 = new ObjectDao<UserInfo>();//实例化持久化类
UserInfo userInfo = objectDao1.queryFrom(hql);//执行查询
request.getSession().setAttribute("userInfo", userInfo);//将查询结果保存在Session中
}
if (null == request.getParameter("count")) {
if (!articleInfo.getAuthor().equals(account)) {
articleInfo.setVisit(articleInfo.getVisit() + 1);
objectDao.updateT(articleInfo);
}
}
request.setAttribute("articleInfo", articleInfo);//保存articleInfo


在这之后还要显示每篇文章对应的评论, 就能接上上一篇文章说的内容了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: