本文地址:[http://blog.csdn.net/sushengmiyan/article/details/40479299](http://blog.csdn.net/sushengmiyan/article/details/40479299)
官方文檔:?[http://struts.apache.org/release/2.3.x/docs/coding-struts-2-actions.html](http://struts.apache.org/release/2.3.x/docs/coding-struts-2-actions.html)[](http://docs.sencha.com/extjs/5.0/apidocs/#!/api/Ext-method-each)
本文作者:[sushengmiyan](http://blog.csdn.net/sushengmiyan)
------------------------------------------------------------------------------------------------------------------------------------
其實學習struts2基礎部分,個人感覺,到前四篇已經可以有個直觀的了解和掌握了,就可以在應用中正常使用struts了,其它struts2的特性,久可以慢慢琢磨API了。
現在再將struts2的一個教程給解釋一下,也算是備用吧,后期可以在指導他人學習struts2的時候供參考。
編寫struts2的代碼只需要三步:
### 1.映射一個action到class
action和class的映射是在struts.xml里面配置的,之前的一個配置如下:
~~~
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
~~~
以上配置就指定了一個hello的action對應于org.apache.struts.helloworld.action.HelloWorldAction class
### 2.映射一個結果到view
~~~
<result name="success">/HelloWorld.jsp</result>
~~~
這個就是將success的結果映射到HelloWorld.jsp這個view中。
### 3.編寫action的處理邏輯
~~~
public String execute() throws Exception {
messageStore = new MessageStore() ;
helloCount++;
return SUCCESS;
}
~~~
這個是class對應的一個方法,是處理事務邏輯的地方。根據你的處理,返回處理結果,如success
這個地方有必要說一下整個的處理過程:
首先,登陸界面,接受用戶的input標簽的數據輸入(用戶名、密碼)
接著,根據struts.xml配置文件,找到對應的用戶名密碼的set方法,將輸入數值設置到對應的類對象中
然后,調用了httprequest方法,獲取剛剛存入對象的輸入數據(用戶名、密碼)
接著,執行execute方法,返回處理結果(如success)
最好,根據處理結果,顯示view給用戶(result.jsp)
這就是struts2的整個處理流程,感覺,熟悉了這個流程,在自己的程序中增加struts2已經很簡單了。