好吧我知道,应该很少人一开始学网站开发就从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);
}
}
分享到:
相关推荐
我对照网上一篇讲述MVC设计模式的文章,用VS2008的C#语言写的MVC设计模式例子程序。
怎样使用mvc建立mvc的设计模式思想,了解mvc 设计模式
java 设计模式 mvc模式 单例模式 代理 工厂 简单工厂
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。 Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。 View...
MVC设计模式讲解PPT材料,模型-视图-控制器(MVC)是80年代Smalltalk-80出现的一种软件设计模式,现在已经被广泛的使用。MVC是一种软件设计模式,不仅仅应用于WEB。在日常生活中处处可以找到类似于MVC的例子。在WEB...
mvc模式.docmvc模式.docmvc设计模式 详解
博客(BLOG)是目前互联网上流行的信息发布交流工具。本文采用 MVC模式设计实现了 一个典型的博客系统,对系统的体系结构进行了详细的阐述,并给出了相应的实现模型,该 系统具有高效、稳定、易维护等特点。
jsp _MVC模式_课程设计 有利于大家的开发技术
非常详细的一个PDF文档 里面详细的解释了MVC设计模式
web项目开发的MVC设计模式的教程源码,MVC设计模式的思想,上课教程的源代码
MVC 设计模式 单例模式 MVC模式是"Model-View-Controller"的缩写,中文翻译为"模式-视图-控制器"。MVC应用程序总是由这三个部分组成。
mvc的详细设计模式,里面还有mvc的设计模式图及例子,对java想有更深层研究的人必看之选
JSP课程设计,基于MVC模式的用户注册登录系统
MVC模式下程序设计.pdf
这是我自己使用mvc设计模式开发的项目,很适合初学者使用。里面还使用到了很多jquery很值得一看
基于MVC模式,数据库用的是ACCESS。有添加删除功能等操作。
让你对MVC设计模式有一个更加深刻的认识,深入浅出的搞明白MVC
MVC设计模式
基于MVC模式的旅游管理与推广系统设计和实现4800字.docx基于MVC模式的旅游管理与推广系统设计和实现4800字.docx基于MVC模式的旅游管理与推广系统设计和实现4800字.docx基于MVC模式的旅游管理与推广系统设计和实现...