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

后端 未结 5 2129
一个人的身影
一个人的身影 2020-12-19 11:58

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

相关标签:
5条回答
  • 2020-12-19 12:27

    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>
    
    0 讨论(0)
  • 2020-12-19 12:33

    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

    0 讨论(0)
  • 2020-12-19 12:50

    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".

    0 讨论(0)
  • 2020-12-19 12:50

    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)

    0 讨论(0)
  • 2020-12-19 12:51

    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>
    
    0 讨论(0)
提交回复
热议问题