??xml version="1.0" encoding="utf-8" standalone="yes"?>精品欧美一区二区三区久久久,国产精品久久久久久免费观看 ,a√中文在线观看http://www.aygfsteel.com/huhu/category/9539.htmlHuhu'Blogzh-cnWed, 28 Feb 2007 08:20:08 GMTWed, 28 Feb 2007 08:20:08 GMT60利用StrutsTestCase试Struts应用E序【{载?/title><link>http://www.aygfsteel.com/huhu/articles/43257.html</link><dc:creator>似水年</dc:creator><author>似水年</author><pubDate>Wed, 26 Apr 2006 05:47:00 GMT</pubDate><guid>http://www.aygfsteel.com/huhu/articles/43257.html</guid><wfw:comment>http://www.aygfsteel.com/huhu/comments/43257.html</wfw:comment><comments>http://www.aygfsteel.com/huhu/articles/43257.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huhu/comments/commentRss/43257.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huhu/services/trackbacks/43257.html</trackback:ping><description><![CDATA[ <center> <span style="FONT-SIZE: 20px"> <b>利用StrutsTestCase试Struts应用E序</b> </span> </center> <br /> <br /> <br /> <br />作?叶枫(<a target="_new">http://blog.matrix.org.cn/page/叶枫</a>)<br />原文:[http://www.matrix.org.cn/resource/article/44/44008_StrutsTestCase.html]http://www.matrix.org.cn/resource/article/44/44008_StrutsTestCase.html[/url]<br />关键?Struts StrutsTestCase<br /><br /><br /><b><span style="FONT-SIZE: 16px">一、Struts试概述</span></b><br />  <br />  一个具有良好系l架构的J2EE应用E序臛_有三层组?卌现层,商业层和pȝ<br />集成?包括数据存取以及和其他系l集?,目前,Struts是应用比较广?实现MVC2模式应用于表现层的一U技? 在这里面,Struts Action主要用来完成一些简单的数据校验,转换,以及程转发控制(注意:q里程不是业务规则). 因此在对整个应用E序q行试?我们同时也要试Struts Action. <br /><br />  但是,试Struts Action相对试单的JavaBean是比较困?因ؓStruts是运行在Web服务器中, 因此要测试Struts Action必d布应用程序然后才能测? 我们惌一?对于一个拥有上千个JSP page和数癄x千Java Classes的大规模应用E序,要把他们发布到诸如Weblogic之类的应用服务器再测?需要多的旉和硬件资? 所以这U模式的试是非常费时费力的.<br /><br />  所?如果有一U办法能够不用发布应用程?不需要Web服务器就能象试普通Java Class一h试Struts Action,那就能极大地加强Struts的可试性能,使应用程序测试更为容?单快? 现在q个工具来了,q就是StrutsTestCase.<br /><br /><br /><b><span style="FONT-SIZE: 16px">二、StrutsTestCase 概述</span></b><br /><br />  StrutsTestCase 是一个功能强大且Ҏ使用的Struts Action开源测试工?<br />它本w就是在大名鼎鼎的JUnit基础上发展v来的。因此通过和JUnitl合<br />使用能极大加强应用程序的试q加快应用程序的开?<br />  <br />  StrutsTestCase提供了两者测试方?模仿方式和容器测试方? 所谓模仿方式就是有StrutsTestCase本n来模拟Web服务? 而容器测试方式则需要Web服务? 本文要讨论的是前?原因很简单,不需要Web服务器就能象试普通的Java Class一h试Struts Action.<br /><br /><br /><b><span style="FONT-SIZE: 16px">三、准备StrutsTestCase和Struts Action/ActionForm/Config</span></b><br /><br />  StrutsTestCase是一个开源工?可以到http://strutstestcase.sourceforge.net下蝲. 目前最新版本是2.1.3,<br />如果你用Servlet2.3׃载StrutsTestCase213-2.3.jar,使用Servlet2.4的就下蝲StrutsTestCase213-2.4.jar.<br />另外StrutsTestCase本n是从JUnitl承?所以你q需要下载JUnit3.8.1.<br /><br />  在本文中,我们用一个简单的例子来做试. 假设我们有一张表Hotline(country varchar2(50),pno varchar2(50)),<br />我们要做的是Ҏ输入条g从这张表索相应的记录.索条件是country. <br /><br /><span style="COLOR: red">Value Object:</span>  <br /><pre class="overflow" title="pre code">package sample;      <br />      public class HotlineDTO implements Serializable{<br />        private String country = "";<br />       private String pno = "";        <br />        /**<br />         * Method HotlineActionForm<br />         *<br />         *<br />         */<br />        public HotlineDTO () {<br />             super();<br />      }<br /><br />        <br />        public void setCountry(String country) {<br />                this.country = country; <br />        }<br /><br />        public void setPno(String pno) {<br />                this.pno = pno; <br />        }<br /><br />        public String getCountry() {<br />                return (this.country); <br />        }<br /><br />        public String getPno() {<br />                return (this.pno); <br />        }        <br />      }</pre><br /><br /><span style="COLOR: red">ActionForm:</span><br />      <pre class="overflow" title="pre code">package sample;<br />      import org.apache.struts.action.ActionForm;<br />      public class HotlineActionForm extends ActionForm{<br />        private String country = "";<br />        private String pno = "";        <br />        /**<br />         * Method HotlineActionForm<br />         *<br />         *<br />         */<br />        public HotlineActionForm() {<br />                super();<br />        }<br /><br />        <br />        public void setCountry(String country) {<br />                this.country = country; <br />        }<br /><br />        public void setPno(String pno) {<br />                this.pno = pno; <br />        }<br /><br />        public String getCountry() {<br />                return (this.country); <br />        }<br /><br />        public String getPno() {<br />                return (this.pno); <br />        }        <br />      }<br />  </pre><br /><br /><span style="COLOR: red">Action Class:</span><br /><pre class="overflow" title="pre code"><br />     public class SearchHotlineAction extends Action {<br />     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,<br />      HttpServletResponse response) throws Exception {<br />        String target = "";<br />        try{<br />         //调用HotlineDAO索hotline<br />         String country=((HotlineActionForm)form).getCountry();<br />         List hotlineList = HotlineDAO.getHotlineList(country);<br />         if(hotlineList!=null && hotlineList.size()>0){<br />            request.setAttribute("hotlineList",hotlineList);<br />            target = "hotlineList";<br />         }else{<br />            target = "notfound";<br />         }<br />        }catch(Exception ex){<br />           ....<br />        }<br />                <br />        <br />     }</pre><br /><br /><span style="COLOR: red">Struts Config:</span><br />  <pre class="overflow" title="pre code"><struts-config><br />   <br />   <form-beans><br />     <form-bean name="hotlineActionForm" type="sample.HotlineActionForm" /><br />       .......<br />   </form-beans><br />  <action-mappings><br />     <action path="/SearchHotline"<br />           name="hotlineActionForm"<br />           type="sample.SearchHotlineAction "<br />                     scope="request"                                   <br />                   validate="false"> <br />      <forward name="hotlineList" path="/hotlineList.jsp"/>        <br />      <forward name="notfound" path="/searchHotline.jsp"/><br />   </action><br />    .....<br />  <action-mappings><br />   ........<br /> <struts-config></pre><br /><br /><b><span style="FONT-SIZE: 16px">四、初试StrutsTestCase</span></b><br /><br />    当采用模拟方式时,所有的StrutsTestCase试Class都是从MockStrutsTestCasel承下来?<br />    下面我们创Z个最单的试Class.<br />    <pre class="overflow" title="pre code">public class SearchHotlineAction extends MockStrutsTestCase {<br /><br />     public void setUp()throws Exception{<br />     }<br /><br />     public void tearDown()throws Exception{<br />     }<br /><br />    public void testSearchHotline() throws Exception{<br />    setRequestPathInfo("/SearchHotline.do");<br />    addRequestParameter("country", "CN");<br />    actionPerform();<br />    }<br />    }</pre><br /><br />   上面的Class怿用过JUnit的朋友都很熟? <br />    好了,一个简单的试例子完成了,如果你用的是Eclipse选择Run-Run...-JUnit-New可以直接运?不需要发布你的程?不需要Q何的Web服务器支?可以测试Struts Action,q就是StrutsTestCase带来的好?下面单地介绍一下它是怎么工作?<br /><br />    在上面的例子?我们调用setRequestPathInfo()告诉StrutsTestCase我们要模拟JSP调用SearchHotline.doq个Action,q且调用addRequestParameter()增加了一个参数country.最后调用actionPerform()q行.<br /><br />看到q里,大家发现一个问题没? 在上面Action的源代码里我们是通过<br /><br /><pre class="overflow" title="pre code">String country=((HotlineActionForm)form).getCountry();</pre><br /><br />也就是ActionForm来取得输入的参数?可我们在testSearchHotline()Ҏ里ƈ没有讄ActionForm?<br /><br />那么它是怎么出来的呢? 其实大家如果熟悉Struts的运行流E的话就知道,JSP接受用户的输入ƈ发请求时<br />都是cMq样的http://hostname/servletName?param1=value1&param2=value2. 只是Struts接受到这?br />参数后再ҎStruts Config里的Action和ActionForm的映把他们转ؓActionForm后传lAction?<br /><br />   在上面的例子,我们只是单地q行了Action,那么Action是否正确执行以及q回的结果是不是我们惌的呢?<br /><br />我们l箋完善一下testSearchHotline()q个Method.<br /><br /><span style="COLOR: navy">public void testSearchHotline() throws Exception{<br />    setRequestPathInfo("/SearchHotline.do");<br />    addRequestParameter("country", "CN");<br />    actionPerform();<br />    verifyNoActionErrors();<br />    verifyForward("hotlineList");<br />    assertNotNull(request.getAttribute("hotlineList"));<br />    List hotlineList = (List) request.getAttribute("hotlineList"); <br />    for (Iterator it = hotlineList.iterator();it.hasNext();){<br />     ....<br />    }<br />    }</span><br /><br />我们在actionPerform()后增加了几行语句来断定Struts Action是否正确执行.<br />   verifyNoActionErrors() -- 判断Action里没有Q何的Action;<br />   verifyForward("hotlineList") -- 判断Action实转发到hotlineList;      <br />   assertNotNull(request.getAttribute("hotlineList")) -- 判断Action实q回了hotlineListq且不ؓI?br /><br />到这?我们已经基本上讨论完了StrutsTestCase的核心部? 从头到尾,我们没有发布应用E序,也不需要Web服务?Ҏ们来?Struts Actionp普通的Java Class一样容易调试测?q就是StrutsTestCasel我们带来的方便.<br /><br /><b><span style="FONT-SIZE: 16px">五、深入StrutsTestCase</span></b><br /><br />  除了以上我们用到的几个断定和校验Ҏ?StrutsTestCaseq提供了其他几个Ҏ便于我们试Struts Action. 下面我一一讲述,具体的大家可以参考文?<br /><br />verifyActionErrors/Messages --  校验ActionActionServlet controller 是否发送了ActionError或ActionMessage.   参数为ActionError/Message Key<br />verifyNoActionErrors/Messages --校验ActionActionServlet controller 没有发送ActionError或ActionMessage<br />VerifyForward -- 校验Action是否正确转发到指定的ActionForward.<br />VerifyForwardPath -- 校验Action是否正确转发到指定的URL<br />verifyInputForward -- 校验Action是否转发到Action Mapping里的input属?br />verifyTilesForward/verifyInputTilesForward--和以上类?应用E序使用到tiles时用?br /><br /><b><span style="FONT-SIZE: 16px">六、关于Web.xml和Struts-Config.xml</span></b><br /><br />  ~省情况?StrutsTestCase认ؓ你的Web.xml和struts-config.xml的\径分别是:<br /><span style="COLOR: blue">  /WEB-INF/web.xml?WEB-INF/struts-config.xml</span><br /><br />  1. 假如你的web.xml/struts-config.xml的\径是<br />  d:/app/web/WEB-INF/web.xml(struts-config.xml)的话,需要把d:/app/web加到classpath.<br />  <br />  2. 假如你的struts config是strust-config-module.xml,<br />  那么必须调用setConfigFile()讄你的struts config文g<br />  <br /><br /><b><span style="FONT-SIZE: 16px">七、结束语</span></b><br />  J2EE应用E序的测试在开发过E中占有相当重要的位|?利用StrutsTestCase能极大方便你试ZStruts的应用程?<br /><img src ="http://www.aygfsteel.com/huhu/aggbug/43257.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huhu/" target="_blank">似水年</a> 2006-04-26 13:47 <a href="http://www.aygfsteel.com/huhu/articles/43257.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>struts html:multibox标签(转蝲)http://www.aygfsteel.com/huhu/articles/39413.html似水年似水年Wed, 05 Apr 2006 08:18:00 GMThttp://www.aygfsteel.com/huhu/articles/39413.htmlhttp://www.aygfsteel.com/huhu/comments/39413.htmlhttp://www.aygfsteel.com/huhu/articles/39413.html#Feedback0http://www.aygfsteel.com/huhu/comments/commentRss/39413.htmlhttp://www.aygfsteel.com/huhu/services/trackbacks/39413.html 问题原型Q?br />     d面中有10个checkboxQ用户选中?个后提交
     修改面中应昄10个checkboxQ同时应该有5个已l被选中
 
详细实际问题Q?br />    Z满业务需要,从linux安装盘中的comps.xml中读取提供的服务所依赖的包
    昄在页面上Q根据包的类型(default,mandatory,optionalQ决定显C的Ҏ
    default默认选中Qmandatory不提供选择Qoptional为用户可?br />  

< c:choose >
  
< c:when  test ="${p.type == 'mandatory'}" >
    
< td >
     
< input  type ="checkbox"  name ="nodeRPMS"
    style
="VISIBILITY: hidden; "
    checked
="true"
    value
="<c:out value=" ${p.rpmName}" /> "/>
    
</ td >
  
</ c:when >

   
< c:when  test ="${p.type == 'default'}" >
    
< td >
      
< input  type ="checkbox"
       name
="nodeRPMS"   checked =true  
       
value ="<c:out  value=" ${p.rpmName}" /> "/>
    
</ td >
   
</ c:when >

  
< c:when  test ="${p.type == 'optional'}" >
    
< td >  
     
< input  type ="checkbox"  name ="nodeRPMS"
       value
="<c:out value=" ${p.rpmName}" /> "/>
    
</ td >
  
</ c:when >
</ c:choose >

在处理层Q采用struts的form来接受,定义string [] nodeRPMs接受卛_
接受后存在数据库里,然后d来在昄在页面上

在修攚w面,首先应该有全部的checkboxQ同时用户选中的应该已l被打勾Q这来自数据库读出的记录Q?l过中午的查找资?br />才知道可以用mutilbox解决q个问题

< c:choose >
  
< c:when test = " ${p.type == 'mandatory'} " >
    
< td >
      
< input type = " checkbox "  name = " nodeRPMS "
    style
= " VISIBILITY: hidden;  "
    
checked = " true "
    value
= " <c:out value= " $ {p.rpmName} " /> " />
   
</ td >
  
</ c:when >

  
< c:otherwise >
   
< td >
      
< html:multibox property = " nodeRPMS " >
      
< c: out  value = " ${p.rpmName} " />
      
</ html:multibox >     
   
</ td >
  
</ c:otherwise >
</ c:choose >


mutilbox会根据你d的值来讑֮
 struts中的checkbox是单选的Q它的值应该只有true和false或者yes no或者on off之分Q?br />r如果希望使用一l可以多选的checkboxQ在struts中的面标签应该是html:multibox,它的用法Q?lt;html:multibox property="strArray" value="value1">或?br />       <html:multibox property="strArray">value2</html:multibox>

真正载页面上使用时可能是q样Q?br /><html:multibox property="strArray">a</html:multibox>
<html:multibox property="strArray">b</html:multibox>
<html:multibox property="strArray">c</html:multibox>

当然"strArray"一定是面相对应的formbean中的一个属性,q且应该是数l:
private String[] strArray; //提供get setҎ

q样Q如果我们在面上面选中了前两个Q则strArray.length=2;strArray[0]里面的值是a,strArray[1]里面的值是b;
如果我们把form中的strArray讄为strArray[0]="b",strArray[1]="c",则{到页面之后,面上只有后两项被选中
所以这样就可以解决问题?br />



似水年 2006-04-05 16:18 发表评论
]]>
վ֩ģ壺 ɳ| | ʯ| | | ƽң| | û| ʯ| ͼ| żҽ| | ɽ| ̩| | ¬| ͳ| | ͼƬ| ԭ| | | | | | ̩| | Է| | ͼ| | | | | | ĩ| | | | | |