好吧我知道,应该很少人一开始学网站开发就从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. 利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(1)

    数据分析和建模方面的大量编程工作都是用在数据准备上的:载入.清理.转换以及重塑.有时候,存放在文件或数据库中的数据并不能满足你的数据处理应用的要求.很多人都选择使用通用编程语言(如Python.Per ...

  2. javascript无缝全屏轮播

    虽然平时能利用插件来实现,但是总是觉得,如果连个无缝轮播都写不出来,还玩个毛线: 其实现在还真的是玩毛线,因为代码都是别人的,不过嘛,很快就变成是我的啦! 代码还没封装成插件,其实我也还没弄清楚. 下 ...

  3. SQL SERVER 自定义函数 split

    Create function [dbo].[split] ( ), ) ) )) as begin declare @i int set @SourceSql=rtrim(ltrim(@Source ...

  4. asp.net断点续传技术---下载(转)

    断点续传的原理 在了解HTTP断点续传的原理之前,先来说说HTTP协议,HTTP协议是一种基于tcp的简单协议,分为请求和回复两种.请求协议是由客户机(浏览器)向服务器(WEB SERVER)提交请求 ...

  5. 公司项目笔记-导出excel

    一.asp.net中导出Excel的方法: 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览 ...

  6. spring cuowu

    spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...

  7. (转) Friendship and inheritance

    原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...

  8. [music]&lt;蔷薇&gt;

    我在虾米音乐听到一首好听的歌曲<蔷薇>,一起来听吧!http://www.xiami.com/song/386109?ref=aother LOFTER:我们的故事   http://us ...

  9. win 下python2.7 pymssql连接ms sqlserver 2000

    python DB-API 连接mysql 要用到库pymssql 下载请到https://pypi.python.org/pypi/pymssql/2.0.1我这里下载的是ms windows in ...

  10. 镜像树(dfs)

    1214: J.镜像树 时间限制: 1 Sec  内存限制: 64 MB提交: 18  解决: 7 标签提交统计讨论版 题目描述 一棵二叉树,若其与自己的镜像完全相同,就称其为镜像树(即这棵二叉树关于 ...