Struts 2: updating a list of objects from a form with model driven architecture

 ̄綄美尐妖づ 提交于 2019-11-28 04:44:16

问题


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:

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

}

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;
    }
}

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>

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>

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

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:

<s:textfield style="width:70px;" name="teilzeitgrads[#rowStatus.index].datumAb" value="%{#datumAb_DE}" label="DatumAb"></s:textfield >

but this did also not work.

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

Lenzo


回答1:


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;
    }
}
}

and the corresponding 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>



回答2:


Have you tried an approach like this?

<s:iterator var="teilzeitgrad" value="teilzeitgrads" status="listStatus">
   <s:set name="paramName">teilzeitgrads[${ listStatus.index }].datumAb</s:set>
   <s:textfield name="%{#paramName}" value="%{#teilzeitgrad.datumAb}"/>
</s:iterator>



回答3:


You are submitting values to model, you have to submit them to your list teilzeitgrads.

For example see http://www.dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-model-driven-action-example-1.html.

Update
How about name="teilzeitgrads[%{#rowStatus.index}].datumBis".




回答4:


Assuming you've got the configuration correct - the problem is probably due to the way you're defining the indexing. Try changing the name attribute on the textfield to use

model[%{#rowStatus.index}].datumBis

and let OGNL sort out the access methods. (I'd also use Firebug in Firefox to see what is actually being sent when you submit the form)




回答5:


Thanks to all of you getting along with this issue! Your hints were most useful. I finally got it up and running rewriting everything from the scratch. I can edit my models now using the following Action-Class:

public class TeilzeitgradEditAction implements ModelDriven<List<Teilzeitgrad>> {

List<Teilzeitgrad> teilzeitgrads;
private String tzgTypKey;
private Integer jahr;

public String execute() {
    return SUCCESS;
}

@Override
public List<Teilzeitgrad> getModel()
{
    if(teilzeitgrads == null) {
        teilzeitgrads = getTeilzeitgradListByTypAndJahr(tzgTypKey, jahr);
    }

    return teilzeitgrads;
}

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

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

    // getters and setters for local attributes
}

and this JSP-Code:

<ul:form action="auth/GroupAdmin/processEditDienstabschnittJahr">
<s:iterator var="teilzeitgrad" value="teilzeitgrads" status="listStatus">
<tr>
    <td>
        <s:date name="%{#teilzeitgrad.datumAb}" var="datumAb_DE" format="dd.MM.yyyy" />
        <s:textfield name="teilzeitgrads[%{#listStatus.index}].datumAb" value="%{#datumAb_DE}"/>
    </td>
</tr>
</s:iterator>
<s:submit style="width:24px; height:24px;" type="image" src="../../../res/24px/floppy-disk.png" value="Speichern"></s:submit>

Thanks a lot for your support!

Cheers, Lenzo



来源:https://stackoverflow.com/questions/13028113/struts-2-updating-a-list-of-objects-from-a-form-with-model-driven-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!