天天看點

Struts2 學習筆記03 Action

  關于Struts2 裡的action,<result name="xxxx"></result>可以定義name屬性根據視圖的傳回由使用者自己定義的Action來決定 ,具體的方法是根據傳回的字元串來對應結果項。 

stuts.xml

<struts>

    <!-- Add packages here -->
    <constant name="struts.devMode" value="true" />
    <package name="front" namespace="/" extends="struts-default">
    
		<action name="hell" class="com.tfj.strut2.IndexAction1">
			<result name="success">
				/index.jsp
			</result>
		</action>
       
    </package>
    

</struts>
           

有一個屬性叫class。我們建立了一個類,命名為IndexAction.java,在com.tfj.struts下,如圖。

Struts2 學習筆記03 Action

IndexAction1.java

package com.tfj.strut2;
import com.opensymphony.xwork2.ActionSupport;


public class IndexAction1 extends ActionSupport{

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return "success";
	}
	
}
           

當調用時傳回success,打開jsp頁面。要實作改實驗有三種方法,1.自己定義execute()方法。2.實作Action接口 。3.從ActionSupport繼承,開發中用第三種,因為可以調用ActionSupport的其他方法。

實驗結果

Struts2 學習筆記03 Action
Struts2 學習筆記03 Action