2013年8月20日 星期二

簡單的用struts2框架做一個 Hello World 練習網站


※Struts2基礎練習(Hello Struts)


在這一篇中會帶領大家建置簡單的Struts架構網站,主要是要讓各位了解Struts的架構


在建立專案時,我們會需要用到一些Struts的jar檔,所以要到Struts的官網http://struts.apache.org/download.cgi#struts2320下載


Eclipse和JDK的部份,在另一個網誌中有整個環境的配置方式,在這就不多說了(傳送門

我們到tomcat的官網http://tomcat.apache.org/download-70.cgi下載我們所需要東西,這次我們下載的是核心的版本,你可以斟酌選擇自己需要的版本下載即可


下載後我們隨便找一個磁碟區將tomcat放進去,然後打開eclipse進行環境設置

首先,我們開一個Dynamic Web Project,專案名稱設置為Hello Struts2,容器的部份,點選New Runtime後,將路徑指向剛下載好的tomcat資料夾,以及jre的版本

-------------------------------------------------------------------------------------------------------------------------

最後請再次確認你的系統環境變數

Server(伺服器):apache-tomcat-7.0.26

環境:JDK7、Struts2

-------------------------------------------------------------------------------------------------------------------------

※Struts2 : Hello World 練習


開始之前,請將系統架構建置完成,並且將本次練習會用到的jar檔放到lib中




完成了前置作業,可以開始來撰寫程式部份

下面會解說每一程式在Struts2中扮演的角色


※web.xml

web.xml為程式的起始檔案,在tomcat在執行時,會優先讀入web.xml中的所有設定,如果有對這一個部份想深入了解的朋友,可以參考tomcat的生命週期

*程式碼擷圖

程式碼說明

xml宣告部份
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" 
version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >

 <display-name>struts2 Hello Word </display-name> 

註冊Struts2過濾器
 <filter> 
         <filter-name>Struts2</filter-name>
         <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
         </filter-class> 
 </filter> 

註冊Struts2過濾器中觸發的事件
在這邊將所有的url都交由命名為Struts2的這個過濾器處理,如果想深入了解可以參考有關過濾器的文獻
 <filter-mapping>
               <filter-name>Struts2</filter-name> 
               <url-pattern>/*</url-pattern>
 </filter-mapping>

設定首頁(可以多個)
 <welcome-file-list> 
               <welcome-file>index.jsp</welcome-file>
 </welcome-file-list> 
</web-app>



※index.jsp

*程式碼擷圖

*程式碼說明

jsp宣告,包含宣告了使用Struts tags
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

JSP架構的部份就不多說了,照打
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p>

這裡加了一個連結,由Struts tage中的s:url導向
struts.xml,並且會去找一個叫hello的action
<a href="<s:url action='hello'/>">Hello World</a>

</p>
</body>
</html>

※struts.xml

struts.xml是程式配置檔,它會將index.jsp中的hello轉發請求給action的HelloWorldAction.java,並且接收HelloWorldAction.java回傳的參數後傳給HelloWorld.jsp

*程式碼擷圖


*程式碼說明

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <constant name="struts.devMode" value="true" />
  <package name="basicstruts2" extends="struts-default">
  <action name="index">
    <result>/index.jsp</result>
  </action>

上面的部份是xml宣告及首頁處理的部份

在我們接到來自index.jsp傳來的請求後,會在<struts>中找對

應的action,然後將流程導向指定的class的method,然後處

理class傳回來的回應(result),在這次練習中只會回傳success

這個值,然後將流程導向HelloWorld.jsp

  <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
    <result name="success">/HelloWorld.jsp</result>
  </action>
</package>
</struts>

※HelloWorldAction.java
*程式碼擷圖

*程式碼說明
package org.apache.struts.helloworld.action;

import org.apache.struts.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;


HelloWorld中繼承了ActionSupport而這個class中會回傳一個SUCCESS的常數,使用者不需要在額外去宣告一個常數
在ActionSupport中就幫使用者建立一個常數了

public class HelloWorldAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private MessageStore messageStore; 
     
    public String execute() throws Exception {
         
        messageStore = new MessageStore() ;
        return SUCCESS;
    }

    public MessageStore getMessageStore() {
        return messageStore;
    }

    public void setMessageStore(MessageStore messageStore) {
        this.messageStore = messageStore;
    }

}


※MessageStore.java
*程式碼擷圖

*程式碼說明

我們在這建了一個Bean,主要是以後加上Spring時可以直接使用到這個Bean
package org.apache.struts.helloworld.model;
public class MessageStore {     
    private String message;
         public MessageStore() {
         setMessage("Hello Struts User");
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
P.S  Eclipse中有自動幫助使用者建立get/set以及Override等等
在class中點選右鍵選擇Surce後選擇需要自動增加的選項

※HelloWorld.jsp
*程式碼擷圖

*程式碼說明

taglib的宣告說明要使用Struts的tag lib,並且指定前置字元為s

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World!</title>
</head>
<body>

<s:property value="messageStore.message" />,會呼叫messageStore物件的getMessage方法

    <h2><s:property value="messageStore.message" /></h2>
</body></html>

※最後Struts跑起來的擷圖如下





如果在執行時彈出錯誤訊息請重新確認你有沒有打錯字


以及你的jar檔有沒有少放了


還有你的檔案有沒有放對目錄



沒有留言:

張貼留言