Gary
Gary

Reputation: 747

Java struts get property from index

I have been tasked with improving the form validation on a page.

The site uses .jsp/bean/struts etc. Currently there is a loop at the top of the page to print errors to the screen:

<nested:iterate id="currentError" property="wizard.errors" type="java.lang.String" indexId="counter">
    <tr>
        <td valign="top" class="wizardErrorText" width="2%">
            &gt;
        </td>
        <td class="wizardErrorText">
            <span errorId="">
                <bean:write name="currentError"/>  
            </span>
        </td>
    </tr>
</nested:iterate>

Now when validation fails I have changed the existing method slightly to indicate the id of the field that has failed validation as well as the error message. I have then set up a similar loop to print the id's of the fields that have failed validation to an area which is read by JavaScript and highlights the fields accordingly.

Rather than having another loop I would like to use the indexId to look up the value in the array and include it in the errorId.

Could anybody advise how to do this please?

I have tried jsp.getProperty and a few other methods but with no luck.

Thanks Gary

Upvotes: 2

Views: 2425

Answers (1)

Joseph Erickson
Joseph Erickson

Reputation: 2294

You should be able to something like this:

<bean:write name="wizard" property="erroredInputs[counter]" />

Or, full example:

<nested:iterate id="currentError" property="wizard.errors" type="java.lang.String" indexId="counter">
    <tr>
        <td valign="top" class="wizardErrorText" width="2%">
            &gt;
        </td>
        <td class="wizardErrorText">
            <span errorId="<bean:write name="wizard" property="erroredInputs[counter]" />">
                <bean:write name="currentError"/>  
            </span>
        </td>
    </tr>
</nested:iterate>

More information can be found here: https://struts.apache.org/1.x/struts-taglib/indexedprops.html

Upvotes: 1

Related Questions