﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-PhoenixLi-文章分类-Java</title><link>http://www.blogjava.net/lihao336/category/41810.html</link><description>成于坚忍，毁于浮躁</description><language>zh-cn</language><lastBuildDate>Wed, 23 Sep 2009 09:44:15 GMT</lastBuildDate><pubDate>Wed, 23 Sep 2009 09:44:15 GMT</pubDate><ttl>60</ttl><item><title>Struts MVC的好处</title><link>http://www.blogjava.net/lihao336/articles/296151.html</link><dc:creator>PhoenixLi</dc:creator><author>PhoenixLi</author><pubDate>Wed, 23 Sep 2009 05:48:00 GMT</pubDate><guid>http://www.blogjava.net/lihao336/articles/296151.html</guid><wfw:comment>http://www.blogjava.net/lihao336/comments/296151.html</wfw:comment><comments>http://www.blogjava.net/lihao336/articles/296151.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/lihao336/comments/commentRss/296151.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/lihao336/services/trackbacks/296151.html</trackback:ping><description><![CDATA[<div class="Section0" style="layout-grid:  15.6pt none">
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">Struts<font face="SimSun">是对</font><font face="Times New Roman">MVC2</font><font face="SimSun">模型的实现，于是许多讲解</font><font face="Times New Roman">Struts</font><font face="SimSun">的书用</font><font face="Times New Roman">Servlet</font><font face="SimSun">做了个符合</font><font face="Times New Roman">MVC2</font><font face="SimSun">要求的</font><font face="Times New Roman">Web</font><font face="SimSun">应用，再用</font><font face="Times New Roman">Struts</font><font face="SimSun">做了个同样功能的</font><font face="Times New Roman">Web</font><font face="SimSun">应用。但是在对两种方式的对比中，我发现</font><font face="Times New Roman">Struts</font><font face="SimSun">似乎并没有为开发者带来很大的方便。以下是我的对比：</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">视图：两者一样</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">控制器：利用<font face="Times New Roman">Struts</font><font face="SimSun">并不能完全摆脱这一层，开发者还是需要写</font><font face="Times New Roman">Action.</font><font face="SimSun">使用</font><font face="Times New Roman">Servlet</font><font face="SimSun">方式，也是写一个同</font><font face="Times New Roman">Action</font><font face="SimSun">一样的</font><font face="Times New Roman">Servlet</font><font face="SimSun">充当控制器。两者在代码量上没有区别，在程序逻辑上也一样</font><font face="Times New Roman">;&nbsp;</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">模型：两者一样</span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">两者的主要差别：<font face="Times New Roman">Struts</font><font face="SimSun">多了一个</font><font face="Times New Roman">ActionServlet</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">既然编写一个类似<font face="Times New Roman">Acition</font><font face="SimSun">的</font><font face="Times New Roman">Servlet</font><font face="SimSun">就可以充当控制器，那么</font><font face="Times New Roman">Struts</font><font face="SimSun">在提供</font><font face="Times New Roman">Action</font><font face="SimSun">后，</font><font face="Times New Roman">ActionServlet</font><font face="SimSun">的意义何在</font><font face="Times New Roman">?</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">ActionServlet<font face="SimSun">的作用是拦截用户请求，并将用户请求转发给合适的</font><font face="Times New Roman">Action</font><font face="SimSun">，而自己的</font><font face="Times New Roman">Web</font><font face="SimSun">应用是将用户请求直接发送给功能等同于</font><font face="Times New Roman">Action</font><font face="SimSun">的自定义</font><font face="Times New Roman">Servlet.ActionServlet</font><font face="SimSun">在拦截过程中注入了一个</font><font face="Times New Roman">ActionForm</font><font face="SimSun">对象和一个</font><font face="Times New Roman">ActionMapping</font><font face="SimSun">对象。经过这个过程后，</font><font face="Times New Roman">Struts</font><font face="SimSun">为开发者带来了如下实际的好处：通过</font><font face="Times New Roman">ActionMapping</font><font face="SimSun">，</font><font face="Times New Roman">Action</font><font face="SimSun">在转发时，并不是转发给一个实际的页面。而是转发给在</font><font face="Times New Roman">strus-config.xml</font><font face="SimSun">中已经配置的对象。这意味着，在不改变</font><font face="Times New Roman">Action</font><font face="SimSun">代码的情况下就可以更换其转发的页面</font><font face="Times New Roman">;&nbsp;</font><font face="SimSun">如果没有</font><font face="Times New Roman">ActionMapping</font><font face="SimSun">，当有</font><font face="Times New Roman">100</font><font face="SimSun">个</font><font face="Times New Roman">Action</font><font face="SimSun">都要更换转发页面时，我们不得不在庞大的</font><font face="Times New Roman">Web</font><font face="SimSun">应用中找出这</font><font face="Times New Roman">100</font><font face="SimSun">个</font><font face="Times New Roman">Action</font><font face="SimSun">，修改其转发页面，然后再重新编译它们。有了</font><font face="Times New Roman">ActionMapping</font><font face="SimSun">后，只需要在</font><font face="Times New Roman">struts-config.xml</font><font face="SimSun">中修改相应的配置即可，这样既查找方便，又不用重新编译。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">现在的一个主要问题是：<font face="Times New Roman">Web</font><font face="SimSun">应用一旦投入使用之后，更换转发页面的可能性有多大</font><font face="Times New Roman">?Action</font><font face="SimSun">转发的页面，一般都是直接向用户展示的</font><font face="Times New Roman">JSP</font><font face="SimSun">页面。软件工程中，一切和用户直接打交道的部分都是极易发生变化的。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">当然<font face="Times New Roman">Struts</font><font face="SimSun">肯定还有其它方面的便利之处，但是这些还并不足以打动我去使用</font><font face="Times New Roman">Struts</font><font face="SimSun">，即便它还提供了丰富的标签库。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">最终一个重要的原因让我认为我的确需要采用像<font face="Times New Roman">Struts</font><font face="SimSun">这样的框架。当然，首先我一直是相信</font><font face="Times New Roman">MVC</font><font face="SimSun">模型所倡导的理念的：将视图和模型分开。把和用户交互的部分独立出来的好处是明显的。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">首先如前面所述，和用户交互的部分是最易发生变化的，视图的独立意味着变化的隔离<font face="Times New Roman">;&nbsp;</font><font face="SimSun">然后是将视图分离出去后，开发者可以将精力集中在对业务流程的处理上。一个大型的系统，最复杂的最核心的部分就是处理业务流程。可是实际的情况是，繁琐地界面处理占用了程序员大量甚至是大部分的时间。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">我相信了<font face="Times New Roman">MVC</font><font face="SimSun">模型带来的好处，所以开发</font><font face="Times New Roman">Web</font><font face="SimSun">应用时我一定会采用这种模式，但是我并不需要</font><font face="Times New Roman">Struts</font><font face="SimSun">，因为</font><font face="Times New Roman">Servlet</font><font face="SimSun">就可以让我实现</font><font face="Times New Roman">MVC</font><font face="SimSun">模型。我的这种想法似乎很自然，但是这里面隐含着一个前提是：我每次开发</font><font face="Times New Roman">Web</font><font face="SimSun">应用，都必须有意识的严格按照</font><font face="Times New Roman">MVC</font><font face="SimSun">规范来写。这看起来不是很困难，但是做起来却很难。因为有时候在业务处理过程中嵌入几行关于界面的代码似乎是非常自然而且简单的，于是我就真的这样做了。一段时间之后，我突然发现我的业务处理过程和界面显示部分又混杂在一起了。至此我才真正相信我要使用</font><font face="Times New Roman">Struts.</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
<p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; text-indent: 21pt"><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'">因为<font face="Times New Roman">Struts</font><font face="SimSun">可以规范程序员的行为。也许</font><font face="Times New Roman">Struts</font><font face="SimSun">并不能降低实际的代码量，甚至有时候不使用</font><font face="Times New Roman">Struts</font><font face="SimSun">的代码可能更简洁，但是按照</font><font face="Times New Roman">Struts</font><font face="SimSun">写出来的</font><font face="Times New Roman">Web</font><font face="SimSun">应用却一定是符合</font><font face="Times New Roman">MVC</font><font face="SimSun">模型的。就我认为，这是采用</font><font face="Times New Roman">Struts</font><font face="SimSun">的最大好处。规范程序员的行为，让程序在不知不觉中写出符合优秀架构的代码，这应该是所有框架的共同目的，也应该是根本目的。</font></span><span style="font-size: 10.5pt; font-family: 'Times New Roman'; mso-spacerun: 'yes'"><o:p></o:p></span></p>
</div>
<!--endfragment-->
<img src ="http://www.blogjava.net/lihao336/aggbug/296151.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lihao336/" target="_blank">PhoenixLi</a> 2009-09-23 13:48 <a href="http://www.blogjava.net/lihao336/articles/296151.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>