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>

四、运行效果演示

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

最新文章

  1. MVC5-11 浅谈拦截器
  2. 修改crontab默认的编辑器
  3. dedecms 发布文章时,关键字会自动加内链
  4. HDU 5741 Helter Skelter(构造法)
  5. PMBOK 和 PRINCE2的技术不同的地方是什么
  6. [20190423]简单测试latch nowilling等待模式.txt
  7. js 发送短信倒计时、秒杀倒计时实现代码
  8. Aspnet Mvc 前后端分离项目手记(三)关于restful 风格Url设计
  9. 此博客不再更新和分享UiPath文章
  10. selenium之 chromedriver与chrome版本映射表(更新至v2.46)
  11. html标签必备
  12. Java中利用Scanner键入的字符串与其他字符串的比较
  13. 利用navcat为mysql数据库单独的表赋权限及表结构同步
  14. ueditor 正在读取目录及网络链接错误
  15. PAT甲题题解-1048. Find Coins (25)-水
  16. MongoDB副本集配置系列五:副本集的在线迁移
  17. CSS动画实现菜单栏从左边滑出
  18. vijos 1360 八数码问题 - 启发式搜索
  19. 〖Linux〗apt-get wait for another apt process
  20. Farm Irrigation ZOJ 2412(DFS连通图)

热门文章

  1. 在C#中调用EXE文件
  2. CentOS7 编译安装 Git 服务器 Centos 7.0 + Git 2.2.0 + gitosis (实测 笔记)
  3. 页面制作部分之PS切图
  4. C语言中的sizeof()
  5. xcode gdb/lldb调试命令
  6. Visual Studio 2013 Web开发
  7. 每周一书-《鸟哥的Linux私房菜》获奖公布
  8. C语言 &#183; 十六进制转十进制
  9. C/C++预处理指令#define,#ifdef,#ifndef,#endif…
  10. ajax局部刷新后,如何让局部中的百度分享重新加载