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


也是分列表显示和详细显示两种


一:列表显示

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

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

// 以下是对文章的全部查询
		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

在这之后还要显示每篇文章对应的评论, 就能接上上一篇文章说的内容了





















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值