In this Struts2 example , i am trying to test the struts tags,
while working on it, i found that the execute()
method (populate()
in my case is running before the request is coming for that method from register.jsp
page :
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;
}
}
when i am running the application in eclipse it first shows up register.jsp
page.
but along with that it also shows the constructor being run(which is fine ) but the populate()
method also been called (i haven't pressed submit button yet) .
INFO: Server startup in 5904 ms
#####inside register action####.....inside populate method.........********exiting populate*********
now i am pressing the submit button the success.jsp page is displayed along with the default constructor for RegisterAction
, but the populate method isn't(which is supposed to run once the submit is received by struts.xml):
#####inside register action####
is it normal behavior? if yes than why is it so because the request from the register.jsp will only enter the struts.xml once we press the submit button. Please help me understand. Let me know if information is insufficient before flagging negative ,as i am in danger to be banned.
Yep that's how Struts works. If you don't want the populate method to run, add another action tag for "showing" the page.
So making you're struts XML like this, means /ShowRegister will render the page. Then you post the form to the /Register and it will actually run the populate method and do the work for you.
<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>
This is a good doc on the Struts Arch.
http://www.roseindia.net/struts/struts2/struts-2-architecture.shtml