Search code examples
javastrutstaglibstruts-1

Display a list in a Struts 1 form


I have to write a backoffice page in order to determine which shops have to be display on a frontoffice page, in a Struts 1 application. So I have a list of shops (boutiques, in the code), contained in a form.

The form:

public class ListeBoutiquesForm extends ActionForm {

    private List<Boutique> boutiques = new ArrayList<Boutique>();

    public List<Boutique> getBoutiques() {
        return boutiques;
    }

    public void setBoutiques(List<Boutique> boutiques) {
        this.boutiques = boutiques;
    }

}

and the save action:

public ActionForward sauverBoutiques(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    ListeBoutiquesForm vlbf = (ListeBoutiquesForm) form;
    for (Boutique boutique : vlbf.getBoutiques()) {
        boutiqueService.updateBoutique(boutique);
    }
    request.getSession().removeAttribute("ListBoutiques");
    return listeSites(mapping, form, request, response);
}

The Boutique class contains an id, a name and a boolean called "selected". I want to display the name with a checkbox for each element of my boutiques list, and a submit button calling my save action, but I lack the Struts taglib knowledge to write the JSP. Can you help? I know it should involve a logic:iterate, a html:checkbox and probably a html:hidden to store the id, but I have the feeling something is missing and there is something I don't understand. How are those linked together, so to say? How will I get back a ListeBoutiquesForm with the updated values in my save action ?

Edit:

Following Susie's advices, I came up with the following JSP:

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %> 

<html:errors/>
<html:form action="/admin/boutiqueviparis">
    <input type="hidden" name="reqCode" value="sauverBoutiques" />

    <table class="tableStatic noHead">
        <logic:notEmpty name ="viparisListeBoutiquesForm" property="boutiques">
            <logic:iterate id="boutique" name ="viparisListeBoutiquesForm" property="boutiques">
                <tr>
                    <td>
                        <html:hidden name ="boutique" property="id" />
                        <bean:write name="boutique" property="nom" />
                    </td>
                    <td>
                        <html:checkbox name="boutique" property="selected" value="true" />
                    </td>
                </tr>
            </logic:iterate>
            <html:submit styleClass="boutonrouge" value="Valider" onclick="this.form.reqCode.value='sauverBoutiques';return window.confirm('Modification validée');"/>
        </logic:notEmpty>
    </table>
</html:form>

This displays the list of the shops and the checkboxes, which are checked accordingly to the value of the "selected" field. But my save action doesn't work. Here my save action, if anyone has an idea:

public ActionForward sauverBoutiques(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    ViparisListeBoutiquesForm vlbf = (ViparisListeBoutiquesForm) form;
    for (Boutique boutique : vlbf.getBoutiques()) {
        log.debug("########## BOUTIQUE nom : "+boutique.getNom()+" selected :"+boutique.isSelected());
        boutiqueService.updateBoutique(boutique);
    }
    request.getSession().removeAttribute("viparisListBoutiques");
    return listeSites(mapping, form, request, response);
}

I can see update statements in my Hibernate logs, but nothing is actually saved. I logged the value of my selected field, and actually, it's the initial value, before I changed it in the displayed form. Kind of puzzling...

Edit:

Actually, the <html:checkbox name="boutique" property="selected" value="id" /> appears in the final HTML as <input type="checkbox" name="selected" value="id">, with "id" instead of the id value. But I don't know how to solve this...


Solution

  • The following is something that you should be doing. struts-config.xml file the configuration file that glues everything together.

    parameter ---> which method to call
    name---> name of the action form
    path---> URL
    type---> action class that is invoked by the above url. 
    forward---->where to forward once it's done.
    

    As you can see I set the list in myMethod() and access in the jsp below.

    JSP page:

       <form name="myForm" action="myAction.do" method="post">
            <logic:notEmpty name="myForm" property="myList">
                        <logic:iterate id="boutique" name="myForm" property="myList"            type="com.Boutique">                
                            <tr>
                                <td><bean:write name="boutique" property="id" /> </td>
                                <td><bean:write name="boutique" property="name" /></td>
                                <td><bean:write name="boutique" property="selected" /></td>
                            </tr>
                        </logic:iterate>
            </logic:notEmpty> 
        </form>
    

    In my action class method:

    public ActionForward myMethod(ActionMapping actionMapping,
                    ActionForm myForm, HttpServletRequest httpServletRequest,
                    HttpServletResponse httpServletResponse) throws Exception {
                    List<Boutique> myList = new ArrrayList<Boutique>();
                    form.setMyList(myList);
                                return mapping.findForward("success");
    
    }
    

    struts-config.xml

    <struts-config>
        <form-beans>
            <form-bean name="myForm" type="com.usps.nom.tops.web.struts.form.transportation.ITransportationInquiryDynaForm">
            </form-bean>
        </form-beans>
        <action-mappings>
            <action path="/myAction" type="com.MyAction"
                    name="myForm" scope="session" validate="false"
                    parameter="myMethod">               
                    <forward name="success" path="tile.view"></forward>     
            </action>
        </action-mappings>
    </struts-config>