0

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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Deplake
  • 865
  • 2
  • 9
  • 22
  • Anything interesting in the configuration? Plugins? Interceptor stacks? Correct dependencies? – Dave Newton May 05 '12 at 20:40
  • I added list of dependenses. Also I use spring struts integration and every action class has @Scope(value = "prototype") annotation, because this actions are created in spring – Deplake May 05 '12 at 20:48
  • You're mixing up a bunch of S2 and XW2 dependencies there; first fix the dependencies so they make sense--you can't arbitrarily mix-and-match versions. JSP API should be listed as "provided". – Dave Newton May 05 '12 at 21:17
  • I marked JSP API dependency as provided. Anything else? – Deplake May 07 '12 at 08:14
  • Yes, the other words I said about mixing dependency versions. – Dave Newton May 07 '12 at 09:08

1 Answers1

2

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()).

Quaternion
  • 10,380
  • 6
  • 51
  • 102