Java Struts2配置理解

Java Struts2配置理解,java,jakarta-ee,struts2,struts-html,Java,Jakarta Ee,Struts2,Struts Html,最近我正在学习Struts2 UI标记的一个教程。所以,我找到了这个例子,并完美地执行它 但是,在struts.xml配置文件中,我无法理解一些OGNL表达式。我在这里写的是: <struts> <package name="default" extends="struts-default"> <action name="*Register" method="{1}" class="nirmal.RegisterAction">

最近我正在学习Struts2 UI标记的一个教程。所以,我找到了这个例子,并完美地执行它

但是,在struts.xml配置文件中,我无法理解一些OGNL表达式。我在这里写的是:

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

/register.jsp
/register.jsp
/success.jsp
在这里,我从index.jsp在populateRegier中填充了一个请求,因此它将其重定向到RegisterAction.java并执行我的类的populate()方法,即如下所示:

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;
    }
}
packagenirmal;
导入java.util.ArrayList;
导入com.opensymphony.xwork2.ActionSupport;
公共类RegisterAction扩展了ActionSupport{
私有字符串用户名;
私有字符串密码;
私人字符串性别;
私人串;
私人国家;
私有ArrayList国家列表;
私人社区;
私人ArrayList社区列表;
私有布尔邮件列表;
公共字符串填充(){
countryList=新的ArrayList();
国家列表。添加(新国家(1,“印度”);
国家列表。添加(新国家(2,“美国”);
国家列表。添加(新国家(3,“法国”);
communityList=新的ArrayList();
communityList.add(“Java”);
communityList.add(“.Net”);
社区列表。添加(“SOA”);
community=新字符串[]{“Java”,“.Net”};
mailingList=true;
返回“填充”;
}
公共字符串execute(){
回归成功;
}
公共字符串getUserName(){
返回用户名;
}
public void setUserName(字符串用户名){
this.userName=用户名;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getGender(){
返回性别;
}
公共无效设置性别(字符串性别){
这个。性别=性别;
}
公共字符串getAbout(){
返回大约;
}
公共无效设置(字符串设置){
这个;
}
公共字符串getCountry(){
返回国;
}
公共国家/地区(字符串国家/地区){
这个国家=国家;
}
公共阵列列表getCountryList(){
返回国家列表;
}
公共无效setCountryList(ArrayList countryList){
this.countryList=countryList;
}
公共字符串[]getCommunity(){
返回社区;
}
公共void集合社区(字符串[]社区){
这个社区=社区;
}
公共阵列列表getCommunityList(){
返回社区列表;
}
公共无效setCommunityList(ArrayList communityList){
this.communityList=社区列表;
}
公共布尔getMailingList(){
返回邮件列表;
}
公共无效设置邮件列表(布尔邮件列表){
this.mailingList=mailingList;
}
}
第一个问题:我不明白为什么在这里执行populate()方法

第二个问题:struts2.xml中
method=“{1}”
的含义是什么


提前感谢…

这两个问题的答案相同。如果您查看struts配置中的这一行:

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

struts站点上的
。就我个人而言,我发现它们对于保持配置的整洁非常有用。

之所以调用填充方法,是因为您需要自动填充一些数据,这有助于用户选择或查看,也有助于您进行默认选择。

您能详细说明一下吗?请给出明确的解释