title: Prepopulating form's textfields with bean default value in Struts2
body: <p>In Struts 1.x , I have prepopulated the forms text fields using form bean's with default values as follows,</p>
  
  <pre><code>&lt;html:form action="/actions/signup1"&gt;
  First name: &lt;html:text property="firstName"/&gt;&lt;BR&gt;
  </code></pre>
  
  <p>And in form bean, I have default value as follows...</p>
  
  <pre><code>public class ContactFormBean {
  private String firstName = "First name";
  </code></pre>
  
  <p>But in Struts 2.x , when I tried with struts-tags textfield as follows, its not prepopulating the default value from the bean,</p>
  
  <pre><code>&lt;s:form action="signup1"&gt;
      &lt;s:textfield name="formBean.firstName" label = "First Name" /&gt;
  </code></pre>
  
  <p>I have the formBean declared in my Action class as follows with appropriate getter and setter methods...</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>Please let me know if this can be accomplished at Request level and not at session level.
  Thanks in advance.</p>
  
  <p>&lt;--Edited--></p>
  
  <p><strong>struts.xml</strong></p>
  
  <pre><code>&lt;struts&gt;
  
      &lt;constant name="struts.devMode" value="true" /&gt;
  
      &lt;package name="basicstruts2" extends="struts-default"&gt;
  
          &lt;action name="index"&gt;
              &lt;result&gt;/index.jsp&lt;/result&gt;
          &lt;/action&gt;
          &lt;action name="signup"&gt;
              &lt;result&gt;/SignUp.jsp&lt;/result&gt;
          &lt;/action&gt;
  
  
      &lt;action name="signup1" class="coreservlets.action.SignupAction1" method="execute"&gt;
      &lt;result name="success"&gt;/SignUp-Confirmation.jsp&lt;/result&gt;
      &lt;result name="error"&gt;/SignUp.jsp&lt;/result&gt;
  
    &lt;/action&gt;
  
  
  
      &lt;/package&gt;
  
  &lt;/struts&gt;
  </code></pre>
  
  <p><strong>SignUp.jsp</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Sign UP&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;H1 ALIGN="CENTER"&gt;Sign Up&lt;/H1&gt;
  &lt;s:form&gt;
      &lt;s:textfield name="formBean.firstName" label = "First Name" /&gt;
      &lt;s:textfield name="formBean.lastName" label = "Last Name" /&gt;
      &lt;s:textfield name="formBean.email" label = "Email Address" /&gt;
      &lt;s:textfield name="formBean.faxNumber" label = "Fax Number" /&gt;
      &lt;s:submit action="signup1" method="loginAfterSubmit" value="Click here to Submit"/&gt;
  &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>ContactFormBean.java</strong></p>
  
  <pre><code>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&lt;defaultValues.length; i++) {
          if (value.equals(defaultValues[i])) {
          return(true);
          }
          }
          return(false);
          }
          }
  
  }
  </code></pre>
  
  <p><strong>SignupAction1.java</strong></p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>