好吧我知道,应该很少人一开始学网站开发就从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);
}
}
分享到:
相关推荐
\7 asp.net应用与开发案例教程.rar
ASP.NET MVC框架开发系列课程(1):MVC模式与ASP.NET MVC框架概述
ASP.NET后台通用MVC+EF6+Bootstrap开发框架,全部采用最新的技术,后代代码采用IOC+AOP+DDD模式,灵活性强。绝对的asp.net开发者的福利,绝对的asp.net开发者的福利,绝对的asp.net开发者的福利,绝对的asp.net...
用的是ASP.NET MVC开发模式,可以导入和导出excel表格,使用了js、ajax,html,css后端使用nhibernate连接数据库,数据库用的sqlsever2015,系统主要有3个模块,部门、用户和资产模块,各模块都有多级分页模糊查询,...
全书论述了ASP.NET开发概述、C#及ADO.NET背景知识、ASP.NET Web窗体的基本控件、数据控件和数据绑定技术、用户控件和自定义控件、ASP.NET内置对象和缓存技术、ASP.NET应用程序配置及编译和部署、ASP.NET与Web服务、...
一个关于ASP.NET MVC开发模式的介绍课件
第ⅲ部分是第6-12章,本部分属于核心内容,结合按揭贷款申请的示例,详细讲解了企业开发涉及的各种设计模式,这些设计模式都是为了达成企业系统的特定目标;后是附录部分,主要是为不太熟悉.平台的读者准备的,介绍...
(1)MVC模式与ASP.NET MVC框架概述 时长1个多小时
基于MVC模式下设计的多个实例 开发环境: Visual Studio 2010(MVC3) SQL2005 运行环境: .net framework 4.0 SQL2005 网站架构: MVC3 EF4.0 SQLSERVER2005 JSON XML 网站结构: 权限:.net membership 文章...
[新闻文章]HuGo版文章发布系统(三层MVC模式)_computer.zip源码ASP.NET网站源码打包下载[新闻文章]HuGo版文章发布系统(三层MVC模式)_computer.zip源码ASP.NET网站源码打包下载[新闻文章]HuGo版文章发布系统(三层MVC...
《ASP.NET MVC网站编程案例精解》面向的读者是熟悉Web开发的基本概念和ASENET的Web开发人员,《ASP.NET MVC网站编程案例精解》中的所有示例是用C#语言实现的。 内容提要 ASENETMVC整合了强大的新功能,可以帮助您...
基于 BOOTSTRAP 和 KNOCKOUT.JS 的 ASP.NET MVC 开发实战。 利用动态服务端Web内容和响应Web设计共同构建的网站,在任何分辨率、桌面或移动设备下都可以进行良好的显示。通过本书的实践应用,你将可以学习对ASP.NET ...
3、数据库操作采用ADO.NET 模式(codefirst已放弃,后期很麻烦,还难调试)。 4、该权限系统精确控制到每一条命令,并且采用缓存机制减少多余的数据库权限验证操作。 三、初次运行说明 在文件夹里有说明文档。 四、...
UI界面应用MVC模式,将这个UI界面代码部分分为三部分:视图部分,模型部分和控制,自动生成Data Source属性来完成view和Model的相互映射;并且能按照功能组生成菜单, 同时系统能自动完成null, PK ,UK等基本的数据检验和...
Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程
一个国人写的开源blog系统,使用ASP.NET MVC框架开发. 具有以下优点: 1,代码精练,优雅,。 2,使用了大量的设计模式 3,采用N层架构设计,分层非常合理。 4,支持强大的扶肤功能 5,大量使用了.net Framework 3.5...
基于Asp.Net的湖南特产销售网站.zip 开发软件:VS2017 (版本2017以上...开发模式:mvc浏览器:谷歌浏览器 后台路径地址:local host:8080/admin/dist/index.html 前台路径地址:local host:8080/front/index.html
全书论述了ASP.NET开发概述、C#及ADO.NET背景知识、ASP.NET Web窗体的基本控件、数据控件和数据绑定技术、用户控件和自定义控件、ASP.NET内置对象和缓存技术、ASP.NET应用程序配置及编译和部署、ASP.NET与Web服务、...