Struts 2:从具有模型驱动架构的表单更新对象列表 [英] Struts 2: updating a list of objects from a form with model driven architecture

查看:21
本文介绍了Struts 2:从具有模型驱动架构的表单更新对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里搜索并找到了几种方法,但我无法让它们为我的项目工作.

I already searched and found several approaches here, but I can't get them working for my project.

我想显示一个对象列表的编辑页面,所有这些对象都应该一次更新.我使用模型驱动的架构方法来实现这一点,但我无法让它正常运行.我总是可以显示和迭代列表及其值,但我不能修改它的值.

I want to show an edit page for a list of objects, which should all be updated at once. I use the model driven architecture approach to achieve this, but I can't get it running properly. I can always display and iterate the list and its values, but I can't modify its values.

这就是我目前正在做的事情:

So here is what I'm currently doing:

我的数据库中有一个模型Teilzeitgrad",它有一些带有 getter 和 setter 的简单属性.

I have a Model 'Teilzeitgrad' in my database, which has some simple attributes with getters and setters.

public class Teilzeitgrad {

    private Date datumAb;
    private Date datumBis;
    private double betrag;

    // ... getters and setters

}

在我的 Action-Class 中,我使用 Teilzeitgrad 对象列表实现了 ModelDriven 接口

In my Action-Class I implement the ModelDriven Interface with a List of Teilzeitgrad-Objects

public class DienstabschnittViewJahrAction implements ModelDriven<List<Teilzeitgrad>>, Preparable
{
    List<Teilzeitgrad> teilzeitgrads;
    private String tzgTypKey;
    private Integer jahrIndex;

    public String execute() {
        return SUCCESS;
    }

    public List<Teilzeitgrad> getModel()
    {
        if(teilzeitgrads == null) {
            teilzeitgrads = getTeilzeitgradListByTypAndJahr(getTzgTypKey(), getJahrIndex());
        }
        return teilzeitgrads;
    }

    public List<Teilzeitgrad> getTeilzeitgrads()
    {
        return teilzeitgrads;
    }

    public void setTeilzeitgrads(List<Teilzeitgrad> teilzeitgrads)
    {
        this.teilzeitgrads = teilzeitgrads;
    }

    @Override
    public void prepare() throws Exception
    {
        // TODO Auto-generated method stub  
    }

    public String getTzgTypKey()
    {
        return tzgTypKey;
    }

    public void setTzgTypKey(String tzgTypKey)
    {
        this.tzgTypKey = tzgTypKey;
    }

    public Integer getJahrIndex()
    {
        return jahrIndex;
    }

    public void setJahrIndex(Integer jahrIndex)
    {
        this.jahrIndex = jahrIndex;
    }
}

struts.xml 中的动作映射定义如下:

The action mapping in struts.xml is defined as follows:

<action name="*/auth/GroupAdmin/processEditDienstabschnittJahr" method="execute" class="org.hocon.ul.portal.action.DienstabschnittViewJahrAction">
    <result name="success" type="redirect">${referer}</result>
</action>

在我的 JSP 文件中,我正在迭代模型对象,在文本字段或列表中显示其值,如下所示:

In my JSP File I'm iterating the model object, displaying its values in textfields or lists as follows:

<ul:form action="auth/GroupAdmin/processEditDienstabschnittJahr">
<s:iterator value="model" status="rowStatus">

<tr>
    <td style="text-align: center;">
        <s:date name="model.get(#rowStatus.index).datumAb" var="datumAb_DE" format="dd.MM.yyyy" />
        <s:textfield style="width:70px;" name="model.get(#rowStatus.index).datumAb" value="%{#datumAb_DE}" label="DatumAb"></s:textfield >
    </td>

    <td style="text-align:center;">
        <s:date name="model.get(#rowStatus.index).datumBis" var="datumBis_DE" format="dd.MM.yyyy" />
        <s:textfield style="width:70px;" name="model.get(#rowStatus.index).datumBis" value="%{#datumBis_DE}" label="DatumBis"></s:textfield >
    </td>

    <td class="currency">
        <s:set var="tzgBetrag">
            <fmt:formatNumber type="NUMBER" maxFractionDigits="0"><s:property value="%{getBetrag()*100}"></s:property></fmt:formatNumber>
        </s:set>
        <s:textfield style="width:30px;" maxlength="3" name="model.get(#rowStatus.index).betrag" value="%{#tzgBetrag}" label="Betrag"></s:textfield >
    </td>
</tr>

</s:iterator>
<s:submit style="width:24px; height:24px;" type="image" src="../../../res/24px/floppy-disk.png" value="Speichern"></s:submit>
</ul:form>

ul-tag 来自自定义 taglib,它将客户特定的 url 参数添加到操作路径.

The ul-tag is from a custom taglib, which adds a customer specific url parameter to action path.

因此,当我显示页面时,它会显示我所有的 Teilzeitgrad 记录,每个条目都有一行.但是当我提交表单时,我的模型列表没有填充.setter setTeilzeitgrads(List teilzeitgrads) 甚至根本没有被调用.我还尝试访问数组语法中的列表:

So when I display the page it shows all my Teilzeitgrad-records with a row for each entry. But when I submit the form, the list of my models is not populated. The setter setTeilzeitgrads(List<Teilzeitgrad> teilzeitgrads) is not even called at all. I also tried to access the list in array-syntax:

但这也不起作用.

对解决此案例的任何帮助表示赞赏!提前致谢!

Any help solving this case is apreciated! Thanks in advance!

伦佐

推荐答案

好的 - 这是一个非常基本的列表索引工作示例.主要的变化是将模型的创建从 getModel() 移动到 prepare().这是因为 getModel() 会为您需要设置列表的每个值调用 - 因此每次覆盖之前的更改时您最终都会重新创建模型.

Ok - here is a very basic working example of list indexing. The main change is to move the creation of the model from getModel() to prepare(). This is because getModel() is called for every value you need to set the list - so you end up re-creating your model each time overwriting the previous change.

package com.blackbox.x.actions;

import java.util.ArrayList;
import java.util.List;

import com.blackbox.x.actions.ListDemo.ValuePair;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ListDemo extends ActionSupport implements ModelDriven<List<ValuePair>>, Preparable {


private List<ValuePair> values;

@Override
public List<ValuePair> getModel() {

    return values;

}

public String execute() {

    for (ValuePair value: values) {
        System.out.println(value.getValue1() + ":" + value.getValue2());
    }

    return SUCCESS;
}


public void  prepare() {
    values = new ArrayList<ValuePair>();
    values.add(new ValuePair("chalk","cheese"));
    values.add(new ValuePair("orange","apple"));
}


public class ValuePair {

    private String value1;
    private String value2;

    public ValuePair(String value1, String value2) {
        this.value1 = value1;
        this.value2 = value2;
    }

    public String getValue1() {
        return value1;
    }
    public void setValue1(String value1) {
        this.value1 = value1;
    }
    public String getValue2() {
        return value2;
    }
    public void setValue2(String value2) {
        this.value2 = value2;
    }
}
}

和对应的jsp

<%@ taglib prefix="s" uri="/struts-tags" %>    
<html>
<head>


</head>
<body>


<s:form action="list-demo" theme="simple">
<table>
<s:iterator value="model" status="rowStatus">
<tr>
<td><s:textfield name="model[%{#rowStatus.index}].value1" value="%{model[#rowStatus.index].value1}"/></td>
<td><s:textfield name="model[%{#rowStatus.index}].value2" value="%{model[#rowStatus.index].value2}"/></td>
</tr>
</s:iterator>
</table>
<s:submit/>
</s:form>
</body>
</html>

这篇关于Struts 2:从具有模型驱动架构的表单更新对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆