【JavaWeb】MVC案例之新闻列表
MVC案例之新闻列表
作者:白宁超
2016年6月6日15:26:30
摘要:本文主要针对javaweb基本开发之MVC案例的简单操作,里面涉及mysql数据库及表的创建,以及jsp页面和servlet的操作,整个操作流程进行梳理。其中涉及的概念问题,不在一一详述。对于整个操作流程按照开发顺序创建。(本文原创,转载标明出处:MVC案例之新闻列表)。
实验准备:
1 win*系统,一般配置笔记本或者台式机
2 安装MyEclipse开发平台,本实验使用MyEclipse2015(点击下载 访问密码 eafa)
3 Mysql数据库,当然oracle或者sql server数据库也是一样的。由于作者采用win8系统,数据库采用低版本,本实验采用mysql-installer-community-5.6.14.0.msi(点击下载 访问密码 39bf),Mysql数据库分为两种,一种是开源的,一种提供安装部署的,效果都一样。
4 JDBC链接数据库的jar包,本实验采用mysql-connector-java-5.1.20.jar(点击下载 访问密码 8bb1)
一、需求分析阶段
1 要求使用mysql数据库去创建数据库test和表news(nid int,ntitle String,ntype String,nauthor String)
2 采用MVC开发,实现新闻的查询操作
二、数据库创建阶段
# 创建数据库test
create database test;
#使用数据库
use test;
#创建表
create table test.news(
nid varchar(45) default null,
ntitle varchar(450) not null,
ntype varchar(450) not null,
nauthor varchar(45) not null
) default charset=GB2312;
#插入数据
insert test.news(nid,ntitle,ntype,nauthor) values('1','2016年6月高考最新信息','教育新闻','新华社');
insert test.news(nid,ntitle,ntype,nauthor) values('2','反贪大老虎再落马小记','时政新闻','人民日报');
#查询数据
select * from test.news  order by nid desc
三、MVC开发阶段
百度百科:MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。
注:详细MVC可以参照官方文档或者google
1 新建jsp页面命名AllNews.jsp
2 新建三个类文件AllNewsModel.java/AllNewsDao.java/AllNewsServlet.java
其中:
AllNewsModel.java:是对数据库中news表的字段实例化
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.cuit.javaweb.mvc;publicclassAllNewsModel {    privateintnid;    privateString ntitle;    privateString ntype;    privateString nauthor;    publicAllNewsModel(intid,String title,String type,String author){        this.nid=id;        this.ntitle=title;        this.ntype=type;        this.nauthor=author;    }    publicintgetNid() {        returnnid;    }    publicvoidsetNid(intnid) {        this.nid = nid;    }    publicString getNtitle() {        returnntitle;    }    publicvoidsetNtitle(String ntitle) {        this.ntitle = ntitle;    }    publicString getNtype() {        returnntype;    }    publicvoidsetNtype(String ntype) {        this.ntype = ntype;    }    publicString getNauthor() {        returnnauthor;    }    publicvoidsetNauthor(String nauthor) {        this.nauthor = nauthor;    }} | 
AllNewsDao.java:进行数据库底层操作,注意此刻mysql-connector-java-5.1.20.jar放在/WebRoot/WEB-INF/lib中,即JDBC驱动
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | package com.cuit.javaweb.mvc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;publicclassAllNewsDao {     publicList<AllNewsModel> getAllNews(){        List<AllNewsModel> AllNews=newArrayList<AllNewsModel>();        Connection conn=null;        PreparedStatement preparedStatement=null;        ResultSet resultSet=null;        try{            String driverClass="com.mysql.jdbc.Driver";            String user="root";            String password="root";            Class.forName(driverClass);            conn=DriverManager.getConnection(url,user,password);            String sql="Select * from news";            preparedStatement=conn.prepareStatement(sql);            resultSet=preparedStatement.executeQuery();            while(resultSet.next()){                intnid=resultSet.getInt(1);                String ntitle=resultSet.getString(2);                String ntype=resultSet.getString(3);                String nauthor=resultSet.getString(4);                AllNewsModel news=newAllNewsModel(nid,ntitle,ntype,nauthor);                AllNews.add(news);            }                    }        catch(Exception ex){            ex.printStackTrace();        }finally{         try{            if(resultSet!=null){                resultSet.close();            }            }catch(SQLException ex){                ex.printStackTrace();            }                  try{                if(preparedStatement!=null){                    preparedStatement.close();                }                }catch(SQLException ex){                    ex.printStackTrace();                }                  try{                if(conn!=null){                    conn.close();                }                }catch(SQLException ex){                    ex.printStackTrace();                }        }        returnAllNews;    }} | 
AllNewsServlet.java:对数据操作层指定视图显示,起着业务逻辑控制的作用
| 1 2 3 4 5 6 7 8 | publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)        throws ServletException, IOException {    AllNewsDao allNewsDao=newAllNewsDao();    List<AllNewsModel> allNews=allNewsDao.getAllNews();    request.setAttribute("AllNews", allNews);    request.getRequestDispatcher("/AllNews.jsp").forward(request, response);} | 
AllNews.jsp:视图的功能用于显示数据
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <body>   <%      List<AllNewsModel> allNews=(List<AllNewsModel>)request.getAttribute("AllNews");   %>   <table>      <tr>         <th>新闻编号</th>         <th>新闻题目</th>         <th>新闻类别</th>         <th>新闻作者</th>      </tr>     <%     for(AllNewsModel news:allNews ){      %>      <tr>         <td><%=news.getNid() %></td>         <td><%=news.getNtitle() %></td>         <td><%=news.getNtype() %></td>         <td><%=news.getNauthor() %></td>      </tr>   <%   }   %>   </table></body> | 
四、运行效果演示

如图所示:完成数据查询功能,其中运行根目录为http://localhost:8080/,项目名称day_03后面的allNewsServlet是创建servlet时候mappping出来的,即重定向技术。
 
                    
                 
                
            
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号