gpt4 book ai didi

java - Struts2 Execute方法在实际请求进入struts.xml之前运行

转载 作者:行者123 更新时间:2023-12-01 12:30:05 25 4
gpt4 key购买 nike

在这个 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>

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 的默认构造函数一起显示,但 populate 方法不会(该方法应该在收到提交后运行)通过struts.xml):

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

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

最佳答案

是的,这就是 Struts 的工作原理。如果您不希望运行 populate 方法,请添加另一个用于“显示”页面的操作标记。

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

关于java - Struts2 Execute方法在实际请求进入struts.xml之前运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26001865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com