【问题标题】:Prepopulating form's textfields with bean default value in Struts2在 Struts2 中使用 bean 默认值预填充表单的文本字段
【发布时间】:2012-11-16 09:42:41
【问题描述】:

在 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 类中声明了 formBean,如下所示,并带有适当的 getter 和 setter 方法...

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>

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

}

【问题讨论】:

    标签: struts2


    【解决方案1】:

    将您的 signup 操作声明更改为

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

    signup1

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

    在您的 SignupAction1 操作类中创建方法 signup1 并将您的逻辑从 execute 移动到它。在execute 然后创建ContactFormBean 的新实例。

    【讨论】:

    • 谢谢你!有效。我之前没有在 struts.xml 中为注册 jsp 配置 Action 类,这就是问题所在。
    • @AleksandrM:我很确定 name 对于文本字段来说已经足够了,value 是不需要的,除非你想从 getter 中显示与直接值不同的东西:)
    • @AndreaLigios:是的,你是对的。打算从我的答案中删除它。
    【解决方案2】:

    把你的默认值放到构造函数中:

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

    【讨论】:

      【解决方案3】:

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

      你可以

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

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

      2) 在你的 getter 上懒惰地实例化它:

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

      编辑(由于您的 cmets):

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

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

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

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

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

      在您的示例中,您可以:

      ContactFormBean:

      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。

      【讨论】:

      • 好吧,在执行方法上实例化 formBean 会将我的表单输入值覆盖到 bean 字段中!也就是说,每次单击提交按钮时,控件都会执行 Action 类的方法,当我这样做时, String firstName = formBean.getFirstName();它选择默认值而不是表单输入值。所以我不应该在 execute 方法中有业务逻辑?
      • 另外,我认为 formBean 只有在调用 Action 类后才会被实例化。所以第一次渲染jsp表单时没有办法获取值。我的意思是只有在按下提交按钮并且控制转到 Action 类之后,bean 才会被实例化,并且在返回成功或失败并在 struts.xml 中相应地映射到这个 jsp 之后,这个文本字段是否会从 bean 中填充。所以我的问题是在初始渲染本身时填充 jsp 表单。希望你清楚。
      • 按照你说的,但仍然没有成功。让我把包括 struts.xml 在内的整个代码粘贴到上面的 cmets 下 请检查我是否按照你的方式进行操作。
      【解决方案4】:

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

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

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

      塔盘

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多