0

在 Struts 1.x 中,我使用带有默认值的表单 bean 预先填充了表单文本字段,如下所示,

<html:form action="/actions/signup1">
First name: <html:text property="firstName"/><BR>

在表单bean中,我的默认值如下...

public class ContactFormBean {
private String firstName = "First name";

但是在 Struts 2.x 中,当我尝试如下使用 struts-tags 文本字段时,它没有从 bean 中预填充默认值,

<s:form action="signup1">
    <s:textfield name="formBean.firstName" label = "First Name" />

我在我的 Action 类中使用适当的 getter 和 setter 方法声明了 formBean...

public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
@Override
    public String execute() throws Exception {
....
}
    public ContactFormBean getFormBean(){
        return formBean;
    }
    public void setFormBean(ContactFormBean fb){
        formBean = fb;
    }
}

请让我知道这是否可以在请求级别而不是在会话级别完成。提前致谢。

<--已编辑-->

struts.xml

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="basicstruts2" extends="struts-default">

        <action name="index">
            <result>/index.jsp</result>
        </action>
        <action name="signup">
            <result>/SignUp.jsp</result>
        </action>


    <action name="signup1" class="coreservlets.action.SignupAction1" method="execute">
    <result name="success">/SignUp-Confirmation.jsp</result>
    <result name="error">/SignUp.jsp</result>

  </action>



    </package>

</struts>

注册.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign UP</title>
</head>
<body>
<H1 ALIGN="CENTER">Sign Up</H1>
<s:form>
    <s:textfield name="formBean.firstName" label = "First Name" />
    <s:textfield name="formBean.lastName" label = "Last Name" />
    <s:textfield name="formBean.email" label = "Email Address" />
    <s:textfield name="formBean.faxNumber" label = "Fax Number" />
    <s:submit action="signup1" method="loginAfterSubmit" value="Click here to Submit"/>
</s:form>
</body>
</html>

ContactFormBean.java

public class ContactFormBean {
    private String firstName = "First name";
    private String lastName = "Last name";
    private String email = "user@host";
    private String faxNumber = "xxx-yyy-zzzz";


    public String getFirstName() {
    return this.firstName;
    }
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return(lastName);
    }
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    public String getEmail() {
    return(email);
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public String getFaxNumber() {
    return(faxNumber);
    }
    public void setFaxNumber(String faxNumber) {
    this.faxNumber = faxNumber;
    }
public boolean isMissing(String value) {
        if ((value == null) || (value.trim().equals(""))) {
        return(true);
        } else {
        for(int i=0; i<defaultValues.length; i++) {
        if (value.equals(defaultValues[i])) {
        return(true);
        }
        }
        return(false);
        }
        }

}

SignupAction1.java

public class SignupAction1 extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private ContactFormBean formBean;

    @Override
    public String execute() throws Exception {
        this.formBean = new ContactFormBean();
        return SUCCESS;

    }

    public String loginAfterSubmit() {

        String firstName = formBean.getFirstName();
        String lastName = formBean.getLastName();
        String email = formBean.getEmail();
        String faxNumber = formBean.getFaxNumber();
        if (formBean.isMissing(firstName)) {
        return ERROR;
        } else if (formBean.isMissing(lastName)) {
        return ERROR;
        } else if ((formBean.isMissing(email)) ||
        (email.indexOf("@") == -1)) {
        return ERROR;
        } else if (formBean.isMissing(faxNumber)) {
        return ERROR;
        } else {
        return SUCCESS;
        }
    }


    public ContactFormBean getFormBean(){
                return this.formBean;
    }
    public void setFormBean(ContactFormBean fb){
        formBean = fb;
    }

}
4

4 回答 4

1

signup将您的操作声明更改为

<action name="signup" class="coreservlets.action.SignupAction1">
   <result>/SignUp.jsp</result>
</action>

signup1_

<action name="signup1" class="coreservlets.action.SignupAction1" method="signup1">

signup1在您的SignupAction1操作类中创建方法并将您的逻辑execute移至其中。然后创建你的execute新实例。ContactFormBean

于 2012-11-16T14:19:48.807 回答
0

将默认值放入构造函数:

public class ContactFormBean {
    private String firstname;
    public ContactFormBean (){
        this.firstName = "First name";
    }
}
于 2012-11-16T09:54:04.097 回答
0

您正在formBean使用该方法返回一个空对象getFormBean,然后构造函数永远不会被调用,并且尝试访问 formBean 属性firstName会给出错误(因为它被 JSP 上的 Struts2 标记包装,所以没有显示。

你可以

1)在您的执行方法上实例化它:

public String execute() throws Exception {
   this.formBean = new ContactFormBean();
}

2)在你的吸气剂上懒惰地实例化它:

public ContactFormBean getFormBean(){
    if (formBean==null) 
       this.formBean = new ContactFormBean();
    return this.formBean;
}

编辑(由于您的评论):

我不知道您是如何构建 Web 应用程序的;

但是,如果您在 JSP 上,则在呈现 JSP 之前调用 Action(及其 execute() 方法或其他方法,如果指定)。

因此,无论您是否有 ActionOne 加载内容并在提交后调用 ActionTwo,或者 ActionOne 加载内容并在提交后调用 ActionOne 的另一个方法,您都可以知道您是处于“pre-JSP”状态还是处于“post-submit”状态状态...

也就是说,如果您要公开一个 Object,并且希望它的值或其属性不同于null,则必须以上述方式之一实例化它。

显然,您的对象应该包含其属性的 getter 和 setter,并且它们必须已绑定到您的 JSP 对象。

在您的示例中,您可以:

联系FormBean:

public class ContactFormBean {
   private String firstName = "First name";
   public String getFirstName(){
      return this.firstName;
   }
   public String setFirstName(String firstName){
      this.firstName = firstName;
   }
}

你的行动:

public class SignupAction1 extends ActionSupport {
    private ContactFormBean formBean;

    public ContactFormBean getFormBean(){
        return formBean;
    }
    public void setFormBean(ContactFormBean fb){
        formBean = fb;
    }


    @Override
    public String execute() throws Exception {
       /* this method is executed before showing the JSP */

       /* first time initialization */
       this.formBean = new ContactFormBean();
       return SUCCESS;
    }

    public String loginAfterSubmit() throws Exception {
       /* this method is executed after the submit button was pressed */

       /* i don't initialize nothing here, 
          ContactFormBean is coming from the page */ 

       System.out.println("ContactFormBean's firstName value is: " +
                          this.formBean.getFirstName());
       return "goSomewhereElse";
    }

}

并在您的 JSP 中:

<s:form>
   <s:textfield name="formBean.firstName" label = "First Name" />
   <s:submit action="signup1" method="loginAfterSubmit" value="Press here to submit" />
</s:form>

您可以通过将execute() 和loginAfterSubmit() 方法拆分为两个不同Action 的两个execute() 方法来使用两个Action;

然后你必须在两个动作中都有你的formBean,至少第一个是getter,第二个是setter。

于 2012-11-16T10:19:22.727 回答
0

您还可以在执行方法中设置 bean 中的 Name 值,它将在您的 JSP 页面中填充它。示例是:

public String execute() throws Exception {
    this.formBean = new ContactFormBean();
    //Set your Name value here        
    formBean.setName("John Doe");
    return SUCCESS;
}

填充 JSP 后,您将在名称文本框中找到该值。希望这会有所帮助。

塔班

于 2012-11-16T15:04:44.803 回答