Search code examples
struts2struts2-spring-plugin

Struts 2 submit form to one object


I have something similar to that:

 class Model {
    private String field1;
    private String field2;

    //setters
 }

class Action extends ActionSupport {
    private Model model;
    public String execute() {
        //breakpoint
    }
    public void setModel(Model model){
        this.model=model;
    }
}

on jsp:

<s:form id="addCommentForm" method="POST" namespace="%{namespace}" action="addComment">
    <input type="text" name="model.field1"/>
    <input type="text" name="model.field2"/>
</s:form>

When I submit this form unfortunately only 1 field in Model class is set. I debug code and find that actually setters are called for both fields (field1 and field2), but for different instances of Model class.

So it appears, that it executes with next steps when form is submitteed:

  1. create new instance (instance1) of Model class, set this instance in Action class
  2. set field1 to instance1
  3. create new instance (instance2) of Model class, set this instance in Action class
  4. set field2 to instance2

So as I see instanse2 replace instance1. I need field1 and field2 in one instance of Model class. What need to be modified?

list of dependenced:

        <dependency>
            <groupId>com.vercer.engine.persist</groupId>
            <artifactId>twig-persist</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.opensymphony</groupId>
            <artifactId>xwork</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.19</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>opensymphony</groupId>
            <artifactId>sitemesh</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>jetty</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1-6.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.1.8</version>
        </dependency>

Solution

  • For complex types you need both a getter and setter so Struts2 can manipulate the object correctly, otherwise it will not be able to get the existing instance and will be forced to create a new instance of Model (new Model().setWhatever()) as opposed to seeing a model already exists and doing (getModel().setWhatever()).