RiKeR

本博客停止更新,最新内容请访问--> http://blog.csdn.net/shuailee

统计

留言簿(3)

积分与排名

阅读排行榜

评论排行榜

我做的第二个MVC项目的小结(完结篇)

 

       1list.jsp==>新建一个ServletDeleteServlet.java,此时仍用doPost方法接收(点击按钮)

package org.riker.news.control;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.riker.news.data.NewsDataBean;

import org.riker.news.logic.NewsLogicBean;

 

public class DetailServlet extends HttpServlet {

 

       /**

        * Constructor of the object.

        */

       public DetailServlet() {

              super();

       }

 

       /**

        * Destruction of the servlet. <br>

        */

       public void destroy() {

              super.destroy(); // Just puts "destroy" string in log

              // Put your code here

       }

 

       /**

        * 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 {

 

              String newsid = request.getParameter("newsid");

              NewsDataBean bean = new NewsLogicBean().detail(newsid);

              if(bean!=null) {

                     request.setAttribute("bean",bean);

                     request.getRequestDispatcher("main/detail.jsp").forward(request, response);

              } else {

                     request.getRequestDispatcher("main/error.jsp").forward(request, response);

              }

             

       }

 

       /**

        * Initialization of the servlet. <br>

        *

        * @throws ServletException if an error occure

        */

       public void init() throws ServletException {

              // Put your code here

       }

 

}

 

2NewsLogicBean.java,写公共的,修改relevant,写deleteNews()方法

package org.riker.news.logic;

 

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

 

import org.riker.news.comm.DataSource;

import org.riker.news.data.NewsDataBean;

 

public class NewsLogicBean {

       private int allRows;

       private int curPage = 1;

       private int rowPerPage = 5;

       private int allPages;

 

       public List findAll() {

              List list = new ArrayList();

              Statement st = null;

              ResultSet rs = null;

              try {

                     st = DataSource.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

                     rs = st.executeQuery("select newsid, newstitle, newscontent, newscount, newsdate from news order by newsdate desc");

                     rs.last();

                     this.allRows = rs.getRow();

                     if(this.allRows%this.rowPerPage==0) {

                            this.allPages = this.allRows/this.rowPerPage;

                     } else {

                            this.allPages = this.allRows/this.rowPerPage + 1;

                     }

                     if(this.curPage == 1) {

                            rs.beforeFirst();

                     } else {

                            rs.absolute((this.curPage - 1) * this.rowPerPage);

                     }

                     int i = 0;

                     while(rs.next() && i < this.rowPerPage) {

                            NewsDataBean bean = new NewsDataBean();

                            bean.setNewsid(rs.getString(1));

                            bean.setNewstitle(rs.getString(2));

                            bean.setNewscontent(rs.getString(3));

                            bean.setNewscount(rs.getString(4));

                            bean.setNewsdate(rs.getString(5));

                            list.add(bean);

                            i++;

                     }

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              return list;

       }

      

       public int insertNews(String newsTitle, String newsContent) {

             

              return modify("insert into news(newstitle, newscontent, newscount, newsdate) values ('"+newsTitle+"', '"+newsContent+"', 0, now()");

 

       }

      

       public int modify(String sql) {

              int result = 0;

              Statement st = null;

              try {

                     st = DataSource.getConnection().createStatement();

                     result = st

                                   .executeUpdate(sql);

 

              } catch (Exception e) {

                     // TODO: handle exception

              } finally {

                     try {

                            st.close();

                     } catch (SQLException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                     }

              }

             

              return result;

       }

      

       public int deleteId(String newsid) {

              return modify("delete from news where newsid="+newsid);

       }

      

       public int updateNews(String newsid, String newsTitle, String newsContent) {

              return modify("update news set newstitle='"+newsTitle+"', newscontent='"+newsContent+"' where newsid="+newsid);

       }

      

       public NewsDataBean findById(String newsid) {

              NewsDataBean bean = new NewsDataBean();

              Statement st = null;

              ResultSet rs = null;

              try {

                     st = DataSource.getConnection().createStatement();

                     rs = st.executeQuery("select newsid, newstitle, newscontent, newscount, newsdate from news where newsid=" + newsid);

                     rs.last();

                    

                     if(rs.next() ) {

                            bean.setNewsid(rs.getString(1));

                            bean.setNewstitle(rs.getString(2));

                            bean.setNewscontent(rs.getString(3));

                            bean.setNewscount(rs.getString(4));

                            bean.setNewsdate(rs.getString(5));

                     }

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              return bean;

       }

      

       public NewsDataBean detail(String newsid) {

              NewsDataBean bean = new NewsDataBean();

              Statement st = null;

              ResultSet rs = null;

              try {

                     st = DataSource.getConnection().createStatement();

                     st.executeUpdate("update news set newscount=newscount+1 where newsid="+newsid);

                     rs = st.executeQuery("select newsid, newstitle, newscontent, newscount, newsdate from news where newsid=" + newsid);

                     rs.last();

                    

                     if(rs.next() ) {

                            bean.setNewsid(rs.getString(1));

                            bean.setNewstitle(rs.getString(2));

                            bean.setNewscontent(rs.getString(3));

                            bean.setNewscount(rs.getString(4));

                            bean.setNewsdate(rs.getString(5));

                     }

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              return bean;

       }

      

       public static void main(String[] args) {

              List list = new NewsLogicBean().findAll();

              System.out.println(list.size());

       }

 

       public int getCurPage() {

              return curPage;

       }

 

       public void setCurPage(int curPage) {

              this.curPage = curPage;

       }

 

       public int getAllPages() {

              return allPages;

       }

 

       public int getAllRows() {

              return allRows;

       }

 

       public int getRowPerPage() {

              return rowPerPage;

       }

 

}

 

       3DeleteServlet.java,去COPY过来跳转的语

 

句。==>查看新闻详细,中间,新建DetailServlet.java,注意此时为doGet

 

地址栏的提交)==>NewsLogicBean.javaCOPY上面Insertpublic

 

NewsDataBean detail(String newsid) {}==>DetailServlet.javaCOPY过来

 

FindByIdServlet.javaNewsDataBean bean = new NewsLogicBean

 

().findById(newsid)modified it==>

package org.riker.news.control;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.riker.news.data.NewsDataBean;

import org.riker.news.logic.NewsLogicBean;

 

public class DetailServlet extends HttpServlet {

 

       /**

        * Constructor of the object.

        */

       public DetailServlet() {

              super();

       }

 

       /**

        * Destruction of the servlet. <br>

        */

       public void destroy() {

              super.destroy(); // Just puts "destroy" string in log

              // Put your code here

       }

 

       /**

        * 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 {

 

              String newsid = request.getParameter("newsid");

              NewsDataBean bean = new NewsLogicBean().detail(newsid);

              if(bean!=null) {

                     request.setAttribute("bean",bean);

                     request.getRequestDispatcher("main/detail.jsp").forward(request, response);

              } else {

                     request.getRequestDispatcher("main/error.jsp").forward(request, response);

              }

             

       }

 

       /**

        * Initialization of the servlet. <br>

        *

        * @throws ServletException if an error occure

        */

       public void init() throws ServletException {

              // Put your code here

       }

 

}

 

       4、用insert.jsp复制新建detail.jsp,引

 

NewsDataBean Package,写标签 <jsp:useBean id="news"

 

class="org.riker.news.logic.NewsLogicBean"/>,这么做没必要!!WHY

因为DetailServlet.javarequest.setAttribute("bean",bean);已经放入

 

bean了,所以直接NewsDataBean bean = (NewsDataBean)

 

request.getAttribute("bean");注意request.getAttribute("bean"),然后格

 

式化,更美观些,用JavaScript返回,<a href="javascript:history.back

 

()">返回</a>

 

<%@ page contentType="text/html;charset=GBK" import="org.riker.news.data.*"%>

 

<html>

 <head>

   

    <title></title>

   

 </head>

 <body>

 <%

  NewsDataBean bean = (NewsDataBean)request.getAttribute("bean");

 %>

  <h2 align="center">

  <%=bean.getNewstitle()%>               发布时间:<%=bean.getNewsdate()%>

  </h2>

  <%=bean.getNewscontent()%>点击的次数:<%=bean.getNewscount()%>

 <hr>

 

    <a href="javascript:history.back()">返回</a>

 

 

 </div>

 </body>

</html>

 

posted on 2007-08-28 18:37 RiKeR 阅读(267) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: