0

この Struts2 の例では、struts タグをテストしようとしています。

それに取り組んでいる間、execute()メソッドが(populate()私の場合、ページからそのメソッドのリクエストが来る前に実行されていることがわかりましたregister.jsp

index.jsp:

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=populateRegister"> 

register.jsp

<body>
<s:form action="Register">
    <s:textfield name="userName" label="User Name" />
    <s:password name="password" label="Password" />
    <s:radio name="gender" label="Gender" list="{'Male','Female'}" />
    <s:select name="country" list="countryList" listKey="countryId"
        listValue="countryName" headerKey="0" headerValue="Country"
        label="Select a country" />
    <s:textarea name="about" label="About You" />
    <s:checkboxlist list="communityList" name="community" label="Community" />
    <s:checkbox name="mailingList"
        label="Would you like to join our mailing list?" />
    <s:submit />
</s:form>
</body>

struts.xml

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

RegisterAction.java:

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

    public RegisterAction(){System.out.print("#####inside register action####");}

    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() {
        System.out.print(".....inside populate method.........");
        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;
        System.out.print("********exiting populate*********");
        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;
    }

}

Eclipse でアプリケーションを実行すると、最初にregister.jspページが表示されます。

しかし、それに加えて、コンストラクターが実行されていることも示しています(これは問題ありません)が、populate()メソッドも呼び出されています(まだ送信ボタンを押していません)。

INFO: Server startup in 5904 ms
#####inside register action####.....inside populate method.........********exiting         populate*********

今、私は送信ボタンを押しています.success.jspページがデフォルトのコンストラクターとともに表示されますRegisterAction.

#####inside register action####

それは正常な動作ですか?送信ボタンを押すと、register.jsp からの要求が struts.xml にのみ入力されるためです。理解を助けてください。私は禁止される危険にさらされているため、否定的なフラグを立てる前に情報が不十分である場合はお知らせください.

4

1 に答える 1

1

ええ、それが Struts の仕組みです。populate メソッドを実行したくない場合は、ページを「表示」するための別のアクション タグを追加します。

したがって、このように XML をストラットにすることは、/ShowRegister がページをレンダリングすることを意味します。次に、フォームを /Register に投稿すると、実際に populate メソッドが実行され、作業が行われます。

<struts>
  <package name="default" extends="struts-default">
    <action name="ShowRegister" class="vaannila.RegisterAction">
        <result name="success">/success.jsp</result>
    </action>  
    <action name="Register" method="populate" class="vaannila.RegisterAction">
        <result name="populate">/register.jsp</result>
        <result name="input">/register.jsp</result>
        <result name="success">/success.jsp</result>
    </action>        
</package>

これは、Struts Arch に関する優れたドキュメントです。

http://www.roseindia.net/struts/struts2/struts-2-architecture.shtml

于 2014-09-23T18:20:20.997 に答える