0

I have some trouble when trying updating some data, I'm facing some exceptions...

I read that Struts 2 nesting iterators and applied modifications, but it does not work...

Here's Java classes :

 public class Epreuveevb implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int idEpreuve;
    private String nom;
    private int coef;
    private int note;
    private int idExamen;

/* all setters and getters */

Then the second :

public class Examenevb implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int idJury;
    private String nom;
    private String prenom;
    private List<Epreuveevb> listeEpreuves = null;

/* all setters and getters */

Finally, this is the highest class (with servlets) :

    public class Saisie extends ActionSupport {

        static final Logger log = LogManager.getLogger(Saisie.class);

        private int idJury;
        private Jury leJury;
        private List<Examenevb> lesExamens;

[...]

        public List<Examenevb> getLesExamens() {
            return lesExamens;
        }
        public void setLesExamens(List<Examenevb> lesExamens) {
            this.lesExamens = lesExamens;
        }

And now JSP :

            <s:form theme="simple" action="relevernote">
        <table class="notation">
            <tbody>
                <s:iterator value="lesExamens" status="examstat">
                <tr>
                    <td class="default"><span class="gras">${nom}</span>, ${prenom}</td>
                    <s:iterator value="listeEpreuves" status="eprstat">
                    <td>
                        ${nom}<br/>
                        <s:textfield name="lesExamens[%{#examstat.index}].listeEpreuves[%{#eprstat.index}].note"/>
                        <p>&nbsp;</p>
                    </td>
                    </s:iterator>
                </tr>
                </s:iterator>
            </tbody>
        </table>
        <p>&nbsp;</p>
        <p class="center"><s:submit value="Valider"/></p>
        </s:form>

All is fine, all marks are correctly in text fields. For eg, this is final HTML page:

        <td class="default"><span class="gras">EL MORABIT</span>, MOHAMED</td>

                <td>
                    Maths<br/>
                    <input type="text" name="lesExamens[0].listeEpreuves[0].note" value="0" id="relevernote_lesExamens_0__listeEpreuves_0__note"/>
                    <p>&nbsp;</p>
                </td>

But when submitting, I see in log :

    2017-05-16 17:29:44,910 ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor [notifyDeveloperParameterException] Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'lesExamens[0].listeEpreuves[0].note' on 'class evb.pg.notation.Saisie: Error setting expression 'lesExamens[0].listeEpreuves[0].note' with value ['0', ]
2017-05-16 17:29:44,914 ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor [notifyDeveloperParameterException] Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'lesExamens[0].listeEpreuves[1].note' on 'class evb.pg.notation.Saisie: Error setting expression 'lesExamens[0].listeEpreuves[1].note' with value ['0', ]

What's going wrong ? Any idea ?

I checked several times setters/getters, all is fine...

Community
  • 1
  • 1
  • I changed Examenevb class, I removed "private List listeEpreuves = null;" and replaced it by "private List listeEpreuves;". Unfortunately, the bug remains... – Djibril Jorandhi May 16 '17 at 20:20
  • Make sure you have correct getters and setters – Roman C May 17 '17 at 08:09
  • Hello Roman, I just checked all setters & getters, all is fine. However, I'm reading a book dealing with Struts2. I may have found a solution : using Preparable interface. Hope I will fix this... – Djibril Jorandhi May 17 '17 at 08:19
  • `Preparable` is used by another interceptor. It has also another meaning. – Roman C May 17 '17 at 10:08
  • Please tell me : what are the rules of Preparable ? I have almost fixed my problem, using both ModelDriven and Preparable. When stabilized, I will report it. – Djibril Jorandhi May 17 '17 at 11:13
  • Model driven is another issue. It's too broad to tell you all about Struts. – Roman C May 17 '17 at 11:41

1 Answers1

0

I just fixed the problem, all is now OK. But I would like being advised as better solutions may exist. Please let me know if so.

Let me show you which modifications I brought.

First, I forgot empty constructors for Epreuveevb and Examenevb classes.

Secondly, I added Preparable and ModelDriven interfaces to ActionSupport class :

public class Saisie extends ActionSupport implements Preparable,    
    ModelDriven<List<Examenevb>> {

        static final Logger log = LogManager.getLogger(Saisie.class);

        private int idJury;
        private Jury leJury;
        private List<Examenevb> lesExamens;
        private int idPassageGrade;

Those interfaces force me two additionnal methods :

@Override
    public List<Examenevb> getModel() {
        // TODO Auto-generated method stub
        return lesExamens;
    }

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

Finally, JSP became a few heavier :

    <s:form theme="simple" action="relevernote">
    <table class="notation">
        <tbody>
            <s:iterator value="lesExamens" status="idexam">
            <tr>
                <td class="default">
                    <span class="gras">${nom}</span>, ${prenom}
                    <s:hidden name="lesExamens[%{#idexam.index}].idJury"/>
                    <s:hidden name="lesExamens[%{#idexam.index}].idAdherent"/>"
                    <s:hidden name="lesExamens[%{#idexam.index}].nom"/>
                    <s:hidden name="lesExamens[%{#idexam.index}].prenom"/>

                </td>
                <s:iterator value="listeEpreuves" status="idepr">
                <td>
                    ${nom}<br/>
                    <s:hidden name="lesExamens[%{#idexam.index}].listeEpreuves[%{#idepr.index}].idEpreuve"/>
                    <s:hidden name="lesExamens[%{#idexam.index}].listeEpreuves[%{#idepr.index}].idExamen"/>
                    <s:hidden name="lesExamens[%{#idexam.index}].listeEpreuves[%{#idepr.index}].coef"/>
                    <s:hidden name="lesExamens[%{#idexam.index}].listeEpreuves[%{#idepr.index}].nom"/>
                    <s:textfield name="lesExamens[%{#idexam.index}].listeEpreuves[%{#idepr.index}].note"/>
                    <p>&nbsp;</p>
                </td>
                </s:iterator>
            </tr>
            </s:iterator>
        </tbody>
    </table>
    <p class="center"><s:submit value="Valider"/></p>
    </s:form>

To ensure all attributes are set, I added several tags... maybe there is a better way. If so, please let me know.