1. 先简单介绍一下MVC:
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:
模型(Model)、视图(View)和控制器(Controller)。
* 控制器Controller:对请求进行处理,负责请求转发 -------servlet
* 视图View:界面设计人员进行图形界面设计;-------jsp
* 模型Model:程序编写程序应用的功能(实现算法等等)、数据库管理;-----javbean
2. Java与MVC
JSP Model1第一代:JSP + DB
JSP Model1第二代:JSP + javabean + DB
JSP Model2:JSP + Servlet + JavaBean + DB
(1).Servlet页面
public class BookServlet extends BaseServlet {
private BookService bookService = new BookService();
public String load(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User form = CommonUtils.toBean(request.getParameterMap(), User.class);
try {
User user = userService.login(form);
request.getSession().setAttribute("session_user", user);
request.getSession().setAttribute("cart", new Cart());
return "r:/index.jsp"; //这是继承了BaseServlet
} catch (UserException e) {
request.setAttribute("msg", e.getMessage());
request.setAttribute("form", form);
return "f:/jsps/user/login.jsp";
(2). Service页面
public class BookService {
private BookDao bookDao=new BookDao();
public List
return bookDao.findAll();
}
}
(3). Dao页面
public class BookDao {
private QueryRunner qr=new QueryRunner();
public List
try{
String sql="select *from book";
return qr.query(sql, new BeanListHandler
}catch(Exception e){
throw new RuntimeException(e);
}
}
public void add(Book book) {
try {
String sql="insert into book values (?,?,?,?,?,?)";
Object[] params= {book.getBid(), book.getBname(), book.getPrice(),
book.getAuthor(), book.getImage(), book.getCategory().getCid()};
qr.update(sql, params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
(4). JSP页面常用c标签
<fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${order.ordertime }"/> //时间格式化