我有一个类似的东西:
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;
}
}关于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>不幸的是,当我提交此表单时,只设置了Model类中的一个字段。我调试代码,发现实际上两个字段(field1和field2)都调用了setter,但是对于不同的模型类实例。
因此,当表单被提交时,它将与接下来的步骤一起执行:
instance1
因此,正如我所看到的,instanse2替换了instance1。在模型类的一个实例中,我需要field1和field2。有什么需要修改的?
受抚养人名单:
<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>发布于 2012-05-06 06:02:15
对于复杂类型,您需要一个getter和setter,这样Struts2就可以正确地操作对象,否则它将无法获得现有的实例,并且将被迫创建模型的新实例(getModel().setWhatever()),而不是看到模型已经存在并执行(getModel().setWhatever())。
https://stackoverflow.com/questions/10465339
复制相似问题