![](/img/trans.png)
[英]Struts2 execute multiple results off of one action from the struts.xml file
[英]Struts2 Execute method running before the actual request enters the struts.xml
在這個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()
方法(我還沒有按下Submit按鈕)。
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的工作方式。 如果您不希望運行填充方法,請添加另一個操作標簽以“顯示”頁面。
因此,使您像這樣使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.