0

在这个 Struts2 示例中,我正在尝试测试 struts 标签,

在处理它时,我发现该execute()方法(populate()在我的情况下是在请求来自register.jsp页面的该方法之前运行:

index.jsp:

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

注册.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>

注册动作.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,但 populate 方法不是(它应该在 struts.xml 接收到提交后运行):

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

这是正常行为吗?如果是,那么为什么会这样,因为来自 register.jsp 的请求只有在我们按下提交按钮后才会进入 struts.xml。请帮我理解。在标记负面之前让我知道信息是否不足,因为我有被禁止的危险。

4

1 回答 1

1

是的,这就是 Struts 的工作方式。如果您不想运行填充方法,请添加另一个操作标记以“显示”页面。

因此,让您像这样使用 struts 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 回答