java - Struts2配置理解

标签 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">
            <result name="populate">/register.jsp</result>
            <result name="input">/register.jsp</result>
            <result name="success">/success.jsp</result>
        </action>        

    </package>
</struts>

这里我在 populateRegier 从 index.jsp 填充一个请求,所以它将它重定向到 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;
    }
}

第一个问题:我不明白为什么要在这里执行 populate() 方法?

第二个问题:struts2.xml中的method="{1}"是什么意思?

提前致谢...

最佳答案

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

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

您会注意到 ***** 和 {1}。 struts 正在做的是接收您的 populateRegister 请求并对上面的 执行通配符匹配。

它采用通配符匹配部分(填充)并将其用作方法名称(将 {1} 替换为填充)。这就是导致在您的 nirmal.RegisterAction 类中调用 populate() 方法的原因。

如果您想在同一个类中调用 execute() 方法,您将发送一个 executeRegister 请求。有关 wildcard mappings 的更多信息在支柱网站上。我个人发现它们对于保持配置清洁非常有用。

关于java - Struts2配置理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236566/

相关文章:

java - 可配置(例如 XML)Java Bean 到 Bean 映射框架

java - hibernate 中延迟加载对象属性的限制

rest - 在 JAX RS 中,返回 Response 和 Bean 或 Collection of Beans (DTO) 之间的区别

java - ExceptionMapper (Java EE 6) 可以处理 NON RuntimeException 吗?

Hibernate 在更新父实体的同时更新子实体

java - java中的DateTimePicker格式

java.lang.ang.NoClassDefFoundError : org/hibernate/util/DTDEntityResolver 错误

java - 如何用java制作Excel表格

java - 如何使用 antrun 插件通过 maven 将类作为 jar 文件运行

java - Struts2 将空字符串参数转换为 "int"