本文地址:[http://blog.csdn.net/sushengmiyan/article/details/40349201](http://blog.csdn.net/sushengmiyan/article/details/40349201)
官方文檔:[?http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html](http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html)[](http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html)[](http://docs.sencha.com/extjs/5.0/apidocs/#!/api/Ext-method-each)
本文作者:[sushengmiyan](http://blog.csdn.net/sushengmiyan)
------------------------------------------------------------------------------------------------------------------------------------
當你在Struts 2 web應用中點擊一個超鏈接或者提交一個form表單數據時,輸入的數據并沒有傳到服務器的另一個頁面,而是到了你提供的一個Java類. 這些類就叫做Actions. 當Action被fire, 結果會選擇一個資源呈現響應. 通常情況下,資源是一個網頁頁面, 也可以是PDF文件, 或者是Excel頁簽, 或者是一個Java applet窗口.
假設你想創建一個簡單的"Hello World" 例子展示歡迎信息. 在簡單設定struts工作環境之后(參照?[如何創建struts 2 web應用](#)), 創建"Hello World"例子,你需要做如下四件事情:
1. 創建一個存儲歡迎信息的java類(模型model)
1. 創建一個呈現信息的頁面(視圖 view)
1. 創建一個Action類來控制用戶、模型和視圖之間的關系(控制器controller)
1. 創建一個mapping (struts.xml) 來組合Action類和視圖
這篇文章假定你已經完成了 怎樣創建一個Struts 2 Web應用 體驗并且擁有一個基礎的struts工作空間. 這篇helloworld體驗的源碼,可以從Struts 2 GitHub 庫https://github.com/apache/struts-examples下載. 例子工程使用maven管理依賴和構造.war文件.
### 第一步- 創建模型類 MessageStore.java
在我們第一個例子中,我們新建一個包com.susheng.struts2Maven.bean在里面新建一個類MessageStore,內容如下:
~~~
package com.susheng.struts2Maven.bean;
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;
}
}
~~~

### 第二步 - 創建Action類 HelloWorldAction.java
新建包com.susheng.struts2Maven.action在此包下新建類HelloWorldAction,內容如下:
~~~
package com.susheng.struts2Maven.action;
import com.opensymphony.xwork2.ActionSupport;
import com.susheng.struts2Maven.bean.MessageStore;
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;
}
}
~~~

### 第三步 - 創建View HelloWorld.jsp
內容如下:
~~~
<%@ 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>
<h2><s:property value="messageStore.message" /></h2>
</body>
</html>
~~~

### 第四步 - 增加Struts配置到struts.xml
完整內容如下:
~~~
<?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>
<action name="hello" class="com.susheng.struts2Maven.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
~~~

### 第五步 - 創建URL Action
index.jsp修改為如下:
~~~
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>
~~~

### 第六步 - 構造WAR文件并運行程序
我構造的war包是basic_struts.war所以我訪問鏈接:http://localhost:8080/basic_struts/index.action,進入之后點擊helloworld鏈接,正常跳轉。

第二篇實際例子就學習完畢。
struts2的工作原理:
