.net网站开发(设计):1.什么是MVC模式
好吧我知道,应该很少人一开始学网站开发就从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模式的更多相关文章
- PHP-购物网站开发设计(一)
2015-07-6 开始使用PHP完成简单购物网站的设计,首先要选择合适的软件平台,所以今天先记录平台的选择与搭建: 我选择使用Apache24 + PHP 5.6 + MySQL 开发环境完成PHP ...
- Web开发的分层结构与MVC模式
1.分层结构 所谓分层结构.把不同的功能代码封装成类,把相同功能的类封装在一个个的包中,也叫层.功能归类如下: 实体类: 封装数据,是数据的载体,在层与层之间进行传递,数据也就传递了.比如说要传递学生 ...
- PHP-购物网站开发设计(二)
2015-07-7 今天介绍购物网站的后台数据库设计,数据库使用的是MySQL (1)在MySQL数据库中新建Database,命名为test (2)在test下新建三个数据表,分别为mismatch ...
- 【转】用java实例学习MVC模式
.1 MVC模式 MVC是三个单词的缩写,这三个单词分别为:模型(Model).视图(View)和控制(Controller).MVC模式的目的就是实现Web系统的职能分工.下面以J2EE开发进行介绍 ...
- MVC模式学习笔记
一.如何设计一个程序的结构,这是一门专门的学问,叫做“架构模式”(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,我觉得它不仅适用于开发软件,也适用于其 ...
- MVC模式浅谈
MVC模式浅谈 一.MVC模式概述 模型-视图-控制器(MVC模式)是一种非常经典的软件架构模式,在UI框架和UI设计思路中扮演着非常重要的角色.从设计模式的角度来看,MVC模式是 一种复合模式,它将 ...
- 【架构】MVC模式
架构模式 如何设计一个程序的结构,这是一门专门的学问,叫做"架构模式"(architectural pattern),属于编程的方法论. MVC模式就是架构模式的一种,它不仅适用于 ...
- struts2的MVC模式
MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而得到更好的开发和维护效率. 在MVC模式中,应用程序被划分成了模型(Model).视 ...
- 什么是MVC模式?
MVC (Model View Controller) 是一个设计模式,使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.M是指数据模型,V是指用户界面,C则是控制器. ...
- mvc 模式和mtc 模式的区别
首先说说Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负责业务对象与数据库的映射( ...
随机推荐
- 字符(汉子)转换为ASCII
一般在jdk里面都会自包含一个官方提供的转换工具:native2ascii.exe 调用方法: 打开cmd界面,使用cd C:\Program Files\Java\jdk1.6.0_39\bin命 ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...
- [每日一题] OCP1z0-047 :2013-07-27 外部表――不能被DML和建索引
首先看官方文档上的解释: Managing External Tables Oracle Database allows you read-only access to data in externa ...
- 基于ADODBX对数据库的CURD
学asp.net也有一个多星期了,之前对这个一无所知,也不知道怎么去找一些相关的资料去学习,不懂了就问问别人这个怎么做,那个怎么写,要不是有jsp和php的基础,估计还得弄上好长的时间来学习.记录一下 ...
- 前端判断用户请求是PC还是移动端
链接:https://www.zhihu.com/question/20004700/answer/13678113 第一步先在服务器端使用User Agent判断,先匹配出移动设备,这一步可以统计U ...
- tomcat6.0目录和server.xml详解
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,目前最新版本是6.x,相对5.x性能提升很多,主要优化了内存使用,增强IO能力,重新构造集群功能. 近期对Tomcat6.x作深入学习, ...
- mysql 获取全局唯一值
在涉及数据库存储数据的时候,经常会遇到唯一值问题,有的是主键带来的限制,有的则是业务上的需要. 下面介绍几种唯一值的获取或者生产方法: 先建一个测试用的表tbl_user,有三个字段:Id.Name. ...
- js各种验证文本框输入格式
不能为空 <input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')"> 只能输入英文 ...
- Log4Net 的简要配置
引用log4net.dll AssemblyInfo.cs中 [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyF ...
- shell之冒号的作用
冒号: :在shell中是一种命令,意思是总是为真,但是却不做任何操作,即总是为真的空命令 eg: [root@localhost ~]# ${abc:=t1}-bash: t1: comma ...