skyful

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            16 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

          2006年8月2日 #

          <body onLoad="page_onload();">

          function page_onload(){
           if(typeof(initPage)=='function'){
            initPage();
           }
          }


          function initPage(){
           for(var i=0;i<document.forms[0].elements.length;i++)
          {
           if (document.forms[0].elements[i].type=="checkbox" && document.forms[0].elements[i].name.substring(0,22)=="hotelRoom.cardTypeItem"){
           document.forms[0].elements[i].checked=true;
          }
          }
          }
          posted @ 2007-04-06 10:13 氓氓 閱讀(1454) | 評(píng)論 (0)編輯 收藏

          Struts作為MVC 2的Web框架,自推出以來不斷受到開發(fā)者的追捧,得到用廣泛的應(yīng)用。作為最成功的Web框架,Struts自然擁有眾多的優(yōu)點(diǎn):
          • MVC 2模型的使用
          • 功能齊全的標(biāo)志庫(kù)(Tag Library)
          • 開放源代碼

          但是,所謂“金無赤金,人無完人”,Struts自身也有不少的缺點(diǎn):

          • 需要編寫的代碼過多,容易引起“類爆炸”
          • 單元測(cè)試?yán)щy

          這些缺點(diǎn)隨著Web的發(fā)展越來越明顯。這就促生了Struts 2.0,它的誕生能很好的解決上述問題。 好啦,廢話就不多說了,現(xiàn)在就讓我們感受一下的Struts 2.0的魅力吧。

          1. 搭建開發(fā)和運(yùn)行環(huán)境
            1. 到Apache下載Struts 2.0包

            2. 打開Eclipse 3.2新建Web工程

              點(diǎn)擊菜單File\New\Project,出現(xiàn)如圖1所示對(duì)話框
              圖1 新建工程對(duì)話框
              圖1 新建工程對(duì)話框
              選擇Web\Dynamic Web Project,點(diǎn)擊“Next”,出現(xiàn)圖2對(duì)話框

              圖2 新建動(dòng)態(tài)Web工程對(duì)話框
              圖2 新建動(dòng)態(tài)Web工程對(duì)話框
              在“Project Name”中鍵入Struts2_HelloWorld,點(diǎn)擊“New”,出現(xiàn)以下對(duì)話框

              圖3 新建服務(wù)器運(yùn)行時(shí)對(duì)話框
              圖3 新建服務(wù)器運(yùn)行時(shí)對(duì)話框
              選擇“Apache\Apache Tomat v5.5”,點(diǎn)擊“Next”,出現(xiàn)以下對(duì)話框

              圖4新建服務(wù)器運(yùn)行時(shí)對(duì)話框
              圖4新建服務(wù)器運(yùn)行時(shí)對(duì)話框
              點(diǎn)擊“Finish”,關(guān)閉對(duì)話框。

            3. 將Struts 2.0 lib下的jar文件加到工程的構(gòu)建路徑(build path)

              圖5 Struts 2.0的lib目錄
              圖5 Struts 2.0的lib目錄
              按ctr+a全選,復(fù)制,再轉(zhuǎn)到Eclipse窗口,在“Project Explorer”子窗口中選中Struts2_HelloWorld\WebContent\WEB-INF\lib,然后粘貼。經(jīng)過Eclipse自動(dòng)刷新“Project Explorer”子窗口,剛才所粘貼的jar文件應(yīng)該會(huì)出現(xiàn)在Struts2_HelloWorld\Java Resources: src\Libraries\Web App Libraries下,如圖6所示:

              圖6 Project Explorer子窗口
              圖6 Project Explorer子窗口

            4. 打開web.xml文件,將其修改為以下代碼:
              <?xml?version="1.0"?encoding="ISO-8859-1"?>
              <!DOCTYPE?web-app?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"?"http://java.sun.com/dtd/web-app_2_3.dtd">
              <web-app>
              ????
              <display-name>Struts?2.0?Hello?World</display-name>
              ????
              <filter>
              ????????
              <filter-name>struts2</filter-name>????????<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
              ????
              </filter>
              ????
              <filter-mapping>
              ????????
              <filter-name>struts2</filter-name>
              ????????
              <url-pattern>/*</url-pattern>
              ????
              </filter-mapping>
              ????
              <welcome-file-list>
              ????????
              <welcome-file>index.html</welcome-file>
              ????
              </welcome-file-list>
              </web-app>

            5. 新建struts.xml文件

              右鍵點(diǎn)擊,Struts2_HelloWorld\Java Resources: src,出現(xiàn)如圖7所示菜單
              圖7 新建Other菜單
              圖7 新建Other菜單
              點(diǎn)擊“Other”,出現(xiàn)新建對(duì)話框,如圖8所示

              圖8 新建對(duì)話框
              圖8 新建對(duì)話框
              點(diǎn)擊“Next”,出現(xiàn)新建文件對(duì)話框,如圖9所示

              圖9 新建文件對(duì)話框
              圖9 新建文件對(duì)話框
              在“File name”中鍵入sturts.xml,點(diǎn)擊“Finish”,然后將struts.xml的內(nèi)容修改為:

              <!DOCTYPE?struts?PUBLIC
              ????????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
              ????????"http://struts.apache.org/dtds/struts-2.0.dtd"
              >
              <struts>
              ????
              <include?file="struts-default.xml"/>
              </struts>

            6. 新建index.html文件

              右鍵點(diǎn)擊Struts2_HelloWorld\WebContent,出現(xiàn)如圖10所示的菜單
              圖10 新建Other菜單
              圖10 新建Other菜單
              點(diǎn)擊“Other”,出現(xiàn)新建對(duì)話框,如圖11所示

              圖11 新建對(duì)話框
              圖11 新建對(duì)話框
              選擇Web\HTML,點(diǎn)擊“Next”出現(xiàn)如圖12所示的對(duì)話框

              圖12 新建HTML頁(yè)面對(duì)話框
              圖12 新建HTML頁(yè)面對(duì)話框
              在“File Name”中鍵入index.html,點(diǎn)擊“Next”,出現(xiàn)如圖13所示的對(duì)話框

              圖13 模板選擇對(duì)話框
              圖13 模板選擇對(duì)話框
              點(diǎn)擊“Finish”,將index.html的內(nèi)容修改為以下內(nèi)容:

              <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
              <html>
              <head>
              <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
              <title>Hello?World</title>
              </head>
              <body>
              <h3>Hello?World!</h3>
              </body>
              </html>

            7. 將應(yīng)用程序打包到tomcat上

              右鍵點(diǎn)擊Struts_HelloWorld,出現(xiàn)如圖14所示的菜單
              圖14 工程菜單
              圖14 工程菜單
              點(diǎn)擊“Export\WAR file”,出現(xiàn)如圖15所示的對(duì)話框

              圖15 輸出對(duì)話框
              圖15 輸出對(duì)話框
              選擇“Web\WAR file”,點(diǎn)擊“Next”,出現(xiàn)如圖16所示的對(duì)話框

              圖16 輸出路徑對(duì)話框
              圖16 輸出路徑對(duì)話框
              輸入war文件的路徑(如%tomcat%\webapps\Struts2_HelloWorld.war),點(diǎn)擊“Finish”關(guān)閉對(duì)話框。

            8. 啟動(dòng)tomcat,運(yùn)行應(yīng)用程序

              打開你的Internet Explorer,鍵入http://localhost:8080/Struts2_HelloWorld/,窗口輸出如圖17所示
              圖17 Hello World窗口
              圖17 Hello World窗口

          2. 第一個(gè)Struts 2.0應(yīng)用程序——Hello World
            1. 新建類包(package)

              右鍵點(diǎn)擊Struts2_HelloWorld\Java Resources: src,出現(xiàn)如圖18所示菜單
              圖18 新建菜單
              圖18 新建菜單"
              點(diǎn)擊“New\Package”,出現(xiàn)如圖19所示對(duì)話框

              圖19新建Java類包對(duì)話框
              圖19新建Java類包對(duì)話框
              在“Name”鍵入tutorial,點(diǎn)擊“Finish”關(guān)閉對(duì)話框。

            2. 新建HelloWorld.java文件

              右鍵點(diǎn)擊Struts2_HelloWorld\Java Resources: src\tutorial,出現(xiàn)如圖20所示菜單
              圖20 新建菜單
              圖20 新建菜單
              點(diǎn)擊“New\Class”,出現(xiàn)如圖21所示對(duì)話框

              圖21 新建Java類對(duì)話框
              圖21 新建Java類對(duì)話框
              在“Name”中鍵入HelloWorld,在“Superclass”中鍵入com.opensymphony.xwork2.ActionSupport,點(diǎn)擊“Finish”關(guān)閉對(duì)話框。將HelloWorld.java的內(nèi)容修改為:

              package?tutorial;

              import?com.opensymphony.xwork2.ActionSupport;

              public?class?HelloWorld?extends?ActionSupport?{
              ????
              private?String?name;
              ????
              ????
              public?String?getName()?{
              ????????
              return?name;
              ????}

              ????
              ????
              public?void?setName(String?name)?{
              ????????
              this.name?=?name;
              ????}

              ????
              ????
              public?String?execute()?{
              ????????name?
              =?"Hello,?"?+?name?+?"!";?
              ????????
              return?SUCCESS;
              ????}

              }

            3. 在struts.xml中添加action映射(mapping)
              <!DOCTYPE?struts?PUBLIC
              ????????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"
              ????????"http://struts.apache.org/dtds/struts-2.0.dtd"
              >
              <struts>
              ????
              <include?file="struts-default.xml"/>
              ????
              <package?name="tutorial"?extends="struts-default">
              ????????
              <action?name="HelloWorld"?class="tutorial.HelloWorld">
              ????????????
              <result>HelloWorld.jsp</result>
              ????????
              </action>
              ????
              </package>
              </struts>

            4. 新建SayHello.jsp

              參考“新建index.html文件”步驟,彈出如圖22所示對(duì)話框
              圖22 新建對(duì)話框
              圖22 新建對(duì)話框
              點(diǎn)擊“Next”, 進(jìn)入下一步,如圖23所示

              圖23 新建JSP對(duì)話框
              圖23 新建JSP對(duì)話框
              在“File name”鍵入SayHello.jsp,點(diǎn)擊“Next”進(jìn)入下一步,如圖24所示

              圖24 模板選擇對(duì)話框
              圖24 模板選擇對(duì)話框
              點(diǎn)擊“Finish”關(guān)閉對(duì)話框,并將SayHello.jsp的內(nèi)容修改為:

              <%@?page?contentType="text/html;?charset=UTF-8"?%>
              <%@?taglib?prefix="s"?uri="/struts-tags"?%>
              <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
              <html>
              ????
              <head>
              ????????
              <title>Say?Hello</title>
              ????
              </head>
              ????
              <body>
              ????????
              <h3>Say?"Hello"?to:?</h3>
              ????????
              <s:form?action="HelloWorld">
              ????????????Name:?
              <s:textfield?name="name"?/>
              ????????????
              <s:submit?/>
              ????????
              </s:form>
              ????
              </body>
              </html>

            5. 新建HelloWorld.jsp(請(qǐng)參考上一步),HelloWorld.jsp的內(nèi)容為:
              <%@?page?contentType="text/html;?charset=UTF-8"?%>
              <%@?taglib?prefix="s"?uri="/struts-tags"?%>
              <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
              <html>
              ????
              <head>
              ????????
              <title>Hello</title>
              ????
              </head>
              ????
              <body>
              ????????
              <h3><s:property?value="name"?/></h3>
              ????
              </body>
              </html>

            6. 重新打包發(fā)布應(yīng)用程序

              先停止tomcat, 再將tomcat里webapps下的Struts2_HelloWorld.war和Struts2_HelloWorld文件夾刪除,參照“將應(yīng)用程序打包到tomcat上”重新發(fā)布應(yīng)用程序。

            7. 啟動(dòng)tomcat,運(yùn)行測(cè)試

              打開Internet Explorer,鍵入http://localhost:8080/Struts2_HelloWorld/SayHello.jsp,窗口輸出如圖25所示
              圖25 SayHello.jsp
              圖25 SayHello.jsp
              在“Name”鍵入字符串(如World),點(diǎn)擊Submit,轉(zhuǎn)到HelloWorld.jsp頁(yè)面,如圖26所示

              圖26 HelloWorld.jsp
              圖26 HelloWorld.jsp

          3. 單元測(cè)試Hello World

            在文章開始的時(shí)候提及,單元測(cè)試?yán)щy是Struts一大缺點(diǎn)。現(xiàn)在讓我們?cè)隗w驗(yàn)一下,在Struts 2.0中是如何進(jìn)行測(cè)試的。

            1. 新建JUnit單元測(cè)試

              右鍵點(diǎn)擊Struts2_HelloWorld\Java Resources: src\tutorial,彈出如圖27所示對(duì)話框
              圖27 新建菜單
              圖27 新建菜單
              點(diǎn)擊“Next\Other”

              圖28 新建對(duì)話框
              圖28 新建對(duì)話框
              選擇“Java\JUnit\JUnit Test Case”,點(diǎn)擊“Next”

              圖29 新建JUnit 測(cè)試用例對(duì)話框
              圖29 新建JUnit 測(cè)試用例對(duì)話框
              選擇“New JUnit 4 test”,在“Name”中鍵入HelloWorldTest,在“Class under test”鍵入tutorial.HelloWorld,點(diǎn)擊“Next”

              圖30 選擇方法對(duì)話框
              圖30 選擇方法對(duì)話框
              選中HelloWorld\execute方法,點(diǎn)擊Finish。如果生成的HelloWorldTest.java文件的圖標(biāo)(Icon)出現(xiàn)紅色交叉標(biāo)志,請(qǐng)進(jìn)行以下步驟添加JUnit 4的jar包。

              右鍵點(diǎn)擊Struts2_HelloWorld,出現(xiàn)如圖所示菜單。 圖31 新建菜單
              圖31 新建菜單
              點(diǎn)擊“Build Path\Add Libararis”,彈出圖32對(duì)話框

              圖32 添加庫(kù)對(duì)話框
              圖32 添加庫(kù)對(duì)話框
              選中“JUnit”,點(diǎn)擊“Next”

              圖33 選擇版本對(duì)話框
              圖33 選擇版本對(duì)話框
              選擇“JUnit 4”,點(diǎn)擊“Finish”關(guān)閉對(duì)話框,并將HelloWorldTest.java的內(nèi)容修改為:

              package?tutorial;

              import?static?org.junit.Assert.assertTrue;

              import?org.junit.Test;

              import?com.opensymphony.xwork2.ActionSupport;

              public?class?HelloWorldTest?{

              ????@Test
              ????
              public?void?testExecute()?{
              ????????HelloWorld?hello?
              =?new?HelloWorld();
              ????????hello.setName(
              "World");
              ????????String?result?
              =?hello.execute();
              ????????
              ????????assertTrue(
              "Expected?a?success?result!",?ActionSupport.SUCCESS.equals(result));
              ????????
              ????????
              final?String?msg?=?"Hello,?World!";
              ????????assertTrue(
              "Expected?the?default?message!",?msg.equals(hello.getName()));
              ????}


              }

            2. 運(yùn)行單元測(cè)試

              右鍵點(diǎn)擊Struts2_HelloWorld\Java Resources: src\tutorial\HelloWorldTest.java,彈出如圖34所示菜單
              圖34 運(yùn)行為菜單
              圖34 運(yùn)行為菜單
              點(diǎn)擊“Run As\JUnit Test”,出現(xiàn)JUnit子窗口如圖35所示

              圖35 JUnit子窗口
              圖35 JUnit子窗口
              圖35的綠色矩形表示,所有單元測(cè)試通過。

          4. 總結(jié)

            上面的例子簡(jiǎn)單地演示了,Web 應(yīng)用程序的基本操作,也即是,頁(yè)面輸入->Action處理->再輸出到另外頁(yè)面。Struts 2.0的簡(jiǎn)單易用、方便測(cè)試相信也會(huì)給大家留下不錯(cuò)的印象吧。我相信,Struts 2.0作為一個(gè)全新的Web架構(gòu),將會(huì)再次掀起Web開發(fā)的熱潮。 不過,Struts 2.0還在測(cè)試中,正式版的發(fā)布還需些時(shí)日,所以文檔方面可能有所欠缺。請(qǐng)大家繼續(xù)留意我的博客,我會(huì)盡我所能為大家寫更多關(guān)于Struts 2.0的文章。

          posted @ 2007-03-17 15:58 氓氓 閱讀(497) | 評(píng)論 (0)編輯 收藏

          今天在eclipse+myeclipse+tomcat5 下,寫個(gè)中文亂碼轉(zhuǎn)碼filter,就報(bào)
          2007-3-13 17:43:46 org.apache.catalina.core.StandardContext start
          嚴(yán)重: Error filterStart
          2007-3-13 17:43:46 org.apache.catalina.core.StandardContext start
          嚴(yán)重: Context startup failed due to previous errors
          ?錯(cuò)誤,所屬項(xiàng)目也就啟動(dòng)不了,web。xml配置為 :
          <filter>
          ? ?<filter-name>CharsetEncodingFilter</filter-name>
          ? ?<filter-class>com.changyou.filter.CharsetEncodingFilter</filter-class>
          ? ?<init-param>
          ? ??<param-name>encoding</param-name>
          ? ??<param-value>UTF-8</param-value>
          ? ?</init-param>
          ? </filter>
          ? <filter-mapping>
          ? ?<filter-name>CharsetEncodingFilter</filter-name>
          ? ?<url-pattern>/*</url-pattern>
          ? </filter-mapping>
          百思不得其解,問題出在哪里。
          在google搜索,有人提出這么幾個(gè)解決方案:
          1、試著把tomat/server/lib目錄下的commons-digester.jar,commons-beanutils.jar拷貝到common/lib/目錄??--經(jīng)測(cè)試不行
          2、里有個(gè)文章說tomcat里的bug,沒有實(shí)現(xiàn)javax.servlet.Filter的Filter會(huì)報(bào)這樣的錯(cuò)誤,看來tomcat在啟動(dòng)就初始化Filter實(shí)例,但是在filter中又沒有看到那段代碼沒有實(shí)現(xiàn)Filter,或者有代碼在啟動(dòng)時(shí)沒有實(shí)例化。--沒發(fā)現(xiàn)問題。

          這個(gè)問題網(wǎng)上也得不到解決方案,真是郁悶得緊
          ****************************************
          終于解決了,原來是在web.xml初始化參數(shù)過程少初始化了一個(gè),導(dǎo)致filter啟動(dòng)失敗。大汗~~~~

          如果以后出現(xiàn)tomcat 嚴(yán)重: Error filterStart 錯(cuò)誤,一般原因?yàn)椋?、xml配置失誤(如我) 2、filter中某段代碼為實(shí)例化(這個(gè)情況是出現(xiàn)最多的,要仔細(xì)檢查) 3、試著把tomat/server/lib目錄下的commons-digester.jar,commons-beanutils.jar拷貝到common/lib/目錄,有些包在部署是沒有被包含
          ?

          posted @ 2007-03-13 17:53 氓氓 閱讀(26654) | 評(píng)論 (40)編輯 收藏

          這里以Eclipse 3.1.2為例給介紹!

          一、下載 Eclipse 3.1.2 和語言包:
          ??
          ?? Eclipse 3.1.2:點(diǎn)此處下載eclipse-SDK-3.1.2-win32.zip
           
          ?? 語言包:點(diǎn)擊下載NLpack1-eclipse-SDK-3.1.1a-win32.zip

          二、解壓縮 eclipse-SDK-3.1.2-win32.zip 到 D:\eclipse(我放的路徑,閣下可以自己選擇)

          ??在 eclipse 目錄中新建一個(gè)名為 language 和 links 的目錄,并將下載下來的語言包 NLpack1-eclipse-SDK-3.1.1a-win32.zip 解壓縮到 language 目錄中,然后在 links 目錄里新建一個(gè)文本文件,命名為 language.link ,在文件里寫入下列信息:path=D:\\eclipse\\language 其中 D 為閣下所使用的盤符,設(shè)置 path 指向 language 目錄,保存即可。

          三,OK了,啟動(dòng) Eclipse 3.1.2,已經(jīng)是中文的了!

          其他版本同理,下載對(duì)應(yīng)語言包即可,網(wǎng)址:http://download.eclipse.org/eclipse/downloads/
          posted @ 2006-08-02 14:17 氓氓 閱讀(515) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 夏河县| 新郑市| 烟台市| 望谟县| 庄浪县| 泰安市| 大埔区| 拜泉县| 丹江口市| 井陉县| 宿迁市| 德庆县| 岳普湖县| 上林县| 合川市| 沂水县| 平远县| 贵溪市| 桐乡市| 红桥区| 陆良县| 上蔡县| 西盟| 施秉县| 呼伦贝尔市| 凉城县| 武宣县| 肇州县| 梁平县| 武邑县| 巫山县| 平安县| 涞源县| 兴仁县| 北票市| 本溪市| 临安市| 高安市| 涟源市| 武隆县| 上蔡县|