2010-07-13 66 views
1

最近我正在爲Struts2 UI標籤編寫一個教程。所以,我發現這個例子並且用完美的方式來執行它。Struts2配置瞭解

但是,在struts.xml配置文件中,我無法理解一些OGNL表達式。那我在這裏寫:

<struts> 
    <package name="default" extends="struts-default"> 
     <action name="*Register" method="{1}" class="nirmal.RegisterAction"> 
      <result name="populate">/register.jsp</result> 
      <result name="input">/register.jsp</result> 
      <result name="success">/success.jsp</result> 
     </action>   

    </package> 
</struts> 

在這裏,我在populateRegier從index.jsp的填充一個請求,所以它的它重定向到RegisterAction.java和執行填充()我的課的方法,即如下:

RegisterAction.java

package nirmal; 

import java.util.ArrayList; 

import com.opensymphony.xwork2.ActionSupport; 

public class RegisterAction extends ActionSupport { 

    private String userName; 
    private String password; 
    private String gender; 
    private String about; 
    private String country; 
    private ArrayList<Country> countryList; 
    private String[] community; 
    private ArrayList<String> communityList; 
    private Boolean mailingList; 

    public String populate() { 

     countryList = new ArrayList<Country>(); 
     countryList.add(new Country(1, "India")); 
     countryList.add(new Country(2, "USA")); 
     countryList.add(new Country(3, "France")); 

     communityList = new ArrayList<String>(); 
     communityList.add("Java"); 
     communityList.add(".Net"); 
     communityList.add("SOA"); 

     community = new String[]{"Java",".Net"}; 
     mailingList = true; 

     return "populate"; 
    } 
    public String execute() { 
     return SUCCESS; 
    } 
    public String getUserName() { 
     return userName; 
    } 
    public void setUserName(String userName) { 
     this.userName = userName; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String getGender() { 
     return gender; 
    } 
    public void setGender(String gender) { 
     this.gender = gender; 
    } 
    public String getAbout() { 
     return about; 
    } 
    public void setAbout(String about) { 
     this.about = about; 
    } 
    public String getCountry() { 
     return country; 
    } 
    public void setCountry(String country) { 
     this.country = country; 
    } 
    public ArrayList<Country> getCountryList() { 
     return countryList; 
    } 
    public void setCountryList(ArrayList<Country> countryList) { 
     this.countryList = countryList; 
    } 
    public String[] getCommunity() { 
     return community; 
    } 
    public void setCommunity(String[] community) { 
     this.community = community; 
    } 
    public ArrayList<String> getCommunityList() { 
     return communityList; 
    } 
    public void setCommunityList(ArrayList<String> communityList) { 
     this.communityList = communityList; 
    } 
    public Boolean getMailingList() { 
     return mailingList; 
    } 
    public void setMailingList(Boolean mailingList) { 
     this.mailingList = mailingList; 
    } 
} 

第一個問題:我不明白爲什麼它exeucting這裏填寫()方法?

第二個問題:在struts2.xml中method="{1}"是什麼意思?

在此先感謝...

+0

請給出明確的解釋 – SMS 2016-07-11 06:11:43

回答

2

2個問題的答案相同。如果您在支柱配置看這個行:

<action name="*Register" method="{1}" class="nirmal.RegisterAction"> 

你會發現一個*****和{1}。什麼struts正在做的是採取您的populateRegister請求和執行上述<行動>通配符匹配。

它需要通配符匹配部分(填充),並使用它作爲方法名(取代{1}與填入)。這是導致在您的nirmal.RegisterAction類中調用populate()方法的原因。

如果你想在同一個類中調用execute()方法,你會發送一個executeRegister請求。在struts網站上有關於wildcard mappings的更多信息。就我個人而言,我發現它們對於保持配置清潔非常有用。

1

填充方法被調用,因爲你需要一些數據進行自動填充這有助於用戶選擇或檢視它,它可以幫助你在你的默認選擇了。

+0

你能詳細說明一下嗎? – Tyrsius 2012-11-29 01:13:10