Struts2之Hello World | #struts2

[url=http://struts.apache.org/2.x/]Struts2[/url]官方推薦教程[url=http://www.vaannila.com/struts-2/struts-2-tutorial/struts-2-tutorial.html]Vaan Nila's Struts 2 Tutorial[/url]中[url=http://www.vaannila.com/struts-2/struts-2-example/struts-2-hello-world-example-1.html]Struts 2 Hello World Tutorial[/url]一節是Struts2極好的入門文章,很適合我們這些新人學習。

該文章以一個簡單的Hello World應用爲便,向我們展示了struts2的魅力。

該例的流程非常簡單:用戶輸入姓名後提交,在新的頁面中顯示問候語。

使用Struts二編寫這個Hello World,需要如下步驟:

[list=1]
[*]創建Web工程並添加struts包
[*]配置過濾器
[*]配置struts.xml
[*]編寫action和視圖
[*]佈署運行
[/list]

[b]1. 創建web工程並添加struts包[/b]

新建一個web工程Struts2Tutorial,在WEB-INFlib文件夾下加入

[list]
[*]struts2-core-2.1.8.1.jar
[*]xwork-core-2.1.6.jar
[*]freemarker-2.3.15.jar
[*]commons-logging-1.0.4.jar
[*]commons-fileupload-1.2.1.jar
[*]ognl-2.7.3.jar
[/list]

[b]2. 配置過濾器[/b]

打開web.xml文件,添加過濾器定義如下:
<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>


過濾器FilterDispatcher是Struts2的核心,最常見的用途就是將用戶請求轉發到不同的Action執行,並返回執行結果給用戶。

一般情況下,FilterDispatcher的url-pattern必須匹配所有用戶請求。

見:[url]http://struts.apache.org/2.1.6/struts2-core/apidocs/org/apache/struts2/dispatcher/FilterDispatcher.html[/url]
[quote]IMPORTANT: this filter must be mapped to all requests. Unless you know exactly what you are doing, always map to this URL pattern: /*[/quote]

[b]3. 配置struts.xml[/b]

struts.xml文件用於配置Action,該文件默認應放置在工程的classpath下面,如src文件夾下,當工程編譯後部署後,程序訪問的其實是WEB-INF/classes/struts.xml文件。

struts.xml的dtd文件可以在struts2-core-2.1.8.1.jar的根目錄下找到。

struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
<package name="default" extends="struts-default">
<action name="helloworld" class="dive.into.struts2.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>


其中pack標籤聲明瞭一個默認的包default,它繼承了struts的默認配置struts-default。

action標籤建立了一個名爲helloworld的Action,我們可以通過/helloworld.action訪問它。

result標籤定義了一個名SUCCESS的執行結果,它指向/success.jsp頁面。

[b]4. 編寫action和視圖[/b]

建立類dive.into.struts2.HelloWorld類,這是一個Action,但事實上你不用繼承什麼類或者實現什麼接口,只需要讓它符合以下規則即可:

  [b]一個返回值爲String類型的public方法execute()[/b]

  其返回值會用於識別struts.xml該Action下的result標籤,從而決定跳轉的地址。

  [b]一組屬性及其setter和getter,如果必要的話[/b]

  該屬性可以在jsp頁面的struts標籤直接訪問,非常方便。

[i][b]HelloWorld.java[/b][/i]
package dive.into.struts2;

public class HelloWorld {
private String userName;
private String message;

public String execute() {
setMessage("Hello, " + getUserName());
return "SUCCESS";
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}


[i][b]index.jsp[/b][/i]
<%-- 使用Struts2標籤庫 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World :: Struts2</title>
</head>
<body>

<%-- 創建一個提交地址爲名爲helloworld的Action的表單 --%>
<s:form action="helloworld">

<%--
創建一個文本域,表單提交後,該文本域的值會自動填充到
helloworld對應的Action對象的userName屬性上
--%>
<s:textfield name="userName" label="User Name"/>

<%-- 創建一個提交按鈕 --%>
<s:submit/>
</s:form>
</body>
</html>


[i][b]success.jsp[/b][/i]
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World :: Struts2</title>
</head>
<body>
<h1>
<%--
該頁面由helloworld跳轉而來
此標籤將會顯示helloworld實例的message屬性的值。
--%>
<s:property value="message"/>
</h1>
</body>
</html>


[b]5. 佈署運行[/b]

在tomcat中佈置工程並通過[url]http://localhost:8080/Struts2Tutorial[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章