views:

77

answers:

1

Hello All...

Recently I am going through with one tutorial for Struts2 UI Tags. So, I found that example and execute it with perfection.

But, in the struts.xml configuration file, I couldn't understand some of the OGNL expressions. That I am writing here :

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

    </package>
</struts>

Here I am populating one request at populateRegier from index.jsp, so it's redirecting it to RegisterAction.java and executing populate() method of my class, i.e. as follows :

RegisterAction.java

package nirmal;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

    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() {

        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;

        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;
    }
}

First Question : I couldn't understand why it's exeucting populate() method here ?

Second Question : What is the meaning of method="{1}" in struts2.xml ?

Thanks in advance...

+2  A: 

The 2 questions have the same answer. If you look at this line in your struts config:

<action name="*Register" method="{1}" class="nirmal.RegisterAction">

You'll notice a * and the {1}. What struts is doing is taking your populateRegister request and performing a wildcard match on the above <action>.

It takes the wildcard matched portion (populate) and uses it as the method name (replaces {1} with populate). This is what causes the populate() method to be invoked in your nirmal.RegisterAction class.

If you wanted to invoke the execute() method in the same class, you would send an executeRegister request. There's more information about wildcard mappings on the struts site. Personally I've found them to be very useful for keeping the config clean.

Pat