天天看點

struts2 helloworld

學習的今天,在第一天struts2,從來沒有學過怎麼辦,那當然是helloworld。前一段時間去學習android。由于工作的一個項目,我們需要使用server。所有開始學習struts2,好感覺。學習的基本過程幾乎是相同的。那是,helloworld,開發環境,然後就是逐個依照知識點打demo,打着打着你就會發現struts2已經掌握地差點兒相同了,讓後就能夠action了。程式猿就須要不停地學習,學習能力當然是最重要地噢。

廢話講完。進入正題:

第一步:部署開發環境:

struts-2.3.1.1-all:struts2的各種jar檔案和例子。

官網http://struts.apache.org/release/2.1.x/

Tomcat server。(參考本人之前博文)

eclipse-jee-indigo-SR1-win32:Eclipse。注意是Java EE 版本号(由于涉及到Web開發)

也能夠是myeclipse(須要破解)

以下以 Eclipse為例。

打開 Eclipse 。先配置 tomcat 吧:

Windows -> preferences -> Server 下拉箭頭 -> Runtime Environments -> Add ->

選擇tomcat 7.x -> next -> Browse 選擇 tomcat 7 檔案夾 -> finish

第二步:建立一個web項目并倒入必須地包

建立 Dynamic Web Project (MyEclipse 裡面建立 Web項目也行):

打上項目名,假設剛才配置預設server成功,在 Target runtime 中應該有 tomcat ,能夠直接點 Finish。

項目結構例如以下:

struts2 helloworld

以下給出 strurts2 的基本配置(struts.xml web.xml 各種jar):

解壓struts-2.3.1.1-all.zip -> apps -> 解壓 struts2-blank.war(據說是配置 struts2 須要的最少 jar 檔案 ) -> WEB-INF 

-> 複制 web.xml -> 粘貼到 項目裡的 WEB-INF目錄裡(web.xml 内容正好不用改)

回到 struts2-blank.war -> lib -> 所有複制裡面的 jar 檔案 -> 項目 WEB-INF -> lib 目錄裡粘貼

回到 struts2-blank.war -> src -> java -> 複制 struts.xml -> 項目 Java Resources -> src 目錄裡粘貼(這個

struts.xml 裡面已有内容,待會删掉)

第三步:寫helloworld

在 WebContent下(不用放到 WEB-INF裡面)建立 Helloworld.jsp 

在Helloworld.jsp的 <body> </body>中寫上Helloworld。儲存。

打開 struts.xml。把 <struts> </struts>中間的都删了,又一次打上:

第四步:部署到tomcat

調出 Server 界面:

struts2 helloworld

在Servers界面裡右鍵 -> New -> Server

struts2 helloworld

選擇 tomcat 7.x -> next -> 把 struts2 (項目名) add 到右邊 -> Finish

struts2 helloworld

也能夠在外邊添加:

struts2 helloworld

右鍵 -> Start 

會出現一堆資訊。假設沒有嚴重錯誤,則server啟動完成。

以下訪問struts2進行驗證:

打開自己打開浏覽器,輸入http://localhost:8080/struts2/FB.action。回車,則跳轉到 Helloworld.jsp

以下在 action 内加入 class 來訪問 Helloworld.jsp 并顯示,打開 struts.xml 改動代碼為:

在 Java Resources -> src 建立 class 命名為 tohello,加入代碼為:

再打開 Helloworld.jsp 。将第一行的字元編碼改動為:charset=UTF-8,<body></body>裡面加入 ${message}<br/>

打開浏覽器又一次驗證

struts2 helloworld

這樣子每次改動都須要又一次啟動server,能夠使用熱部署。僅僅要改動代碼後自己主動重新啟動server:

改動struts.xml,在<struts>頭标簽以下添加: