1

最近、Struts2 UI タグのチュートリアルを 1 つ行っています。それで、その例を見つけて完璧に実行しました。

しかし、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>

ここでは、index.jsp から populateRegier に 1 つのリクエストを入力しているので、RegisterAction.java にリダイレクトし、クラスの populate() メソッドを実行しています。つまり、次のようになります。

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;
    }
}

最初の質問:なぜここで populate() メソッドを実行しているのか理解できませんでしたか?

2 番目の質問:method="{1}" struts2.xmlの の意味は何ですか?

前もって感謝します...

4

2 に答える 2

2

2つの質問は同じ答えです。Struts 構成で次の行を見ると、次のようになります。

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

***** と{1}に気付くでしょう。struts が行っているのは、populateRegisterリクエストを受け取り、上記の <action> でワイルドカード マッチを実行することです。

ワイルドカードに一致する部分 (populate) を取り、それをメソッド名として使用します ({1} を populate に置き換えます)。これにより、nirmal.RegisterAction クラスで populate() メソッドが呼び出されます。

同じクラスで execute() メソッドを呼び出したい場合は、executeRegister リクエストを送信します。Struts サイトには、ワイルドカード マッピングに関する詳細情報があります。個人的には、構成をきれいに保つのに非常に役立つことがわかりました。

于 2010-07-15T12:04:24.347 に答える
1

Populate メソッドが呼び出されるのは、一部のデータを自動入力する必要があるためです。これは、ユーザーが選択または表示するのに役立ちます。デフォルトの選択にも役立ちます。

于 2011-02-22T10:38:53.467 に答える