了解Struts1.1介绍

分类: J2EE   出处:iocblog整理  更新时间:2008-06-18   添加到收藏  

  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设计模式的开发方式让你选择。(来源www.iocblog.net)
  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了
Tag: Struts



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。