通讯录改造——MVC设计模式

将之前用servlet写的程序转化为jsp+servlet的简单的MVC的三层结构。项目中程序的包如图



首先是实体对象:

package com.contactSystem.entiey;

public class Contact {
	private String Id;
	private String name;
	private String sex;
	private String age;
	private String phone;
	private String qq;
	private String email;
	public String getId() {
		return Id;
	}
	public void setId(String id) {
		Id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "Contact [Id=" + Id + ", name=" + name + ", sex=" + sex
				+ ", age=" + age + ", phone=" + phone + ", qq=" + qq
				+ ", email=" + email + "]";
	}

}

然后就是对数据操作的抽象类

package com.contactSystem.dao;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.DocumentException;
import com.contactSystem.entiey.Contact;

public interface ContactOperate {
	public void addContact(Contact contact) throws Exception;
	public void updateContact(Contact contact) throws Exception;
	public void removeContact(String id) throws Exception;
	public Contact findContact(String id) throws Exception;
	public List<Contact>  allContacts();
	//根据名称姓名查询是否有存在重复。
	public boolean checkIfContact(String name);
}

具体实现类

package com.contactSystem.dao.daoImpl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.persistence.Id;
import javax.persistence.Tuple;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.contactSystem.dao.ContactOperate;
import com.contactSystem.entiey.Contact;
import com.contactSystem.util.XMLUtil;

public class Operater implements ContactOperate {

	@Override
	//添加联系人
	public void addContact(Contact contact) throws Exception {
		// TODO Auto-generated method stub
		File file=new File("e:/contact.xml");
		Document doc=null;
		Element rootElem=null;
		if (file.exists()) {
			doc=new SAXReader().read(file);
			rootElem=doc.getRootElement();
		}else {
			doc=DocumentHelper.createDocument();
			rootElem=doc.addElement("ContactList");
		}

		//开始添加个体
		Element element=rootElem.addElement("contact");
		//有系统自动生成一随机且唯一的ID,赋给联系人Id,系统提供了一个包UUID包
		String uuid=UUID.randomUUID().toString().replace("-", "");

		element.addAttribute("Id", uuid);
		element.addElement("姓名").setText(contact.getName());
		element.addElement("name").setText(contact.getName());
		element.addElement("sex").setText(contact.getSex());
		element.addElement("age").setText(contact.getAge());
		element.addElement("phone").setText(contact.getPhone());
		element.addElement("email").setText(contact.getEmail());
		element.addElement("qq").setText(contact.getQq());

		//写入到本地的xml文档中
		XMLUtil.write2xml(doc);

	}

	@Override
	public void updateContact(Contact contact) throws Exception {
		// TODO Auto-generated method stub
		//通过xpath查找对应id的联系人
		Document document=XMLUtil.getDocument();
		Element element=(Element) document.selectSingleNode("//contact[@Id=‘"+contact.getId()+"‘]");
		//根据标签改文本
		System.out.println(element);
		element.element("name").setText(contact.getName());
		element.element("age").setText(contact.getAge());
		element.element("email").setText(contact.getEmail());
		element.element("phone").setText(contact.getPhone());
		element.element("sex").setText(contact.getSex());
		element.element("qq").setText(contact.getQq());
		XMLUtil.write2xml(document);

	}

	@Override
	public void removeContact(String id) throws Exception {
		// TODO Auto-generated method stub
		//通过xpath去查找对应的contact
		Document document=XMLUtil.getDocument();
		Element element=(Element) document.selectSingleNode("//contact[@Id=‘"+id+"‘]");
		/**
		 * 如果是火狐浏览器,其本身有一个bug,对于一个get请求,它会重复做两次,
		 * 第一次会把一个唯一的id给删除掉,但是在第二次的时候,它会继续去删,而这个时候查找不到,会报错,会发生错误,
		 * 为了解决这个bug,我们在这里验证其是否为空
		 */
		if (element!=null) {
			element.detach();
			XMLUtil.write2xml(document);
		}
	}

	@Override
	public Contact findContact(String id) throws Exception {
		// TODO Auto-generated method stub
		Document document=XMLUtil.getDocument();
		Element e=(Element) document.selectSingleNode("//contact[@Id=‘"+id+"‘]");

		Contact contact=null;
		if (e!=null) {
			contact=new Contact();
			contact.setAge(e.elementText("age"));
			contact.setEmail(e.elementText("email"));
			contact.setId(id);
			contact.setName(e.elementText("name"));
			contact.setPhone(e.elementText("phone"));
			contact.setSex(e.elementText("sex"));
			contact.setQq(e.elementText("qq"));
		}
		return contact;
	}

	@Override
	public List<Contact> allContacts() {
		// TODO Auto-generated method stub
		Document document=XMLUtil.getDocument();
		List<Contact> list=new ArrayList<Contact>();
		List<Element> conElements=(List<Element>)document.selectNodes("//contact");
		for (Element element : conElements) {
			Contact contact=new Contact();
			contact.setId(element.attributeValue("Id"));
			contact.setAge(element.elementText("age"));
			contact.setEmail(element.elementText("email"));
			contact.setName(element.elementText("name"));
			contact.setPhone(element.elementText("phone"));
			contact.setQq(element.elementText("qq"));
			contact.setSex(element.elementText("sex"));
			list.add(contact);
		}
		return list;
	}
	/**
	 * true:说明重复
	 * false:说明没有重复
	 */
	@Override
	public boolean checkIfContact(String name) {
		// TODO Auto-generated method stub
		//查询name标签是否一样
		Document doc=XMLUtil.getDocument();
		Element element=(Element) doc.selectSingleNode("//name[text()=‘"+name+"‘]");
		if (element!=null) {
			return true;
		}else {
			return false;
		}

	}

}

为了减轻Servlet的负担在增加一层(业务逻辑层)Service,这里举例,当联系人的名字存在时,提示出错,不在servlet中去判断,而在service中去处理,同样首先写出service抽象接口

package com.contactSystem.service;

import java.util.List;

import com.contactSystem.entiey.Contact;

public interface ContactService {
	public void addContact(Contact contact) throws Exception;
	public void updateContact(Contact contact) throws Exception;
	public void removeContact(String id) throws Exception;
	public Contact findContact(String id) throws Exception;
	public List<Contact>  allContacts();
}

这个时候有点相当于在Operate操作中添加了一层操作,在contactService的实现类中去有逻辑判断的去使用Operater的方法

package com.contactSystem.service.imple;

import java.util.List;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.entiey.Contact;
import com.contactSystem.exception.NameRepeatException;
import com.contactSystem.service.ContactService;
/**
 * 处理项目中出现的业务逻辑
 * @author Administrator
 *
 */
public class ContactServiceImpe implements ContactService {
	Operater operater =new Operater();
	@Override
	public void addContact(Contact contact) throws Exception {
		// TODO Auto-generated method stub
		//执行业务逻辑判断
		/**
		 * 添加是否重复的业务逻辑
		 */
		if (operater.checkIfContact(contact.getName())) {
			//重复
			/**
			 * 注意:如果业务层方法出现任何错误,则返回标记(自定义的异常)到servlet
			 */
			 throw new NameRepeatException("姓名重复,不可使用");
		}
		operater.addContact(contact);

	}

	@Override
	public void updateContact(Contact contact) throws Exception {
		// TODO Auto-generated method stub
		operater.updateContact(contact);
	}

	@Override
	public void removeContact(String id) throws Exception {
		// TODO Auto-generated method stub
		operater.removeContact(id);
	}

	@Override
	public Contact findContact(String id) throws Exception {
		// TODO Auto-generated method stub
		return operater.findContact(id);
	}

	@Override
	public List<Contact> allContacts() {
		// TODO Auto-generated method stub
		return operater.allContacts();
	}

}

这里通过抛出异常去将信息传递给Servlet,异常代码如下

package com.contactSystem.exception;
/**
 * 姓名重复的自定义异常
 * @author Administrator
 *
 */
public class NameRepeatException extends Exception{
	public NameRepeatException(String msg) {
		super(msg);
	}
}

现在来写Servlet,首先是首页的联系人列表Servlet

package com.contactSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.entiey.Contact;
import com.contactSystem.service.ContactService;
import com.contactSystem.service.imple.ContactServiceImpe;

public class Index extends HttpServlet {

	/**
	 * 显示所有联系人的逻辑方式
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		ContactService service=new ContactServiceImpe();
		List<Contact> contacts=service.allContacts();

		/**
		 * shift+alt+A ——>区域选择
		 * 正则表达式:“."表示任意字符,"*"表示多个字符
		 *             “/1”表示一行代表匹配一行内容
		 */

		request.setAttribute("contacts", contacts);
		request.getRequestDispatcher("/index.jsp").forward(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

添加联系人

package com.contactSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.entiey.Contact;
import com.contactSystem.exception.NameRepeatException;
import com.contactSystem.service.ContactService;
import com.contactSystem.service.imple.ContactServiceImpe;

public class addServlet extends HttpServlet {

	/**
	 * 处理添加联系人的逻辑
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String userName=request.getParameter("userName");
		String age=request.getParameter("age");
		String sex=request.getParameter("sex");
		String phone=request.getParameter("phone");
		String qq=request.getParameter("qq");
		String email=request.getParameter("email");

		ContactService service=new ContactServiceImpe();

		Contact contact=new Contact();
		contact.setName(userName);
		contact.setAge(age);
		contact.setSex(sex);
		contact.setPhone(phone);
		contact.setQq(qq);
		contact.setEmail(email);
		try {
			service.addContact(contact);
		} catch (NameRepeatException e) {
			// TODO Auto-generated catch block
			//处理名字重复的异常
			request.setAttribute("message", e.getMessage());
			request.getRequestDispatcher("/add.jsp").forward(request, response);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.sendRedirect(request.getContextPath()+"/Index");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

查找联系人

package com.contactSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.entiey.Contact;
import com.contactSystem.service.ContactService;
import com.contactSystem.service.imple.ContactServiceImpe;

public class FindIdServlet extends HttpServlet {

	/**
	 * 修改联系人逻辑
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");

		ContactService service=new ContactServiceImpe();

		String id=request.getParameter("id");

		Contact contact=null;
		try {
			contact=service.findContact(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.setAttribute("contact", contact);
		request.getRequestDispatcher("/update.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

修改联系人

package com.contactSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.entiey.Contact;
import com.contactSystem.service.ContactService;
import com.contactSystem.service.imple.ContactServiceImpe;

public class UpdateServlet extends HttpServlet {

	/**
	 * 将修改后的数据提交
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");

		request.setCharacterEncoding("utf-8");
		String userName=request.getParameter("userName");
		String age=request.getParameter("age");
		String sex=request.getParameter("sex");
		String phone=request.getParameter("phone");
		String qq=request.getParameter("qq");
		String email=request.getParameter("email");
		String id=request.getParameter("id");
		ContactService service=new ContactServiceImpe();
		Contact contact=new Contact();
		contact.setId(id);
		contact.setName(userName);
		contact.setAge(age);
		contact.setSex(sex);
		contact.setPhone(phone);
		contact.setQq(qq);
		contact.setEmail(email);
		try {
			service.updateContact(contact);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.sendRedirect(request.getContextPath()+"/Index");

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

删除联系人

package com.contactSystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.service.ContactService;
import com.contactSystem.service.imple.ContactServiceImpe;

public class DeleteServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");

		String id=request.getParameter("id");
		ContactService service=new ContactServiceImpe();
		try {
			service.removeContact(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		response.sendRedirect(request.getContextPath()+"/Index");

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
}

以上完成了MVC的M和C,还差View,现在来具体显示jsp页面

首页所有联系人的显示

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset=‘utf-8‘>
    <title>查询所有联系人</title>
    <style media=‘screen‘>
        table td {
            text-align: center;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>

<body>
    <center>
        <h2>查询所有联系人</h2>
    </center>
    <table border=‘1‘ align=‘center‘>
        <tbody>
            <thead>
                <th>编号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>电话</th>
                <th>QQ</th>
                <th>邮箱</th>
                <th>操作</th>
            </thead>
           <c:forEach items="${contacts}" var="con"  varStatus="varSta">
            <tr>
                <td>${varSta.count }</td>
                <td>${con.name }</td>
                <td>${con.sex }</td>
                <td>${con.age }</td>
                <td>${con.phone }</td>
                <td>${con.qq }</td>
                <td>${con.email }</td>
                <td><a href=‘${pageContext.request.contextPath }/FindIdServlet?id=${con.id}‘>修改</a>  <a href=‘${pageContext.request.contextPath }/DeleteServlet?id=${con.id}‘>删除</a></td>
            </tr>
            </c:forEach>
            <tr>
                <td colspan=‘8‘>
                    <a href=‘${pageContext.request.contextPath}/add.jsp‘>添加联系人</a>
                </td>
            </tr>
        </tbody>
    </table>
</body>

</html>

  

修改联系人,首先要把要修改联系人的信息都显示在信息栏中,然后用户去修改相关信息

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>修改联系人</title>
    <style media="screen">
      #btn{
        width:40px;
        width: 50px;
        background: green;
        color: white;
        font-size:14px;
      }
    </style>
</head>

<body>
    <center>
        <h2>修改联系人</h2>
    </center>
    <form action="${pageContext.request.contextPath }/UpdateServlet" method="post">
        <table border="1" align="center">
            <tbody>

                <tr>
                    <th>姓名</th>
                    <td><input type="text" name="userName" value="${contact.name}"/></td>
                    <input type="hidden" name="id" value="${contact.id }"/>

                </tr>
                <tr>
                    <th>年龄</th>
                    <td><input type="text" name="age" value="${contact.age }" /></td>
                </tr>
                <tr>
                  <th>性别</th>
                  <td>

                    <input type="radio" name="sex"  value="男" <c:if test="${contact.sex==‘男‘ }"> checked="true"</c:if>/>男  
                    <input type="radio" name="sex" value="女" <c:if test="${contact.sex==‘女‘ }"> checked="true"</c:if> />女
                  </td>
                </tr>
                <tr>
                    <th>电话</th>
                    <td><input type="text" name="phone" value="${contact.phone }" /></td>
                </tr>
                <tr>
                    <th>QQ</th>
                    <td><input type="text" name="qq" value="${contact.qq }" /></td>
                </tr>
                <tr>
                    <th>邮箱</th>
                    <td><input type="text" name="email" value="${contact.email }" /></td>
                </tr>
                <tr>
                  <td colspan="3" align="center">
                    <input type="submit"  value="提交" id="btn"/>
                  </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>

</html>

  

最后就是添加联系人了

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加联系人</title>
    <style media="screen">
      #btn{
        width:40px;
        width: 50px;
        background: green;
        color: white;
        font-size:14px;
      }
    </style>
</head>

<body>
    <center>
        <h2>添加联系人</h2>
    </center>
    <form action="${pageContext.request.contextPath}/addServlet" method="post">
        <table border="1" align="center">
            <tbody>

                <tr>
                    <th>姓名</th>
                    <td><input type="text" name="userName" value="${message }" /></td>
                </tr>
                <tr>
                    <th>年龄</th>
                    <td><input type="text" name="age" /></td>
                </tr>
                <tr>
                  <th>性别</th>
                  <td>
                    <input type="radio" name="sex"  value="男"/>男  
                    <input type="radio" name="sex" value="女" />女
                  </td>
                </tr>
                <tr>
                    <th>电话</th>
                    <td><input type="text" name="phone" /></td>
                </tr>
                <tr>
                    <th>QQ</th>
                    <td><input type="text" name="qq" /></td>
                </tr>
                <tr>
                    <th>邮箱</th>
                    <td><input type="text" name="email" /></td>
                </tr>
                <tr>
                  <td colspan="3" align="center">
                    <input type="submit"  value="提交" id="btn"/>
                  </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>

</html>

最后可以加上一个测试类,方便自己调试

package com.contactSystem.Junit;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Before;

import com.contactSystem.dao.daoImpl.Operater;
import com.contactSystem.entiey.Contact;

public class Test {
Operater operator=null;

	//初始化这个对象的实例
	@Before
	public void init(){
		 operator=new Operater();
	}

	@org.junit.Test
	public void Add() throws Exception{
		Contact contact=new  Contact();

		contact.setAge("21");
		contact.setEmail("[email protected]");
		contact.setName("gqxing");
		contact.setPhone("13455555");
		contact.setQq("235346662");
		contact.setSex("男");
		operator.addContact(contact);
	}

	@org.junit.Test
	public void update() throws Exception{
		Contact contact=new  Contact();
		contact.setId("002");
		contact.setAge("0");
		contact.setEmail("[email protected]");
		contact.setName("test");
		contact.setPhone("0-00000000000");
		contact.setQq("000000000000");
		contact.setSex("男");
		operator.updateContact(contact);
	}

	@org.junit.Test
	public void removeContact() throws Exception{
		operator.removeContact("002");
	}

	@org.junit.Test
	public void findContact() throws Exception{
		Contact contact=operator.findContact("002");
		System.out.println(contact);
	}

	@org.junit.Test
	public void allContact() throws Exception{
		List<Contact> contacts= operator.allContacts();
		for (Contact contact : contacts) {
			System.out.println(contact);
		}
	}
	@org.junit.Test
	public void TetsCheckBoolean() throws Exception{
		Boolean flag=operator.checkIfContact("郭庆兴");
		System.out.println(flag);
	}

}

最后运行的结构示意图:

当名字重复的时候:提交就会显示

时间: 2024-12-14 10:30:09

通讯录改造——MVC设计模式的相关文章

python mvc设计模式(一)

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

Java Web开发中MVC设计模式简介

一.有关Java Web与MVC设计模式 学习过基本Java Web开发的人都已经了解了如何编写基本的Servlet,如何编写jsp及如何更新浏览器中显示的内容.但是我们之前自己编写的应用一般存在无条理性,对于一个小型的网站这样的编写没有任何问题,但是一但我们需要编写大型的web工程的话,我们现有的编写模式会造成web应用的可扩展性较差,而且一但出现问题不能准确的定位出问题出在哪里. Java是一门应用设计模式比较广泛的语言.目前主流提出的23种设计模式均可在Java语言编写的程序中所应用.目前

转---MVC设计模式详解

MVC(Model View Controller)模型(model)-视图(view)-控制器(controller):MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式.比如一批统计数据你可以分别用柱状图.饼图来表示.C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新,从例子可以看出MVC就是Observer设计模式的一个特例.MVC如何工作MVC是一个设计模式,它强制性

【原创翻译】认识MVC设计模式:web应用开放的基础(基础篇)

原文地址:http://www.larryullman.com/2009/10/08/understanding-mvc/ 翻译:shadowmydx 转帖请注明 最近,我计划写一个系列关于自己在过去几个月使用的Yii框架(shadowmydx:基于PHP5的一个web开发 框架,详情自行google)的文章.但在一切开始以前,我认为首先还是需要先介绍一下MVC设计模式: 模型-视图-控制器.MVC模式(30年前就有鸟)已经成为了框架以及许多各式各样的应用的首选.MVC模 式主要着眼于分离应用的

MVC设计模式在网站中的应用

MVC设计模式在网站中的应用 以淘宝的购物车为例 一.结合六个基本质量属性 可修改性 采用MVC设计模式的时候,可以将视图.模型.控制器分析,将用户动作.数据表示.应用数据分离开来,如果用户需要以不同的视图来展示,只需要修改视图中的代码即可,对于模型和控制器的代码,则不需要做改动,即可满足要求,使得对代码的修改非常方便. 易用性 用户可以直接在商品展示界面中将商品添加进购物车,直接点开购物车就可以看到已经添加的商品:删除某一件商品时,也可以直接在商品的后面点击删除,确认之后,即可删除. 安全性

如何理解MVC设计模式

MVC是一种架构模式 MVC(Model View Controller) M-模型(model) V-视图(view) C-控制器(controller): MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式.比如一批统计数据你可以分别用柱状图.饼图来表示.C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新,从例子可以看出MVC就是Observer设计模式的一个特例. MV

ASP.NET下MVC设计模式的实现

[转载]MVC架构在Asp.net中的应用和实现 转载自:http://www.cnblogs.com/baiye7223725/archive/2007/06/07/775390.aspx 摘要:本文主要论述了MVC架构的原理.优缺点以及MVC所能为Web应用带来的好处.并以“成都市信息化资产管理系统”框架设计为例,详细介绍其在Asp.net环境下的具体实现.旨在帮助Web设计开发者更好的了解和掌握MVC,合理利用MVC构建优秀的Web应用. 关键字:MVC.视图.控制器.模型.Asp.net

MVC设计模式与三层架构

三层架构分别是:表示层(Web层).业务逻辑层(BLL层)和数据访问层(DAL层). (1)表示层负责: a.从用户端收集信息 b.将用户信息发送到业务服务层做处理 c.从业务服务层接收处理结果 d.将结果显示给用户 (2)业务逻辑层负责: a.从表示层接收输入 b.与数据层交互执行已设计的业务 c.操作(业务逻辑,系统服务等) d.将处理结果发送到表示层 (3)数据访问层负责: a.数据存储 b.数据获取 c.数据维护 d.数据完整性 在三层结构设计中,表示层可由视图和控制器来实现,而业务逻辑

程序开发:MVC设计模式与应用

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑.MVC被独特的发展起来用于映射传统的输入.处理和输出功能在一个逻辑的图形化用户界面的结构中. 下面讲解简单的登录操作: 程序流程图: 在本程序中用户输入的登陆信息提交给Servlet进行接收,Servle