博客统计信息

51cto推荐博客
用户名:阿汐
文章数:57
评论数:70
访问量:82763
无忧币:4183
博客积分:1524
博客等级:5
注册日期:2008-06-04

我的技术圈(4)

更多>>
jsp开发教程之 仿MOP论坛 三(帖子列表-下)
2008-09-05 23:17:30
标签:jsp 休闲 职场
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://axiii.blog.51cto.com/396236/97508
 
首先设计关于帖子列表的servlet类,来进行页面和逻辑代码之间的分离和控制,这里我直接省略了bo层,权且用servlet来代替bo
com.axbbs.bo 下新建一个 GetPostsList类
内容如下:
package com.axbbs.bo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.axbbs.Dao.GetPostListDAO;
import com.axbbs.po.PostPo;

/**
* @程序编写 阿汐
* @版本 1.0
* @说明 仿wc的jsp版本小论坛
* @本模块是用于获取帖子列表信息的Servlet类
* @并且功能并不需要太多,因此本版本没有采用传统意义上的MVC开发模式,而是针对
* @vo,bo类进行了精简,用po类来共用vo类,用servlet类来共用bo类,很奇怪的开发模式
* @日期 2008.09.01
*
*/

public class GetPostsList extends HttpServlet {

    /**
  * Constructor of the object.
  */

    public GetPostsList() {
  super();
    }

    /**
  * Destruction of the servlet. <br>
  */

    public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
    }

    /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *  
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */

    public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  //创建一个集合,用来存储帖子列表的各种信息
  ArrayList list = new ArrayList();
  //创建DAO对象
  GetPostListDAO gpld = new GetPostListDAO();
  //将DAO执行的ViewList方法所返回的帖子列表信息赋值给list 集合对象
  list=gpld.ViewList();
  //创建session对象以期将帖子列表信息在页面上显示
  HttpSession session = request.getSession();
  //设置session viewlist的属性为list对象
  session.setAttribute("viewList", list);
  //页面跳转到menu.jsp,也就是帖子列表显示的页面
  response.sendRedirect("menu.jsp");
  out.flush();
  out.close();
    }

    /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *  
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */

    public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      doGet(request, response);
    }

    /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */

    public void init() throws ServletException {
  // Put your code here
    }

}
 
开始设计menu.jsp
 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
//在jsp页面导入PostPo包,用来进行类型强制转换用
<%@ page  import="com.axbbs.po.PostPo" %>
<style type="text/css">
<!--
*{
font-size:12px;

}
body{
    font-size: 12px;  
    background-color: #A9DAFF;    
}
a{
TEXT-DECORATION:none;
color:#000000;
}
.STYLE1 {
    color: #FF0000;
    font-weight: bold;
}
.STYLE4 {color: #000033}

-->
</style>
<!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=GBK" />
<title></title>
</head>

<body>
<div id="head">
  <select name="select">
    <option value="1">1日内</option>
    <option value="2">2日内</option>
    <option value="3">3日内</option>
  </select>
  <select name="select2">
    <option>时间</option>
    <option>浏览</option>
    <option>回复</option>
  </select>
  <input type="submit" name="Submit" value="ALL" />
  <input type="submit" name="Submit2" value="SELF" />
发贴 必读 <span class="STYLE1">关注</span> 刷新</div>
<br/>
<span class="STYLE4">用户在本站所发表言论只代表其个人观点,不代表本站立场. </span><br/>
<br/>
<%    
    //创建一个ArrayList集合对象用来存储帖子列表的信息
    ArrayList arrayList =(ArrayList)session.getAttribute("viewList");
    //集合存在的情况下继续
    if(arrayList!=null){
    //循环读取集合的信息,有很多帖子....
        for(int i = 0;i<arrayList.size();i++){
        //对集合进行类型强制转换用以便于取值
        PostPo pp = (PostPo)arrayList.get(i);
      //输出帖子列表的信息
      out.print("<a href=GetPostInfo?id="+pp.getPostId()+" target=mainFrame title="+pp.getPosttime()+">◆ "+pp.getPostTitle()+"("+pp.getPostReply()+"/"+pp.getPostHits() + ")"+"</a></br>");
        }
    }
%>

  
</body>
</html>

    
OK
最终我们访问程序就是这样了,这三条数据是我在数据库中手动添加用作测试的.
 
 
下一张讲解获得帖子的具体信息(看帖界面)

本文出自 “九黎部落” 博客,请务必保留此出处http://axiii.blog.51cto.com/396236/97508

分享至
更多
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别:JAVA技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: