Struts 2 Fields Validation with dynamic forms

828 views Asked by At

I have Struts 2 application.

I have a jsp file which looks like below

<s:form method="post" action="%{#attr.actionType}">

    <c:forEach items="${nvpList2}" var="item">
        <s:textfield label="%{#attr.item.displayName}" 
                     value="%{#attr.item.value}" 
                      name="%{#attr.item.getName()}" 
                        id="%{#attr.item.getName()}"/>
    </c:forEach>

    <s:submit type="button"   cssClass="btn btn-primary" 
               key="button.save" theme="simple" >
        <i class="icon-ok icon-white"></i>
    </s:submit>

</s:form>

As it can be seen that all the elements starting from action name to field names are created dynamically

Problem occurs when I do a validation check on fields. On validation check, the errors are shown but none of the textfields appear in the page.

How do I solve this problem.

1

There are 1 answers

0
Andrea Ligios On

Read the detailed workflow of what happens when a validation or conversion error occours;

Once you get what is happening, read How do we repopulate controls when validation fails (prepare() method and other ways);

Then, take a look at which kind of notation (and structures, in complex cases) you need to build to send Lists or Maps from JSP to Action, no matter if static (JSP or HTML) or dynamic (JSP or Javascript);

Finally, I'm not sure why you are using JSTL instead of a Struts Iterator, nor I know the data structure you are sending data to, but pay attention to the need of putting an index in the name (with IteratorStatus.index).