当前位置: 代码迷 >> 综合 >> java_web 快速入门之第十九章 MVC
  详细解决方案

java_web 快速入门之第十九章 MVC

热度:33   发布时间:2023-12-05 09:21:22.0

          MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。
 

模型层

    该层是应用系统的核心层,主要负责封装数据和业务操作。使用JavaBean实现

视图层

   该层主要是指与用户交互的界面,即应用程序的外观。使用HTML,JSP等等实现。

控制层

    该层主要的工作是控制整个系统处理的流程,该角色通常介于视图层和模型层之间,进行数据传递和流程转向。使用Servlet实现。

web开发模式发展:模型1 模型2

完全使用jsp开发

使用这种开发模式,主要是开发小的项目,开发时间短,但是代码的可读性差,重用度较低维护较难。

使用jsp+JavaBean开发

          这种模式是在第一代模式上的创新,使得代码的可读性,重用度等增高。但是缺少了MVC模式中控制层去控制相关流程,在jsp中将视图层与控制层耦合在一起,违背了高内聚和低耦合标准   

Model2模式

 

  MVC也是一种开发架构设计模式,与三层架构类似。

      MVC其实就是将三层架构中的显示层"一分为二",将三层架构中的业务逻辑层与数据访问层进行合并成"model"。
      MVC是一个大的概念,三层架构其实就是MVC的具体的实现的一种设计模式。

案例:基于mvc的购物车登录与首页数据显示

            login.jsp登录页面

?
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录界面</title>
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
form {width: 500px;margin:auto;
}
</style>
</head>
<body><form action="LoginServlet" method="post"><h1>登录</h1><div class="form-group"><input name="uname" class="form-control" placeholder="用户名"></div><div class="form-group">地址: <input name="uaddress" class="form-control" placeholder="地址"></div><div class="form-group"><button class="btn btn-primary btn-block">登录</button></div></form>
</body>
</html>?

            登录的servlet操作

?
package com.zking.cart.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.zking.cart.biz.UserDaoBiz;
import com.zking.cart.biz.impl.UserDaoBizImpl;
import com.zking.cart.entity.Orderitem;
import com.zking.cart.entity.User;/*** 登录servlet类*/
// 配置文件
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取数据// 用户名String uname = request.getParameter("uname");// 地址String uaddress = request.getParameter("uaddress");// 封装数据User user = new User(uname, uaddress);// 实例化业务逻辑处理层UserDaoBiz ud = new UserDaoBizImpl();// 调用方法User login = ud.userLogin(user);// out对象PrintWriter out = response.getWriter();// session对象HttpSession session = request.getSession();// 判断if (login != null) {out.println("<script>alert('登录成功')</script>");session.setAttribute("login", login);//生成一个篮子 List<Orderitem> list=new ArrayList<Orderitem>();//将篮子给到当前的用户request.getSession().setAttribute("gs", list);response.sendRedirect("index.jsp");} else {out.println("<script>alert('登录失败')</script>");response.sendRedirect("login.jsp");}}}?

               index.jsp首页页面

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>商品首页</title>
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
body {padding: 20px 40px;
}
</style></head>
<body><c:if test="${empty all }"><jsp:forward page="loadServlet.do"></jsp:forward></c:if><h1>您好, <small>这是首页</small></h1><h1><button class="btn btn-primary" onclick="location.href='myCar.jsp'">点我去购物车?</button></h1><table class="table table-bordered table-striped"><tr><th>商品编号</th><th>商品价格</th><th>商品名称</th><th>商品操作</th></tr><c:forEach items="${all}" var="goods"><tr><td>${ goods.gid }</td><td>${ goods.gname }</td><td>${ goods.gprice }</td><td><buttononclick="location.href='${pageContext.request.contextPath }/AddCarServlet?gid=${ goods.gid }'"class="btn btn-default">加入?</button></td></tr></c:forEach></table>
</body>
</html>

             首页servlet数据显示操作

?
package com.zking.cart.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.zking.cart.biz.GoodsDaoBiz;
import com.zking.cart.biz.impl.GoodsDaoBizImpl;
import com.zking.cart.entity.Goods;/*** 首页数据加载servlet类*/
//配置文件
public class LoadServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println(123);response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");//实例化业务逻辑处理层GoodsDaoBiz gd=new GoodsDaoBizImpl();//调用查询所有数据的方法List<Goods> all = gd.selectGoodsAll();System.out.println(all);//获取session对象HttpSession session = request.getSession();//设置数据session.setAttribute("all", all);//跳转到首页response.sendRedirect("index.jsp");}}?