MVC 精髓理解事例代码
来源:互联网 发布:淘宝情报 编辑:程序博客网 时间:2024/04/29 18:04
/*--------------这个html就是View-------------------------*/
<html>
... <!-- 省略不必要的部分 -->
...
<Form method=post action= "/Book/Broker "> <!-- 问题 1 -->
<table>
<tr>
<td> Enter ticker: </td>
<td> <input type=text length=5 name= "ticker "> </td>
</tr>
<tr>
<td> Number of shares: </td>
<td> <input type=text length=5 name= "numShares "> </td>
</tr>
<tr>
<td> Share Price: </td>
<td> <input type=text length=5 name= "price "> </td>
</tr>
</table>
<hr>
<table border=1>
<tr>
<td> <input type=hidden name= "Buy " value= "/jsp/Buy.jsp ">
<input type=submit name= "action " value= "Buy "> </td> <!-- 注意 -->
<td> <input type=hidden name= "Sell " value= "/jsp/Sell.jsp ">
<input type=submit name= "action " value= "Sell "> </td> <!-- 注意 -->
<td> <input type=hidden name= "Cancel " value= "/jsp/Cancel.jsp ">
<input type=submit name= "action " value= "Cancel "> </td> <!-- 注意 -->
<td> <input type=reset value= "reset "> </td>
.... <!-- 省略不必要的部分 -->
.... <!-- 注意该例子三个提交按钮“Buy”,“Sell”,“Cancel”都用同一个名字“action”
----------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------
/*---------
这个可以看成是controller
-----------*/
package simple;
...
... //和第一个例子相同,引入servlet包,这里省略
public calss BrokerServlet extends HttpServlet {
private static final String MAPPINGS_FILE= "/WEB-INF/config.properties ";
private Properties properties=new Properties();
public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
properties.load(config.getServletContext().getResourceAsStream(MAPPINGS_FILE));
}
catch(IOExcetion ioexp){
....
}
public void doPost(HttpServletRequest request,HttpServetResponse response)
throws ServletException,IOException {
RequestDispatcher rd = null;
String action = request.getParameter( "action ");
String nextURL = properties.getProperty(action);
if(nextURL!=null){
rd = request.getRequestDispatcher(nextURL);
rd.forward(request,response);
}
}
}
下面是属性文件
# Mapping of actions to destinations
Buy=/jsp/Buy.jsp
Sell=/jsp/Sell.jsp
Cancel=/jsp/Cancel.jsp
粗剖析:
1,html 文件即 V(view),是客户端交互可见的界面,当用户有动作(点击按钮),就提交form
2,服务器程序BrokerServlet 就是c(controller),接收html(view)的请求,并且将请求和Model(config)联系;
将从Model获得的数据(config文件中的那些文件名)提交给view(客户端浏览器)显示相应的页面。
3,config文件就是所谓的M(model)
细剖析:
本实例的最大好处可以看到,首先充分分离了V - C - M ;客户端只要提交请求就行而且重要的是他在界面的几个按钮都用name =“action”,这样在服务器端就可以统一处理了;而服务器端接收请求,加载config文件(Model),动态根据用户请求的信息来确定要返回给客户端的页面 rd.forward(request,response);
也就是说 如果客户端html页面如果再加一个按钮 name="action",然后在直接在配置文件里面加一条响应该按钮点击的响应页面就OK了 ,而 服务器端代码可以纹丝不动。
- MVC 精髓理解事例代码
- IOS KeyChain理解及代码事例
- 代码事例
- MVC思想精髓
- 客户端代码事例
- UIScrollView事例(代码)
- 代码事例1
- 代码事例2
- 代码事例3
- 反射事例代码
- solr实战代码事例
- SurfaceView简介(事例代码)
- bootStrap事例代码
- C/C++代码精髓
- C# 事务的事例代码
- C#.net反射-代码事例
- 大华 解码器上大屏代码事例
- hadoop wordcount代码事例详解
- MTK矢量字库与研究
- 双向反射率BRDF介绍
- PHP - 获取和设置include_path
- DIV+CSS 命名规范
- LINUX移植笔记----S3C2410
- MVC 精髓理解事例代码
- 你的第一个中断处理程序
- 关于 c++ 很精辟的一段评论
- win_linux_db_bak
- hdu 2056 -- Rectangles (矩形重合)
- datalist根据隐藏域的值改变背景与JS高度控制与JS自适应高度
- js弹出确认框
- Android学习笔记之TextView
- Ant与批处理(win环境)学习笔记(2)