2

I'm trying to do that

  1. Getting a List of Objects from db in my action ( OK)
  2. Printing it on JSP (OK)
  3. This list come out as an editable table in JSP.. I want to modify then submit it back to the same action to save it on my db (FAIL. when i call my method with an <s:submit action="myaction" method="mymethod"> the list i had previously populated from db now is null.How can i solve that?

i found some topic talking about a struts2 interceptor to inject data in myaction with reflection.

public class CurrentOra {
    private int idCommessa;
    private String descrizioneCommessa; 
    private int idCliente;
    private String descrizioneCliente;
    private List<OreTimesheetGiorno> orePerCommessa;
    
    public int getIdCommessa() {
        return idCommessa;
    }
}

and

public class OreTimesheetGiorno {

    private int numeroGiorno;
    private OreTimesheet oreTimesheet;
    public int getNumeroGiorno() {
        return numeroGiorno;
    }
    public void setNumeroGiorno(int numeroGiorno) {
        this.numeroGiorno = numeroGiorno;
    }
    public OreTimesheet getOreTimesheet() {
        return oreTimesheet;
    }
    public void setOreTimesheet(OreTimesheet oreTimesheet) {
        this.oreTimesheet = oreTimesheet;
    }
    
}



    

this is my object structure , and in the JSP i print it with

<s:iterator value="listOre" >

  <tr class="giornoSettimana giornoUno">

    <td><s:property value="descrizioneCliente"/></td>
    <td><s:property value="descrizioneCommessa"/></td>

    <s:iterator value="orePerCommessa">
        <td>
            <input type="text" 
                   class="oreConsuntivazione" 
                   maxlength="2" 
                   giorno = "<s:property value="numeroGiorno" />" 
                   value="<s:property value="oreTimesheet.numeroOre" />">
        </td>

     </s:iterator>

  </tr>

</s:iterator>

basically I need to iterate to show customers and for each customer the hour worked on it. Then I should have the possibility to edit each hour and save it back to DB

Roman C
  • 49,761
  • 33
  • 66
  • 176
Federik
  • 335
  • 6
  • 16
  • Of course the `List` is `null` - your action in generated anew with each incoming request. You need to some _something_ back from your web page to tell the action what to delete. [This](http://code.google.com/p/struts2-jquery/wiki/EditGrid) ought to give you a starting point. – Boris the Spider Mar 29 '13 at 15:56
  • Pay attention to this: When you submit from a form on a page, all form elements are included in the request. This means that if you want to submit information from a form, it must be stored in a form element that resides within the form. Put your list in the form. – DwB Mar 29 '13 at 16:01
  • Ofc all the field I'm trying to submit are inside the form. I have problems only with a list. I have no problem passing an input from jsp>action. I got problem only when I print a list with an cuz the tag iterator doesn't have name property to bind it the request and send back to the action.. – Federik Mar 29 '13 at 16:29
  • Use hidden fields. And show your JSP. – Aleksandr M Mar 29 '13 at 16:30
  • 1
    post the Object structure – Andrea Ligios Mar 29 '13 at 23:42
  • You want an action to list something and then you want the same action to save it? An action "does" something, listing and saving are two different things, use two different actions. – Quaternion Mar 30 '13 at 05:31

2 Answers2

1

Every time you make a request to the Struts it creates a new instance of the action object and populate it via interceptors. One of them is params interceptor. It's responsible to iterate a parameters map and inject values of parameters to the action properties. The rule it takes the parameter names that fit to the names of action attributes and have corresponding setters in the action. So, you could solve this by firstly

@DefaultInterceptorRef(value = "defaultStack") 

to make sure all necessary interceptors on the stack, and your action references this stack implicitly or explicitly.

Then make sure you have properties that are initialized before the interceptors call them.

And finally the most fun topic is the form fields names should comply to the rules OGNL is able to retrieve from the valueStack. If your object structure is simple then fieldnames are directly mapped to the action properties. If you use collections then you'd better look at my answers that will give you better insight of the problem.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
1
  • giorno is not a valid attribute for an HTML input field;
  • CurrentOra is missing getters and setters (but I suppose you simply omitted them while posting here, btw check them carefully);

That said, to post values back, you need to specify the index of the List by using IteratorStatus.index property.

Assuming that user can only enter the number of hours, and not changes the number of the day, try as follows:

<s:iterator value="listOre" >

  <tr class="giornoSettimana giornoUno">

    <td><s:property value="descrizioneCliente"/></td>
    <td><s:property value="descrizioneCommessa"/></td>

    <s:iterator value="orePerCommessa" status="ctr" >
        <td>
            <s:hidden name="orePerCommessa[%{#ctr.index}].numeroGiorno" />

            <s:property value="numeroGiorno" />

            <s:textfield 
               name="orePerCommessa[%{#ctr.index}].oreTimesheet.numeroOre" 
               cssClass = "oreConsuntivazione"
               maxlength="2" />

        </td>

     </s:iterator>

  </tr>

</s:iterator>

This by the way will not prevent the user to hack your code altering the hidden values of orePerCommessa.numeroGiorno; if it is important to preserve them from user manipulation, put them in session once read from database, then show them in JSP (with <s:property/>), but do not post them (don't put the <s:hidden />), then while posting back the page, retrieve them from session and match them in some way (for example you could use an HashMap with them as value, and store a counter as keys in the JSP)... that way the user can just mess with the keys, not with the values.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243