好吧我知道,应该很少人一开始学网站开发就从MVC开始,但如果你已经理解了三层架构之类的,那直接尝试强大的微软MVC网站开发模式也是挺不错的。

但其实我们学校有个实验室,那些干进去的就算是大一的学生,也是直接开始使用鲁比语言(我忘了英文怎么拼了~)的MVC模式开发网站,而且是可以真实部署到客户厂家进行使用的。

下面开始介绍MVC模式。

MVC,即Model模型、View视图、Controller控制器

用户发送请求到Controller,Controller将请求规范化交给Model处理,Model调用(可以通过BLL、DAL)数据库信息,得到数据后以“表”的形式返回给Controller,Controller将表信息交给View处理,View将其“美化”展示给用户。

其实要用文字来表达什么是MVC可能理解起来是有的麻烦的,比较得自己多找点资料吧。

这里我再贴出JAVA课写出来的JAVA  MVC  代码,不拖一个空间就可以实现该效果:

文件有:

代码如下:

Model

package mvccalculator;

// The Model performs all the calculations needed
// and that is it. It doesn't know the View
// exists public class CalculatorModel { // Holds the value of the sum of the numbers
// entered in the view private int calculationValue; public void addTwoNumbers(int firstNumber, int secondNumber){ calculationValue = firstNumber + secondNumber; } public void subTwoNumbers(int firstNumber, int secondNumber){ calculationValue = firstNumber - secondNumber; } public void mulTwoNumbers(int firstNumber, int secondNumber){ calculationValue = firstNumber * secondNumber; } public void divTwoNumbers(int firstNumber, int secondNumber){ calculationValue = firstNumber / secondNumber; } public int getCalculationValue(){ return calculationValue; } }

View

package mvccalculator;

// This is the View
// Its only job is to display what the user sees
// It performs no calculations, but instead passes
// information entered by the user to whomever needs
// it. import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList; import javax.swing.*; public class CalculatorView extends JFrame{ private ArrayList<JTextField> firstNumber = new ArrayList<JTextField>();
private ArrayList<JLabel> additionLabel = new ArrayList<JLabel>();
private ArrayList<JTextField> secondNumber = new ArrayList<JTextField>();
private ArrayList<JButton> calculateButton = new ArrayList<JButton>();
private ArrayList<JTextField> calcSolution = new ArrayList<JTextField>(); CalculatorView(){ // Sets up the view and adds the components ArrayList<JPanel> calcPanel = new ArrayList<JPanel>(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 200);
this.setLayout(new GridLayout(4,1,0,0));
String[] str = {"+", "-", "*", "/"};
for(int i = 0;i < 4;i++)
{
firstNumber.add(new JTextField(10));
additionLabel.add(new JLabel(str[i]));
secondNumber.add(new JTextField(10));
calculateButton.add(new JButton("Calculate"));
calcSolution.add(new JTextField(10)); calcPanel.add(new JPanel()); calcPanel.get(i).add(firstNumber.get(i));
calcPanel.get(i).add(additionLabel.get(i));
calcPanel.get(i).add(secondNumber.get(i));
calcPanel.get(i).add(calculateButton.get(i));
calcPanel.get(i).add(calcSolution.get(i)); this.add(calcPanel.get(i),i);
} // End of setting up the components -------- } public int getFirstNumber(int i){ return Integer.parseInt(firstNumber.get(i).getText()); } public int getSecondNumber(int i){ return Integer.parseInt(secondNumber.get(i).getText()); } public int getCalcSolution(int i){ return Integer.parseInt(calcSolution.get(i).getText()); } public void setCalcSolution(int i, int solution){ calcSolution.get(i).setText(Integer.toString(solution)); } // If the calculateButton is clicked execute a method
// in the Controller named actionPerformed void addCalculateListener(int i, ActionListener listenForCalcButton){ calculateButton.get(i).addActionListener(listenForCalcButton); } // Open a popup that contains the error message passed void displayErrorMessage(String errorMessage){ JOptionPane.showMessageDialog(this, errorMessage); } }

Control

package mvccalculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; // The Controller coordinates interactions
// between the View and Model public class CalculatorController { private CalculatorView theView;
private CalculatorModel theModel; public CalculatorController(CalculatorView theView, CalculatorModel theModel) {
this.theView = theView;
this.theModel = theModel; // Tell the View that when ever the calculate button
// is clicked to execute the actionPerformed method
// in the CalculateListener inner class
for(int i = 0;i < 4;i++)
{
this.theView.addCalculateListener(i, new CalculateListener(i));
}
} class CalculateListener implements ActionListener{
int index = -1;
private CalculateListener(int i) {
index = i;
} public void actionPerformed(ActionEvent e) { int firstNumber = 0;
int secondNumber = 0; // Surround interactions with the view with
// a try block in case numbers weren't
// properly entered try{ firstNumber = theView.getFirstNumber(index);
secondNumber = theView.getSecondNumber(index);
if(index == 0)
{
theModel.addTwoNumbers(firstNumber, secondNumber);
}
else if(index == 1)
{
theModel.subTwoNumbers(firstNumber, secondNumber);
}
else if(index == 2)
{
theModel.mulTwoNumbers(firstNumber, secondNumber);
}
else if(index == 3)
{
theModel.divTwoNumbers(firstNumber, secondNumber);
} theView.setCalcSolution(index, theModel.getCalculationValue()); } catch(NumberFormatException ex){ System.out.println(ex); theView.displayErrorMessage("You Need to Enter 2 Integers"); } } } }

主类

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mvccalculator; /**
*
* @author Administrator
*/
public class MVCCalculator { /**
* @param args the command line arguments
*/
public static void main(String[] args) { CalculatorView theView = new CalculatorView(); CalculatorModel theModel = new CalculatorModel(); CalculatorController theController = new CalculatorController(theView,theModel); theView.setVisible(true); }
}

.net网站开发(设计):1.什么是MVC模式的更多相关文章

  1. PHP-购物网站开发设计(一)

    2015-07-6 开始使用PHP完成简单购物网站的设计,首先要选择合适的软件平台,所以今天先记录平台的选择与搭建: 我选择使用Apache24 + PHP 5.6 + MySQL 开发环境完成PHP ...

  2. Web开发的分层结构与MVC模式

    1.分层结构 所谓分层结构.把不同的功能代码封装成类,把相同功能的类封装在一个个的包中,也叫层.功能归类如下: 实体类: 封装数据,是数据的载体,在层与层之间进行传递,数据也就传递了.比如说要传递学生 ...

  3. PHP-购物网站开发设计(二)

    2015-07-7 今天介绍购物网站的后台数据库设计,数据库使用的是MySQL (1)在MySQL数据库中新建Database,命名为test (2)在test下新建三个数据表,分别为mismatch ...

  4. 【转】用java实例学习MVC模式

    .1 MVC模式 MVC是三个单词的缩写,这三个单词分别为:模型(Model).视图(View)和控制(Controller).MVC模式的目的就是实现Web系统的职能分工.下面以J2EE开发进行介绍 ...

  5. MVC模式学习笔记

    一.如何设计一个程序的结构,这是一门专门的学问,叫做“架构模式”(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,我觉得它不仅适用于开发软件,也适用于其 ...

  6. MVC模式浅谈

    MVC模式浅谈 一.MVC模式概述 模型-视图-控制器(MVC模式)是一种非常经典的软件架构模式,在UI框架和UI设计思路中扮演着非常重要的角色.从设计模式的角度来看,MVC模式是 一种复合模式,它将 ...

  7. 【架构】MVC模式

    架构模式 如何设计一个程序的结构,这是一门专门的学问,叫做"架构模式"(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,它不仅适用于 ...

  8. struts2的MVC模式

    MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而得到更好的开发和维护效率. 在MVC模式中,应用程序被划分成了模型(Model).视 ...

  9. 什么是MVC模式?   

    MVC (Model View Controller) 是一个设计模式,使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.M是指数据模型,V是指用户界面,C则是控制器. ...

  10. mvc 模式和mtc 模式的区别

    首先说说Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负责业务对象与数据库的映射( ...

随机推荐

  1. 修改UISearchBar背景色

    //iOS7.1 [[[[_searchBar.subviews objectAtIndex:] subviews] objectAtIndex:] removeFromSuperview]; [_s ...

  2. springMVC3学习(六)--SimpleFormController

    SimpleFormController提交表单流程例如以下: login.jsp <form action="login" method="post"& ...

  3. Android应用程序消息处理机制(Looper、Handler)分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6817933 Android应用程序是通过消息来 ...

  4. Android系统进程Zygote启动过程的源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6768304 在Android系统中,所有的应用 ...

  5. SQLLoader1(简单测试,以控制文件方式导入数据)

    1.创建表:SQL> conn scott/tiger@orcl;已连接. SQL> CREATE TABLE EMP2 AS SELECT * FROM EMP WHERE 1=2; 表 ...

  6. xtrabackup备份恢复测试

    http://blog.chinaunix.net/uid-20682026-id-3319204.html

  7. C# 仿百度自动匹配

    private void Form1_Load(object sender, EventArgs e) { AutoCompleteStringCollection source = new Auto ...

  8. OD调试1--第一个win32程序

    OD调试一:第一个Win32程序的修改 在软件开发的过程中,程序员会使用一些调试工具,以便高效地找出软件中存在的错误.而在逆向分析领域,分析者也会利用相关的调试工具来分析软件的行为并验证分析结果.由于 ...

  9. Python3.5入门学习记录-列表、元组、字典

    1.列表 python列表的定义使用[] list = [1,2,3,4,5] #创建一个心列表list 获取列表中的值 first = list[0] #list中第一个值 last = list[ ...

  10. .Net平台下ActiveMQ入门实例(转)

    1.ActiveMQ简介 先分析这么一个场景:当我们在网站上购物时,必须经过,下订单.发票创建.付款处理.订单履行.航运等.但是,当用户下单后,立即跳转到"感谢那您的订单" 页面. ...