JavaWeb中MVC的使用--以管理系统举例
开发环境:JavaSE1.7、JavaEE7.0、JSTL1.2.2、Web2.3、MySQL5.5.28
系统分析与功能设计:
本系统实现商品信息的管理,应包括以下几个功能:
商品信息列表:列出所有商品信息,并提供对指定商品信息的修改和删除接口
添加商品信息:向数据库中添加一条商品信息
编辑商品信息:修改数据库中已有的商品信息
删除商品信息:删除指定商品的信息
异常处理:跳转到错误页面并显示异常信息

MVC框架模式设计:
(1)模型:Proccess.java完成商品信息的在数据库中的增删改查,将要处理的商品信息封装到Goods.java中。
(2)控制器:Controller.java完成区别客户端不同业务请求,根据不同的参数进行不同的操作。
(3)视图:list.jsp显示商品信息列表,同时提供增加、编辑和删除链接。
edit.jsp添加或编辑信息
error.jsp显示异常信息
除此以外添加监听器,当应用关闭时关闭与数据库的链连接。
项目结构:

效果截图:

注意事项:
(1)在Web项目中导入MySQL架包要把jar复制到WebRoot/WEB-INF/lib下。
(2)Web2.3中使用JSTL时需要在jsp上加这两句:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>
(3)PreparedStatement能够对sql语句进行预编译,预编译后能够提高数据库sql语句执行效率。用于处理动态SQL语句,在执行前会有一个预编译过程,这个过程是有时间开销的,虽然相对数据库的操作,该时间开销可以忽略不计,但是PreparedStatement的预编译结果会被缓存,下次执行相同的预编译语句时,就不需要编译,只要将参数直接传入编译过的语句执行代码
中就会得到执行,所以,对于批量处理可以大大提高效率。
Proccess.java
package mvc.model; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; public class Proccess {
private static Connection conn; private void getConn() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/goods?characterEncoding=UTF-8",
"root", "123456"); // url user passwd
} private Goods fill(ResultSet rs) throws SQLException{
Goods gd = new Goods();
gd.setHh(rs.getString("hh"));
gd.setName(rs.getString("name"));
gd.setNum(rs.getString("number"));
return gd;
} public List<Goods> list() throws ClassNotFoundException, SQLException{
List<Goods> gds = new ArrayList<Goods>();
if(conn == null){
getConn();
}
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from goods");
while(rs.next()){
gds.add(fill(rs));
}
rs.close();
statement.close();
return gds;
} public Goods findByHH(String hh) throws ClassNotFoundException, SQLException{
Goods gd = null;
if(conn == null){
getConn();
}
PreparedStatement pstatement = conn.prepareStatement("select * from goods where hh= ?");
pstatement.setString(1, hh);
ResultSet rs = pstatement.executeQuery();
if(rs.next()){
gd = fill(rs);
}
return gd;
} public void save(Goods gd, String oldHh) throws ClassNotFoundException, SQLException{
if(conn == null){
getConn();
}
String sql = "update goods set hh=?,name=?,number=? where hh=?";
if(oldHh == null || "".equals(oldHh)){
sql = "insert into goods set hh=?,name=?,number=?";
}
PreparedStatement pstatement = conn.prepareStatement(sql);
pstatement.setString(1, gd.getHh());
pstatement.setString(2, gd.getName());
pstatement.setString(3, gd.getNum());
if(oldHh != null && !("".equals(oldHh))){
pstatement.setString(4, oldHh);
}
pstatement.executeUpdate();
} public void delete(String Hh) throws ClassNotFoundException, SQLException{
if(conn == null){
getConn();
}
String sql = "delete from goods where hh=?";
PreparedStatement pstatement = conn.prepareStatement(sql);
pstatement.setString(1, Hh);
pstatement.executeUpdate();
} public static void conClose(){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Proccess
Controller.java
package mvc.controller; import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import mvc.model.Goods;
import mvc.model.Proccess; public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L; public Controller() {
super();
} public void destroy() {
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
Proccess pc = new Proccess();
try {
if ("list".equals(action)) {
List<Goods> goods = pc.list();
String test = "Test";
request.setAttribute("goods", goods);
request.setAttribute("test", test);
request.getRequestDispatcher("list.jsp").forward(request, response);
} else if ("add".equals(action)) {
request.getRequestDispatcher("edit.jsp").forward(request, response);
} else if ("edit".equals(action)) {
String hh = request.getParameter("hh");
Goods gds = pc.findByHH(hh);
request.setAttribute("gds", gds);
request.getRequestDispatcher("edit.jsp").forward(request, response); } else if ("save".equals(action)) {
String oldHh = request.getParameter("oldHh");
String hh = request.getParameter("hh");
String name = request.getParameter("name");
String num = request.getParameter("num"); Goods gds = new Goods();
gds.setHh(hh);
gds.setName(name);
gds.setNum(num);
pc.save(gds, oldHh);
response.sendRedirect("ctrl?action=list"); } else if ("delete".equals(action)) {
String hh = request.getParameter("hh");
pc.delete(hh);
response.sendRedirect("ctrl?action=list");
}
} catch (Exception e) {
request.setAttribute("errMsg", e.getMessage());
request.getRequestDispatcher("error.jsp").forward(request, response);
e.printStackTrace();
}
} public void init() throws ServletException {
}
}
Controller
JavaWeb中MVC的使用--以管理系统举例的更多相关文章
- javaWeb中MVC的编程思想示例
没有学习MVC之前我只写了一个Servlet类(Note_List.java),分层之后,我将这个类分成了5个类(NoteDao.java,,NoteDaoImpl.java,,NoteService ...
- JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...
- javaWeb中的文件上传下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- MVC模式学习--雇员管理系统项目开发
1, 项目开发,使用原型开发, ① 开发流程: 需求分析->设计阶段->编码阶段->测试阶段->发布阶段/维护阶段 需求阶段:PM/项目经理 对客户 设计阶段:技术人员(架构师 ...
- JavaWeb笔记——MVC设计模式和JavaWeb经典三层架
1 MVC设计模式 MVC设计模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(C ...
- Odoo domain 中的 like, ilike, =like, =ilike 举例说明【转】
Odoo domain 中的 like, ilike, =like, =ilike 举例说明 Odoo domain 操作符使用场景非常多,很多小伙伴被 like, ilike, =like, =il ...
- PHP实例开发(3)PHP中MVC学习之ThinkPHP
PHP中MVC学习之ThinkPHP 1.什么是MVC MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离 MVC是一个设 ...
- 在JavaWeb中使用Log4j步骤
在JavaWeb中使用Log4J指南.每次在开始写一个项目的时候都忘记Log4J如何配置.所以写个步骤,作为记录. 第一步 下载Log4J jar包 从Apache Logging Services ...
- 在Javaweb中使用Scala
Java 是一门比较优秀的编程语言, 其最大功劳是建立非常繁荣的JVM平台生态.不过 Java 语法比较麻烦,写过 C, Python 的人总是想使用简洁的语法,又希望利用上 Java 平台的强大,因 ...
随机推荐
- 一行删除所有docker container的命令
ns=`docker | awk '//{print $1}'`;for n in $ns;do docker rm -f $n;done docker | awk '{print $1}'|xarg ...
- (转)C#用Linq实现DataTable的Group by数据统计
本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序 ...
- php实现oracle操作
<?php function Query($sql,$prms){ $db = " (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ...
- wpf数据验证实例及常用方法小结
虽然标题是wpf数据验证,但并不是对IDataErrorInfo.ValidationRule.属性中throw Exception这几种验证方式的介绍: 之前做项目时(例如员工工资管理),根据员工编 ...
- weex和vue开发环境配置详解(配置系统变量等等)
本文详细讲解如何搭建weex和vue开发环境 安装java 现在java安装包,网上的安装包都是国外的,很难下载下来 就用这个链接下载,亲测无毒,http://www.wmzhe.com/soft-3 ...
- 删除CentOS系统自带的jdk
转自:https://www.cnblogs.com/linjiqin/archive/2013/03/23/2977377.html 在安装CentOS6.4时,系统会自动安装jdk,先把它下载掉, ...
- mysql使用存储过程插入数据后,参数为中文的为?或乱码
最近了解了一下mysql存储过程,之前版本的mysql不支持存储过程,5.0版本后就可以支持存储过程的使用:恰好笔者下载使用版本为5.6.20: 做了一个给表插入数据的简单存储过程,发现打开表后汉字全 ...
- C#log4net引入配置文件后,数据库连接找不到并且有很多 未能找到元素“appender”的架构信息
今天用了log4net加入配置信息后,数据库链接的字符串就报错,无法连接数据库.后来发现,只需要调整一下位置就可以了 configSections 节点必须写在 connectionStrings 节 ...
- 使用spring-loaded实现应用热部署
作为一名Java开发者您是否会遇到这种情况:新增一个方法或字段必须重启tomcat才能对其进行调试? 有没有办法使得不重启tomcat就能调试呢.spring-loaded就可以. spring-lo ...
- UML在实践中的现状和一些建议
本文是我在csdn上看到的文章,由于认识中的共鸣,摘抄至此. 原文地址:http://blog.csdn.net/vrman/article/details/280157 UML在国内不少地方获得了应 ...