posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          StrutsTestCase 不需要 寫 classpath 方法 ^_^

          Posted on 2007-04-16 16:01 G_G 閱讀(682) 評論(4)  編輯  收藏 所屬分類: JUnit
          下載http://sourceforge.net/projects/strutstestcase/
          struts-config.xml
          <? xml?version = " 1.0 " ?encoding = " UTF-8 " ?>
          <! 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 >
          ??
          < data - sources? />
          ??
          < form - beans? >
          ????
          < form - bean?name = " loginForm " ?type = " com.yourcompany.struts.form.LoginForm " ? />

          ??
          </ form - beans >

          ??
          < global - exceptions? />
          ??
          < global - forwards? />
          ??
          < action - mappings? >
          ????
          < action
          ??????name
          = " loginForm "
          ??????path
          = " /login "
          ??????scope
          = " request "
          ??????type
          = " com.yourcompany.struts.action.LoginAction " >
          ??????
          < forward
          ????????name
          = " ok "
          ????????path
          = " /form/suss.jsp " />
          ??????
          < forward
          ????????name
          = " error "
          ????????path
          = " /form/login.jsp " />
          ?????????
          ????
          </ action >

          ??
          </ action - mappings >

          ??
          < message - resources?parameter = " com.yourcompany.struts.ApplicationResources " ? />
          </ struts - config >
          Action
          //Created?by?MyEclipse?Struts
          //?XSL?source?(default):?platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/JavaClass.xsl

          package?com.yourcompany.struts.action;

          import?javax.servlet.http.HttpServletRequest;
          import?javax.servlet.http.HttpServletResponse;

          import?org.apache.struts.action.Action;
          import?org.apache.struts.action.ActionForm;
          import?org.apache.struts.action.ActionForward;
          import?org.apache.struts.action.ActionMapping;

          import?com.yourcompany.struts.form.LoginForm;
          /**?
          ?*?MyEclipse?Struts
          ?*?Creation?date:?04-16-2007
          ?*?
          ?*?XDoclet?definition:
          ?*?@struts.action?path="/login"?name="loginForm"?input="/form/login.jsp"?scope="request"?validate="true"
          ?*?@struts.action-forward?name="Myjsp"?path="/WEB-INF/"
          ?
          */
          public?class?LoginAction?extends?Action?{

          ????
          //?---------------------------------------------------------?Instance?Variables

          ????
          //?---------------------------------------------------------?Methods

          ????
          /**?
          ?????*?Method?execute
          ?????*?
          @param?mapping
          ?????*?
          @param?form
          ?????*?
          @param?request
          ?????*?
          @param?response
          ?????*?
          @return?ActionForward
          ?????
          */
          ????
          public?ActionForward?execute(
          ????????ActionMapping?mapping,
          ????????ActionForm?form,
          ????????HttpServletRequest?request,
          ????????HttpServletResponse?response)?{
          ????????LoginForm?loginForm?
          =?(LoginForm)?form;
          ????????
          ????????String?name?
          =?loginForm.getName()?;
          ????????String?pass?
          =?loginForm.getPass()?;
          ????????
          ????????
          if(name.equals(pass)){
          ?????????????
          return?mapping.findForward("login");
          ????????????}
          ?????????
          return?mapping.findForward("ok");
          ?????????
          ????}

          }


          Form
          package?com.yourcompany.struts.form;

          import?org.apache.struts.action.ActionErrors;
          import?org.apache.struts.action.ActionForm;

          public?class?LoginForm?extends?ActionForm?{
          ????
          private?String?name?;
          ????
          private?String?pass?;
          ????
          ????
          public?void?setName(String?name){
          ????????
          this.name?=?name?;
          ????}
          ????
          public?String?getName(){
          ????????
          return?this.name?;
          ????}
          ????
          public?void?setPass(String?pass){
          ????????
          this.pass?=?pass?;
          ????}
          ????
          public?String?getPass(){
          ????????
          return?this.pass?;
          ????}
          }

          關鍵的來了 ----
          package?test;

          import?java.io.File;

          import?servletunit.struts.MockStrutsTestCase;
          import?junit.framework.TestCase;
          //
          public?class?StrutsTest?extends?MockStrutsTestCase?{

          ????
          public?StrutsTest(String?testName){
          ????????
          super(testName);
          ????}
          ????
          public?void?setUp()throws?Exception{
          ????????
          super.setUp();
          ??????????File?web?
          =?new?File("E:/src/StrutsTestCase/WebRoot/");
          ????????
          this.setContextDirectory(web);
          ????????setConfigFile(
          "E:/src/StrutsTestCase/WebRoot/WEB-INF/web.xml");
          ????????setConfigFile(
          "E:/src/StrutsTestCase/WebRoot/WEB-INF/struts-config.xml");
          ?

          ????}
          ????
          ????
          public?void?testAction()?{
          ??? ???
          setRequestPathInfo("/login");
          ????????this.addRequestParameter("name","liu");
          ????????
          this.addRequestParameter("pass","123");
          ????????actionPerform();
          ????????
          this.verifyForward("ok");
          ????}
          }

          不需要 寫 classpath 哈哈

          評論

          # 有關 cactus 的   回復  更多評論   

          2007-05-15 09:40 by G_G
           
          http://www.aygfsteel.com/zhyiwww/archive/2006/06/07/51185.html

          # re: StrutsTestCase 不需要 寫 classpath 方法 ^_^  回復  更多評論   

          2007-11-08 10:16 by 李泳
          假設我是用MyEclipse開發的,
          setConfigFile("E:/src/StrutsTestCase/WebRoot/WEB-INF/web.xml");
          里面的路徑是開發時項目中的路徑還是發布之后在Tomcat中的路徑????

          # re: StrutsTestCase 不需要 寫 classpath 方法 ^_^  回復  更多評論   

          2007-11-09 17:16 by G_G
          @李泳
          不是怎么理解的你的io操作!不要寫物理路徑。
          我告訴你一種方法
          這通過Classload來加載 寫邏輯路徑 就沒有這什么考慮的了
          可以參考我blog里的介紹
          http://www.aygfsteel.com/Good-Game/archive/2007/08/09/128154.html

          中的 hbn 加載文件

          # re: StrutsTestCase 不需要 寫 classpath 方法 ^_^  回復  更多評論   

          2007-11-09 17:20 by G_G
          你可以用
          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

          new File(
          classLoader.get URL 什么方法名字不太記的了
          );

          寫邏輯名就可以如你的:
          /WebRoot/WEB-INF/web.xml
          主站蜘蛛池模板: 磴口县| 康定县| 南乐县| 普安县| 永康市| 田阳县| 临武县| 大悟县| 长宁县| 武汉市| 东港市| 桓仁| 资阳市| 衡南县| 页游| 蓝山县| 如东县| 大足县| 克山县| 榕江县| 台东市| 海丰县| 沙洋县| 紫阳县| 鲜城| 定州市| 上栗县| 舞钢市| 通城县| 武宁县| 顺昌县| 云阳县| 和平县| 抚州市| 木兰县| 霞浦县| 安平县| 大邑县| 丰城市| 巍山| 惠东县|