好吧我知道,应该很少人一开始学网站开发就从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. 【软件技巧】Sublime Text为不同语法定义不同高亮

    Sublime Text默认的语法高亮已经非常美丽了,可是对于个别语言还是有些不爽. 默认高亮规则叫Monokai,能够从Preferences->Settings - Default中看到: ...

  2. ajaxFileUpload SyntaxError: syntax error

    在使用ajaxFileUpload上传文件时,Chrome没问题,IE和Firefox出错,Firefox报SyntaxError: syntax error错误 JS代码例如以下: $.ajaxFi ...

  3. E514:write error(file system full?)

    vi编辑某文件,保存时报错,提示:E514: write error (file system full?)---写入错误,磁盘满了? 查看磁盘空间:df -h根目录磁盘空间已满,used%100. ...

  4. 写一个简易web服务器、ASP.NET核心知识(4)--转载

    第一次尝试(V1.0) 1.理论支持 这里主要要说的关于Socket方面的.主要是一个例子,关于Socket如何建立服务端程序的简单的代码. static void Main(string[] arg ...

  5. SQL按汉语拼音首字母排序

    以常用到的省的数据表(province)为例,其中name字段为省的名称,SQL语句如下: ))) as py ,a.name from province a left outer join ( se ...

  6. (转) Functions

    Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...

  7. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  8. [翻译] C++ STL容器参考手册(第一章 <array>)

    返回总册 本章节原文:http://www.cplusplus.com/reference/array/array/ 1. std::array (C++11支持) template < cla ...

  9. [ofbiz]设置任务计划(job),提示service_item已经传递

    问题描述:设置任务计划(job),提示service_item已经传递 解决办法: 红色框内不要填写,就可以了."已经传递"是翻译的不准确,应该是"已过时",所 ...

  10. Application.DoEvents()

    今天,在按照书上的一个winform例子做练习(实则就是边看边抄了一遍...)的时候,看到Application.DoEvents()这个方法,但是并不明白这个方法是做什么用的.只知道不用这个方法的话 ...