test2009MVCDButils課程設(shè)計(jì)報(bào)告_第1頁
test2009MVCDButils課程設(shè)計(jì)報(bào)告_第2頁
test2009MVCDButils課程設(shè)計(jì)報(bào)告_第3頁
test2009MVCDButils課程設(shè)計(jì)報(bào)告_第4頁
test2009MVCDButils課程設(shè)計(jì)報(bào)告_第5頁
已閱讀5頁,還剩34頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、基于MVC模式的博客評論表的設(shè)計(jì)1、 功能簡介該系統(tǒng)主要采用MVC模式來對博客評論信息進(jìn)行顯示、修改、添加、刪除等操作。2、 MVC原理圖在MVC模式中分為模型、視圖、控制器三層。模型:代表應(yīng)用程序狀態(tài)和業(yè)務(wù)邏輯。視圖:提供可交互的客戶界面,向客戶顯示模型數(shù)據(jù)??刂破鳎焊鶕?jù)客戶的請求來操縱模型,并把結(jié)果經(jīng)由視圖展現(xiàn)給客戶。具體如下圖所示。模型層(Model)又叫業(yè)務(wù)邏輯層封裝業(yè)務(wù)方法由實(shí)現(xiàn)相應(yīng)功能的函數(shù)組成JavaBean控制器層(Control)接受用戶請求調(diào)用模型層實(shí)現(xiàn)相應(yīng)功能向視圖層輸出結(jié)果Servlet返回計(jì)算結(jié)果調(diào)用服務(wù)器端輸出用戶請求視圖層(View)用戶直接可看見,可輸入數(shù)據(jù)的界

2、面JSP客戶端3、 MVC各部分分工ViewModelControllogin.jsp登錄界面BlogDAO.java博客評論信息修改DeleteServlet.java控制博客評論信息的刪除admin_main.jsp主界面UserDAO.java用戶信息修改InsertServlet.java控制博客評論信息的添加insert.jsp添加界面Blog.java博客評論信息ListServlet.java控制博客評論信息的顯示list_blogs.jsp顯示全部信息界面User.java用戶信息LoginServlet.java控制登錄update.jsp更新界面DBConnection.j

3、ava數(shù)據(jù)庫連接UpdateDoServlet.java控制博客評論信息的更新UpdateServlet.java控制博客評論信息的修改4、 系統(tǒng)流程圖5、 表的設(shè)計(jì)1、 表字段分配用戶表列名數(shù)據(jù)類型是否允許為空是否主鍵user_id Int否是user_name Varchar(50) 否否user_pass Varchar(50) 否否emailVarchar(100) 是否levelChar(2)是否photoVarchar(20) 是否博客評論表列名數(shù)據(jù)類型是否允許為空是否主鍵Idint否是blog_idVarchar(100)是否nameVarchar(100)是否contentVa

4、rchar(100)是否2、 表的創(chuàng)建博客評論表CREATE TABLE blog ( id int NOT NULL AUTO_INCREMENT, blog_id varchar(300) DEFAULT NULL, name varchar(100) DEFAULT NULL, content varchar(100) DEFAULT NULL, PRIMARY KEY (id) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=gbk;用戶表CREATE TABLE user_table (user_id INT NOT NULL AUTO

5、_INCREMENT, user_name VARCHAR(50) NOT NULL, user_pass VARCHAR(50) NOT NULL, email VARCHAR(100), level CHAR(2), photo VARCHAR(20), CreateDate DATETIME, PRIMARY KEY (user_id);6、 具體實(shí)現(xiàn)過程1、 博客評論管理系統(tǒng)登陸界面(1)圖用戶名:admin登陸密碼:1(2) 代碼(a)view部分 function check() if(myform.userName.value = ) alert(請輸入用戶名!); myform

6、.username.focus(); return; if(myform.password.value = ) alert(請輸入密碼); myform.password.focus(); return; myform.submit(); 用戶名: 密碼: All Rights Reserved.版權(quán)沒有 Copyright2008如有任何問題和建議,請E-mail to me!建議您使用1024*768分辨率,IE6.0以上版本瀏覽本站! %String userNamePasswordLevelError = (String) request.getParameter(userNamePa

7、sswordLevelError);if (userNamePasswordLevelError != null & !userNamePasswordLevelError.equals() out.println(用戶名或密碼錯(cuò)誤或權(quán)限錯(cuò)誤); % (b)model部分public class UserDAO /private static final Logger log = Logger.getLogger(UserDAO.class); public int findByNameAndPassword(String username, String password) long res

8、ult = 0; try String querySql = select count(*) from user_table where user_name=? and user_pass=?; QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource(); result = (Long) queryRunner.query(querySql, new ScalarHandler(1), new Object username, password); /System.out.println(arr); catch (SQ

9、LException e) System.out.println(數(shù)據(jù)庫操作錯(cuò)誤); System.out.println(e.getMessage(); return (int)result; (c)control servlet部分protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(text/html;charset=UTF-8); PrintWr

10、iter out = response.getWriter(); HttpSession session = request.getSession(); String path = login.jsp; / 1、接收傳遞的參數(shù) String userName = request.getParameter(userName); String password = request.getParameter(password); String level = request.getParameter(level); / 2、將請求的內(nèi)容賦值給VO類 User user = new User(); u

11、ser.setUserName(userName); user.setPassword(password); user.setLevel(level); UserDAO userDAO = new UserDAO(); int flag = userDAO.findByNameAndPassword(userName,password); if (flag = 1) System.out.println(管理員 + user.getUserName() + 登陸成功); session.setAttribute(username, user.getUserName(); response.se

12、ndRedirect(admin/admin_main.jsp); else if (flag = 0) response.sendRedirect(login.jsp?userNamePasswordLevelError=error); 2、 主頁面(1) 圖(2) 代碼 ? 3、 顯示模塊(1) 運(yùn)行界面(2) 代碼(a) view部分 .tablecss2 border:#C0C0C0 1px solid; border-collapse:collapse; margin:5px; /*.tablecss2 td BORDER-BOTTOM: #C0C0C0 1px solid; BOR

13、DER-right: #C0C0C0 1px solid; padding:2px; */.tablecss2 tr BORDER-BOTTOM: #C0C0C0 1px solid; BORDER-right: #C0C0C0 1px solid; padding:2px; background-color:#E8E8E8 .style1 width: 800px; height: 100px; margin: 0px auto; margin-bottom:20px; border:1px solid #96C2F1; background-color: #EFF7FF; border-c

14、ollapse:collapse /* .style1 a font-size:20px; text-decoration:none; color:green */ .style1 td border:1px solid #; border-collapse:collapse ; .style1 th border:1px solid #; background-color: #0099CC; a:link color: #FF0000; text-decoration: none /* 未訪問的鏈接 */a:visited color: #; text-decoration: none /*

15、 已訪問的鏈接 */a:hover color: #FF00FF; text-decoration: underline /* 鼠標(biāo)在鏈接上 */a:active color: #0000FF; text-decoration: underline /* 激活鏈接 */ 請輸入查詢內(nèi)容: 添加新博客評論 博客id 博客地址 姓名 內(nèi)容 編輯 修改 % if (all.size() a href=Update?Id= IMG onclick=javascript:edit() height=16 src=./images/pencil.gif width=16 border=0 IMG oncl

16、ick=javascript:del() height=16 src=./images/bin_closed.gif width=16 border=0 (b)model部分 public Blog queryById(int id) Blog blog = null; String sql = SELECT id, + blog_id, + name, + content from blog where id=?; / Connection conn = DBConnection.getConnection(); QueryRunner qr = new QueryRunner(JDBCUt

17、ils.getDataSource(); try blog = (Blog) qr.query(sql, new BeanHandler(Blog.class),id); catch (SQLException e) e.printStackTrace(); return blog;(c)control servlet部分 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.set

18、ContentType(text/html;charset=UTF-8); request.setCharacterEncoding(GB2312); String keyword = request.getParameter(keyword); / out.println(keyword) ; if (keyword = null) / 沒有任何查詢條件 keyword = ; / 編碼轉(zhuǎn)換 PrintWriter out = response.getWriter(); try BlogDAO blogDao = new BlogDAO(); request.setAttribute(blo

19、gs,blogDao.queryAll() ; request.getRequestDispatcher(list_blogs_jstl.jsp).forward(request, response); finally out.close(); 4、 添加數(shù)據(jù)模塊(1) 運(yùn)行界面(2) 代碼(a) view部分JSP+JDBC 博客評論管理添加新博客評論博客地址: 姓名: 內(nèi)容: 回到博客評論列表頁(b)model部分 public boolean insert(Blog blog) String sql = INSERT INTO blog( + blog_id, + name, + con

20、tent ) VALUES(?,?,?); /Connection conn = DBConnection.getConnection(); boolean updateSuccessFlag = false; Object insertParams = blog.getBlog_id(), blog.getName(), blog.getContent(); try QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource(); int i = qr.update( sql, insertParams); if (i = 1) / 如果

21、插入成功,則肯定能執(zhí)行到此段代碼 updateSuccessFlag = true; catch (SQLException e) System.out.println(更新操作中出現(xiàn)錯(cuò)誤!); System.out.println(e); return updateSuccessFlag; (c)control servlet部分public class InsertServlet extends HttpServlet /* * Processes requests for both HTTP GET and POST methods. * param request servlet re

22、quest * param response servlet response * throws ServletException if a servlet-specific error occurs * throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType(tex

23、t/html;charset=UTF-8); PrintWriter out = response.getWriter(); try / String subject = request.getParameter(subject) ;/String choice_a = request.getParameter(choiceA) ;/String choice_b = request.getParameter(choiceB) ;/String choice_c= request.getParameter(choiceC) ;/String choice_d = request.getPara

24、meter(choiceD) ;/String answer = request.getParameter(answer);/ String typeId = request.getParameter(typeId);/ String categoryId = request.getParameter(categoryId);/ Question question=new Question();/ question.setSubject(subject);/ question.setChoiceA(choice_a);/ question.setChoiceB(choice_b);/ ques

25、tion.setChoiceC(choice_c);/ question.setChoiceD(choice_d);/ question.setAnswer(answer);/ question.setAnswer(answer);/ question.setTypeId(typeId);/ question.setCategoryId(categoryId); Blog blog = new Blog(); Map map = request.getParameterMap(); BeanUtils.populate(blog, map); BlogDAO blogDAO = new Blo

26、gDAO(); boolean flag = false; flag=blogDAO.insert(blog); request.setAttribute(flag, new Boolean(flag); String path = insert_do.jsp; request.getRequestDispatcher(path).forward(request, response); catch (Exception e) finally out.close(); / /* * Handles the HTTP GET method. * param request servlet requ

27、est * param response servlet response * throws ServletException if a servlet-specific error occurs * throws IOException if an I/O error occurs */Override protected voiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException processRequest(request, respons

28、e); /* * Handles the HTTP POST method. * param request servlet request * param response servlet response * throws ServletException if a servlet-specific error occurs * throws IOException if an I/O error occurs */Override protected voiddoPost(HttpServletRequest request, HttpServletResponse response)

29、throws ServletException, IOException processRequest(request, response); /* * Returns a short description of the servlet. * return a String containing servlet description */Override public String getServletInfo() return Short description; / 5、 修改數(shù)據(jù)模塊(1) 運(yùn)行模塊(2) 代碼(a) view部分JSP+DAO 博客評論管理程序博客評論管理范例 JS

30、P + DAO實(shí)現(xiàn) 修改博客評論 博客地址 input type=text name=blog_id value= 姓名 input type=text name=name value= 內(nèi)容 input type=text name=content value= input type=hidden name=Id value= 回到博客評論列表頁(b)model部分public boolean update(Blog blog) String sql = UPDATE blog set blog_id=?, + name=?, +content=? where id=?; Connection conn = DBConnection.getConnection(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論