【JavaWeb】MVC案例之新闻列表

时间:2024-05-01 08:36:33

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() default null,
ntitle varchar() not null,
ntype varchar() not null,
nauthor varchar() not null
) default charset=GB2312;
#插入数据
insert test.news(nid,ntitle,ntype,nauthor) values('','2016年6月高考最新信息','教育新闻','新华社');
insert test.news(nid,ntitle,ntype,nauthor) values('','反贪大老虎再落马小记','时政新闻','人民日报'); #查询数据
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表的字段实例化

package com.cuit.javaweb.mvc;

public class AllNewsModel {
private int nid;
private String ntitle;
private String ntype;
private String nauthor;
public AllNewsModel(int id,String title,String type,String author){
this.nid=id;
this.ntitle=title;
this.ntype=type;
this.nauthor=author;
}
public int getNid() {
return nid;
}
public void setNid(int nid) {
this.nid = nid;
}
public String getNtitle() {
return ntitle;
}
public void setNtitle(String ntitle) {
this.ntitle = ntitle;
}
public String getNtype() {
return ntype;
}
public void setNtype(String ntype) {
this.ntype = ntype;
}
public String getNauthor() {
return nauthor;
}
public void setNauthor(String nauthor) {
this.nauthor = nauthor;
}
}

AllNewsDao.java:进行数据库底层操作,注意此刻mysql-connector-java-5.1.20.jar放在/WebRoot/WEB-INF/lib中,即JDBC驱动

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; public class AllNewsDao {
public List<AllNewsModel> getAllNews(){
List<AllNewsModel> AllNews=new ArrayList<AllNewsModel>();
Connection conn=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try{
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql:///test";
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()){
int nid=resultSet.getInt(1);
String ntitle=resultSet.getString(2);
String ntype=resultSet.getString(3);
String nauthor=resultSet.getString(4);
AllNewsModel news=new AllNewsModel(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();
}
}
return AllNews;
}
}

AllNewsServlet.java:对数据操作层指定视图显示,起着业务逻辑控制的作用

	public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AllNewsDao allNewsDao=new AllNewsDao();
List<AllNewsModel> allNews=allNewsDao.getAllNews();
request.setAttribute("AllNews", allNews);
request.getRequestDispatcher("/AllNews.jsp").forward(request, response); }

AllNews.jsp:视图的功能用于显示数据

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

四、运行效果演示

【JavaWeb】MVC案例之新闻列表

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