国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

了解Struts1.1介紹

2019-11-17 06:23:05
字體:
來源:轉載
供稿:網友

  MVC是Model,View,Controller的縮寫,MVC是application開發的設計模式,也就是大家所知道的Model2.在MVC的設計模式中,要求在Application開發中你把商業邏輯,界面顯示,數據分離。也就是分別在Model,View,Controller實現:數據,控制(商業邏輯),顯示(頁面顯示).
  
  在以前或者說傳統的Web Application開發方式當中,如asp,php,jsp(Model 1)開發當中,我們在Asp(Php,Jsp)中實現一切,如:從數據庫中取到我們需要的數據,并根據數據之間的關聯和實際的需要按照某種方式把他顯示在頁面中以及從頁面提交的表單中提取數據,根據商業邏輯從數據庫查詢相關數據,或者把數據寫入數據庫。也就是說我們在Asp(Php,Jsp)實現一切包括:界面顯示,商業邏輯,數據存取。這樣帶來的后果就是你所寫的Asp(Php,Jsp)沒有層次,并且Html和Script(javascript、JScript,Asp、Php、Jsp源代碼)相互嵌套.可維護性差,最要命的是在Web Application通常顯示一塊是由美工完成的,很多時候也是你先寫好Asp、Php、Jsp然后美工進行美化,很有可能你發現經過美工處理完以后你的代碼已經面目全非了。你不得不把你的代碼重新組織。
  在MVC模式中這個問題的解決辦法是:View中負責顯示,View一般從Controller得到已經處理/過的數據,然后顯示在頁面當中,應該說這樣在Html中嵌套很少的Script.基本上美工的修改不大會廢掉你的勞動成果。
  在使用Java開發Web Application有幾種符合MVC設計模式的開發方式讓你選擇。
  1:Jsp+Servlet+JavaBean(EJB)
  2:Jsp+JavaBean(Controller)+JavaBean(EJB)(Model)
  3:TDK(Turbine,Velocity...)
  4:Xsp
  5:Jsp+Struts+JavaBean(EJB)
  我個人認為后面兩種比較好,其他幾種都有可取的地方非凡是使用TDK因為有一個比較好的工具可以自動生成很多代碼,至于它的缺點在后面幾種開發方式的比較當中我會介紹。
  
  Struts1.1的新功能
  Struts1.1與1.0相比加了一些很不錯的功能。最主要是表單驗證上功能增強。在Struts1.1數據的驗證不象以前在Action中在validator具體實現,而是在validation.xml通過配置實現這樣做的好處就是重用性加強了很多。
  
  Struts1.1實現的主要組成
  主要包括:Action,ActionForm,ActionMapping,ActionForward,開發當中最主要寫的是Action,ActionForm根據需要可以寫或不寫。下面我就一一具體介紹。
  Action
  An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to PRocess this request.
  上面是Struts開發小組對Action的描述,說Action實際上Request和Business Logic中間的適配器.通俗的說就是從表單中取到數據并穿給商業邏輯操作進行一系列的操作,然后返回相應的操作信息。
  
  ActionForm
  An ActionForm is a JavaBean optionally associated with one or more ActionMappings. SUCh a bean will have had its properties initialized from the corresponding request parameters before the corresonding action's execute() method is called.
  ActionForm實際上就是把從Request取到的數據封裝并進行校驗,然后把合法的數據給Action進行處理。實際上ActionForm除了進行數據校驗之外另外更重要的是在表單回寫的時候作用很大。反而在1.1以后數據校驗的大部分工作在validation.xml去實現。
  
  ActionMapping,ActionForward
  ActionMapping主要是用與配置和描述相關屬性使用的。先看下在struts-config.xml
  中的配置文件一段配置描述:
  <action-mappings>
  <!-- Registration Action -->
  <action  path="/usereg"
  type="com.bingo.finance.action.UseregAction"
  name="useregForm"
  scope="request"
  validate="true"
  input="/usereg.jsp">
  <forward name="success"       path="/msg.jsp"/>
  </action>
  </action-mappings>
  ActionMapping就是用來描述一個Action的URL、具體實現的文件、相對應的ActionForm 數據屬性(request or session)、是否需要進行數據校驗和回寫、以及處理完成后可能跳轉的URL.
  而ActionForward你就可以理解為Action 操作完成后的跳轉URL,Action在處理完相關操作后,返回的是一個ActionForward也就是告訴Struts我做完這個操作下一步到哪兒去。
  
  構建Struts1.1運行環境
  我的配置是居于Tomcat4.0以上版本討論,其他的AppServer大致相同。
  
  1:得到Struts1.1
  http://jakarta.apache.org/builds/jakarta-struts/release/v1.1-b1/jakarta-struts-1.1-b1.zip
  
  2:設置
  把Struts.jar Copy到$Tomcat_home/common/lib 或你使用Struts的Appaction下的WEB-INF/lib下
  在你使用Struts的Appaction下web.xml中增加下列配置
  
  <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
  <param-name>debug</param-name>
  <param-value>3</param-value>
  </init-param>
  <init-param>
  <param-name>detail</param-name>
  <param-value>3</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
  </servlet>
  
  <taglib>
  <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
  <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  
  <taglib>
  <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
  <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  
  <!-- Nested Tag Library Descriptor -->
  <taglib>
  <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
  <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>
  
  <!-- Template Tag Library Descriptor -->
  <taglib>
  <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
  <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
  </taglib>
  Struts1.1中提供了很具體的例子,你可以仔細看看.
  接下來你該根據需要配置struts-config.xml,以下是一個簡單的例子
  <?xml version="1.0" encoding="ISO-8859-1" ?>
  
  <!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  
  <struts-config>
  
  <!-- ========== Form Bean Definitions =================================== -->
  <form-beans>
  
  <!-- Registration form bean -->
  <form-bean   name="useregForm"
  type="com.bingo.finance.action.UserForm"/>
  
  </form-beans>
  
  <!-- ========== Global Forward Definitions ============================== -->
  <global-forwards>
  <forward  name="error"       path="/error.jsp"/>
  </global-forwards>
  
  <!-- ========== Action Mapping Definitions ============================== -->
  <action-mappings>
  
  <!-- Registration Action -->
  <action  path="/usereg"
  type="com.bingo.finance.action.UseregAction"
  name="useregForm"
  scope="request"
  validate="true"
  input="/usereg.jsp">
  <forward name="success"       path="/msg.jsp"/>
  </action>
  </action-mappings>
  
  <!-- ========== Message Resources Definitions =========================== -->
  
  <message-resources
  parameter="com.bingo.finance.common.DisplayMsg"/>
  
  <!-- ========== Plug Ins Configuration ================================== -->
  
  <!-- Add multiple validator resource files by setting the pathname property -->
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  <set-property property="pathname" value="/WEB-INF/validator-rules.xml"/>
  <set-property property="pathname" value="/WEB-INF/validation.xml"/>
  </plug-in>
  
  </struts-config>
  上面的英文我相信你能夠看懂。我就不做解釋了。你需要繼續配置validation.xml了,看如下
  簡單的例子.
  <form-validation>
  <formset>
  <form  name="useregForm">
  <field  property="username"
  depends="required,mask,minlength,maxlength">
  <arg0 key="common_username"/><

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文登市| 汪清县| 兰西县| 镇江市| 北票市| 镇康县| 平利县| 南投县| 河西区| 清新县| 富宁县| 兴城市| 林周县| 兴安县| 德昌县| 西平县| 敖汉旗| 丁青县| 宜川县| 宿松县| 阿坝县| 体育| 绥化市| 津市市| 华坪县| 天津市| 雷波县| 西林县| 枣庄市| 彰武县| 盐山县| 大厂| 股票| 思茅市| 禹州市| 涿鹿县| 龙泉市| 卢氏县| 东宁县| 介休市| 永登县|