title: Trimming Struts2 textfield string input
body: <p>What is the best way to trim this string/where is the best place to put the trim code?</p>
  
  <p>Say I have the following textfield in my jsp:</p>
  
  <pre><code>&lt;s:textfield label="First Name" name="person.firstname"/&gt;
  </code></pre>
  
  <p>The action class:</p>
  
  <pre><code>public class BaseAction extends ActionSupport implements ServletRequestAware, SessionAware {
      private Person person;
      // Getters, setters and action logic
  }
  </code></pre>
  
  <p>The bean: </p>
  
  <pre><code>public class Person implements Serializable {
      private String lastname;
      private String firstname;
      // Getters and setters
  }
  </code></pre>
  
  <p>I can change the default setting in the bean but this seems like a hack:</p>
  
  <pre><code>public void setFirstname(String firstname) {
      this.firstname = firstname.trim();
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: I did also see this question: <a href="https://stackoverflow.com/questions/2499530/struts2-trim-all-string-obtained-from-forms?rq=1">struts2 trim all string obtained from forms</a> where it's also suggested by some that the "correct" method is to use an interceptor.  </p>
  
  <p>Why is an interceptor the "correct" way? What is so wrong about changing the bean's setters?</p>