facebook

ModelAttribute is null

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #318633 Reply

    Hi, I have scaffolded some tables from a mysql db, so I got every crud method generated automatically.
    Now I’m creating a search function, where the user inserts something in a form and if he clicks on “search” the system does a query with the data he entered.
    I’ve managed to make everything work for the entity SoggettiWeb, then I applied everything to the entity SoggettiindirizziWeb and it doesn’t work.
    I debugged the code to understand that the modelattribute that the search method gets has all the fields set to null.

    Now here’s some code:
    in my SoggettiindirizziController
    The method that loads the page. The page shows a table with all the entities loaded from the db (with the method convertToWeb(soggettiindirizziService.loadSoggettiindirizzis()))) and a form in which the user can enter his search criteria (backed with the object new SoggettiindirizziWeb()):

    
    @RequestMapping("/indexSoggettiindirizzi")
        public ModelAndView listSoggettiindirizzis()
        {
            ModelAndView mav = new ModelAndView();
            
            mav.addObject("soggettiindirizzis", convertToWeb(soggettiindirizziService.loadSoggettiindirizzis()));
            mav.addObject("soggettiindirizzi", new SoggettiindirizziWeb());
            mav.setViewName("soggettiindirizzi/newListSoggettiindirizzi.jsp");
            
            return mav;
        }
    
    

    The method called when the user presses the search button.

    
    @RequestMapping("/searchSoggettiindirizzi")
        public ModelAndView searchSoggettiindirizzi(@ModelAttribute SoggettiindirizziWeb soggettiindirizzi)
        {
            System.out
                    .println("SoggettiindirizziController.searchSoggettiindirizzi(): \n" + soggettiindirizzi);
            ModelAndView mav = new ModelAndView();
            
            mav.addObject("soggettiindirizzis", convertToWeb(soggettiindirizziDAO.searchSoggettiindirizzi(convertToBean(soggettiindirizzi))));
            mav.addObject("soggettiindirizzi", soggettiindirizzi);
            mav.setViewName("soggettiindirizzi/newListSoggettiindirizzi.jsp");
            
            return mav;
        }
    

    My form in the page newListSoggettiindirizzi.jsp
    There are some comments because I’m struggling with this since two days, and those are the various trials and errors.
    I think it’s not necessary to illustrate every single thing the fields do, I just need to know why they are not mapped to the object I pass to the page.

    
    <form:form id="soggettiindirizziform"
                                                name="soggettiindirizziform"
                                                action="${pageContext.request.contextPath}/searchSoggettiindirizzi"
                                                method="POST" commandName="soggettiindirizzi">
                                                <table cellpadding="0" cellspacing="0" id="viewTable">
                                                    <tbody>
                                                        <!--<form:hidden id="soggettiindirizzi_indirizzoId"
                                                            path="indirizzoId" />
                                                        <form:hidden id="soggettiindirizzi_soggettiCodice"
                                                            path="soggettiCodice" />-->
    
                                                        <tr>
                                                            <td class="label" valign="top">
                                                                <fmt:message key="soggetti.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:select id="soggettiindirizzi_soggettiDescrizione"
                                                                    path="soggettiDescrizione" cssStyle="width:300px;">
                                                                    <option></option>
                                                                    <jsp:setProperty name="SoggettiindirizziWebId"
                                                                        property="soggettiDescrizione"
                                                                        value='${soggettiindirizzi.soggettiDescrizione}' />
                                                                    <jsp:setProperty name="tempArrayList" property="array"
                                                                        value='${soggettiList}' />
                                                                    <%     
                                        
                                        delidete.webdomain.SoggettiWeb c;
                                        for(Object current: tempArrayList.getArray())
                                        {
                                            c = (delidete.webdomain.SoggettiWeb)current;
                                            if(SoggettiindirizziWebId.getSoggettiDescrizione().equals(c.getSoggettiRagSocCognNome()))
                                            {    
                                                out.println("<option value=\"" + c.getSoggettiRagSocCognNome() + "\" selected=\"selected\">" + c.getSoggettiRagSocCognNome() + "</option>");
                                            }
                                            else
                                            {
                                                out.println("<option value=\"" + c.getSoggettiRagSocCognNome() + "\">" + c.getSoggettiRagSocCognNome() + "</option>");
                                            }
                                        }
                                    %>
                                                                </form:select>
                                                            </td>
                                                            <td class="label" valign="top">
                                                                <fmt:message
                                                                    key="soggettiindirizzi.indirizzodescrizione.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:input id="soggettiindirizzi_indirizzoDescrizione"
                                                                    path="indirizzoDescrizione" cssStyle="width:300px;" />
                                                                <script type="text/javascript">
        Spring
             .addDecoration(new Spring.ElementDecoration(
                   {
                      elementId : "soggettiindirizzi_indirizzoDescrizione",
                      widgetType : "dijit.form.ValidationTextBox",
                      widgetAttrs :
                      {
                          promptMessage : "<fmt:message key="soggettiindirizzi.indirizzodescrizione.help"/>"
                      }
                   }));
    </script>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td class="label" valign="top">
                                                                <fmt:message key="soggettiindirizzi.indirizzocap.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:input id="soggettiindirizzi_indirizzoCap"
                                                                    path="indirizzoCap" cssStyle="width:300px;" />
                                                                <script type="text/javascript">
        Spring
             .addDecoration(new Spring.ElementDecoration(
                   {
                      elementId : "soggettiindirizzi_indirizzoCap",
                      widgetType : "dijit.form.ValidationTextBox",
                      widgetAttrs :
                      {
                          promptMessage : "<fmt:message key="soggettiindirizzi.indirizzocap.help"/>"
                      }
                   }));
    </script>
                                                            </td>
    
                                                            <td class="label" valign="top">
                                                                <fmt:message key="soggettiindirizzi.indirizzodal.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:input id="soggettiindirizzi_indirizzoDal"
                                                                    path="indirizzoDal" cssStyle="width:275px;" />
                                                                <img title="Open Calendar" class="tcalIcon"
                                                                    onclick="A_TCALS['indirizzoDalId'].f_toggle()"
                                                                    id="tcalico_indirizzoDalId" src="javascripts/calendar/img/cal.gif" />
                                                                <br />
                                                                <!--                <script language="JavaScript">
        new tcal(
       {
          'formname' : 'soggettiindirizziform',
          'controlname' : 'indirizzoDal',
          'id' : 'indirizzoDalId'
       });
    </script>-->
    <!--                                                             <script type="text/javascript">
        Spring.addDecoration(new Spring.ElementDecoration(
       {
          elementId : "soggettiindirizzi_indirizzoDal",
          widgetType : "dijit.form.ValidationTextBox",
          widgetAttrs :
          {
              promptMessage : "<fmt:message key="navigation.dateTime.title"/>"
          }
       }));
    </script> -->
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td class="label" valign="top">
                                                                <fmt:message key="soggettiindirizzi.indirizzoal.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:input id="soggettiindirizzi_indirizzoAl"
                                                                    path="indirizzoAl" cssStyle="width:275px;" />
                                                                <img title="Open Calendar" class="tcalIcon"
                                                                    onclick="A_TCALS['indirizzoAlId'].f_toggle()"
                                                                    id="tcalico_indirizzoAlId" src="javascripts/calendar/img/cal.gif" />
                                                                <!-- <script language="JavaScript">
        new tcal(
       {
          'formname' : 'soggettiindirizziform',
          'controlname' : 'indirizzoAl'
       });
    </script> -->
    <!--                                                             <script type="text/javascript">
        Spring.addDecoration(new Spring.ElementDecoration(
       {
          elementId : "soggettiindirizzi_indirizzoAl",
          widgetType : "dijit.form.ValidationTextBox",
          widgetAttrs :
          {
              promptMessage : "<fmt:message key="navigation.dateTime.title"/>"
          }
       }));
    </script> -->
                                                            </td>
    
                                                            <td class="label" valign="top">
                                                                <fmt:message key="comuni.comunidescrizione.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:select id="soggettiindirizzi_comuniDescrizione"
                                                                    path="comuniDescrizione" cssStyle="width:300px;">
                                                                    <option></option>
                                                                    <jsp:setProperty name="SoggettiindirizziWebId"
                                                                        property="comuni" value='${soggettiindirizzi.comuni}' />
                                                                    <jsp:setProperty name="SoggettiindirizziWebId"
                                                                        property="comuniDescrizione"
                                                                        value='${soggettiindirizzi.comuniDescrizione}' />
                                                                    <jsp:setProperty name="tempArrayList" property="array"
                                                                        value='${comuniList}' />
                                                                    <%     
                                        delidete.domain.Comuni c;
                                        for(Object current: tempArrayList.getArray())
                                        {
                                            c = (delidete.domain.Comuni)current;
                                            if(!SoggettiindirizziWebId.getComuniDescrizione().equals("") && SoggettiindirizziWebId.getComuniDescrizione().substring(0, SoggettiindirizziWebId.getComuniDescrizione().indexOf("(") - 1).equals(c.getComuniDescrizione()))
                                            {
                                                out.println("<option value=\"" + c.getComuniDescrizione() + " (" + c.getProvince().getProvinceDescrizione() + ")\" selected=\"selected\">" + c.getComuniDescrizione() + " (" + c.getProvince().getProvinceDescrizione() + ")"+ "</option>");
                                            }
                                            else
                                            {
                                                out.println("<option value=\"" + c.getComuniDescrizione() + " (" + c.getProvince().getProvinceDescrizione() + ")\" >" + c.getComuniDescrizione() + " (" + c.getProvince().getProvinceDescrizione() + ")"+ "</option>");
                                            }
                                        }
                                    %>
                <!--                                                     <script type="text/javascript">
        Spring.addDecoration(new Spring.ElementDecoration(
       {
          elementId : "soggettiindirizzi_comuniDescrizione",
          widgetType : "dijit.form.ComboBox",
          widgetAttrs :
          {
              promptMessage : "<fmt:message key="comuni.comunidescrizione.help"/>"
          }
       }));
    </script> -->
                                                                </form:select>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td class="label" valign="top">
                                                                <fmt:message
                                                                    key="datigenerali.datigeneralidenominazioneente.title" />
                                                                :
                                                            </td>
                                                            <td>
                                                                <form:select
                                                                    id="soggettiindirizzi_datigeneraliDescrizione"
                                                                    path="datigeneraliDescrizione" cssStyle="width:300px;">
                                                                    <option></option>
                                                                    <jsp:setProperty name="SoggettiindirizziWebId"
                                                                        property="datigenerali"
                                                                        value='${soggettiindirizzi.datigenerali}' />
                                                                    <jsp:setProperty name="SoggettiindirizziWebId"
                                                                        property="datigeneraliDescrizione"
                                                                        value='${soggettiindirizzi.datigeneraliDescrizione}' />
                                                                    <jsp:setProperty name="tempSet" property="set"
                                                                        value='${datiGeneraliList}' />
                                                                    <%     
                                        delidete.domain.Datigenerali c;
                                        for(Object current: tempSet.getSet())
                                        {
                                            c = (delidete.domain.Datigenerali)current;
                                            if(SoggettiindirizziWebId.getDatigeneraliDescrizione().equals(c.getDatiGeneraliDenominazioneEnte()))
                                            {
                                                out.println("<option value=\"" + c.getDatiGeneraliDenominazioneEnte() + "\" selected=\"selected\">" + c.getDatiGeneraliDenominazioneEnte() + "</option>");
                                            }
                                            else
                                            {
                                                out.println("<option value=\"" + c.getDatiGeneraliDenominazioneEnte() + "\">" + c.getDatiGeneraliDenominazioneEnte() + "</option>");
                                            }
                                        }
                                    %>
                                                                </form:select>
                                                            </td>
                                                            <td class="label" valign="top">
                                                            </td>
                                                            <td nowrap="nowrap" class="tabletd">
                                                        </tr>
                                                    </tbody>
                                                </table>
                                                <!-- <span class="inputbutton"><input class="refreshbutton" id="searchSoggettiindirizzi" type="submit" value="<fmt:message key="navigation.searchSoggettiindirizzi"/>"/></span> -->
                                                <span class="inputbutton"><input class="refreshbutton"
                                                        id="searchSoggettiindirizzi"
                                                        onClick="javascript:refreshTabWithPost('tabListSoggettiindirizzi', 'tabListSoggettiindirizzi', '${pageContext.request.contextPath}/searchSoggettiindirizzi', '<fmt:message key="navigation.view"/> <fmt:message key="soggettiindirizzi.title"/>')"
                                                        value="<fmt:message key="navigation.searchSoggettiindirizzi"/>" />
                                                </span>
                                            </form:form>
    

    The bean SoggettiindirizziWeb:

    
    package delidete.webdomain;
    
    
    import java.io.Serializable;
    
    import delidete.domain.Comuni;
    import delidete.domain.Datigenerali;
    
    
    public class SoggettiindirizziWeb implements Serializable
    {
        private static final long serialVersionUID = 1L;
        
        Integer indirizzoId;
        Integer soggettiCodice;
        SoggettiWeb soggetti;
        String soggettiDescrizione;
        String indirizzoDescrizione;
        String indirizzoCap;
        String indirizzoDal;
        String indirizzoAl;
        String datigeneraliDescrizione;
        Datigenerali datigenerali;
        Comuni comuni;
        String comuniDescrizione;
        
        public void setIndirizzoId(Integer indirizzoId)
        {
            this.indirizzoId = indirizzoId;
        }
        
        /**
         */
        public Integer getIndirizzoId()
        {
            return this.indirizzoId;
        }
        
        /**
         */
        public void setSoggettiCodice(Integer soggettiCodice)
        {
            this.soggettiCodice = soggettiCodice;
        }
        
        /**
         */
        public Integer getSoggettiCodice()
        {
            return this.soggettiCodice;
        }
        
        /**
         */
        public void setIndirizzoDescrizione(String indirizzoDescrizione)
        {
            this.indirizzoDescrizione = indirizzoDescrizione;
        }
        
        /**
         */
        public String getIndirizzoDescrizione()
        {
            return this.indirizzoDescrizione;
        }
        
        public String getIndirizzoCap()
        {
            return indirizzoCap;
        }
        
        public void setIndirizzoCap(String indirizzoCap)
        {
            this.indirizzoCap = indirizzoCap;
        }
        
        /**
         */
        public void setIndirizzoDal(String indirizzoDal)
        {
            this.indirizzoDal = indirizzoDal;
        }
        
        /**
         */
        public String getIndirizzoDal()
        {
            return this.indirizzoDal;
        }
        
        /**
         */
        public void setIndirizzoAl(String indirizzoAl)
        {
            this.indirizzoAl = indirizzoAl;
        }
        
        /**
         */
        public String getIndirizzoAl()
        {
            return this.indirizzoAl;
        }
        
        public String getDatigeneraliDescrizione()
        {
            return datigeneraliDescrizione;
        }
        
        public void setDatigeneraliDescrizione(String datigeneraliDescrizione)
        {
            this.datigeneraliDescrizione = datigeneraliDescrizione;
        }
        
        public Datigenerali getDatigenerali()
        {
            return datigenerali;
        }
        
        public void setDatigenerali(Datigenerali datigenerali)
        {
            this.datigenerali = datigenerali;
        }
        
        /**
         */
        public void setComuni(Comuni comuni)
        {
            this.comuni = comuni;
        }
        
        /**
         */
        public Comuni getComuni()
        {
            return comuni;
        }
        
        public SoggettiWeb getSoggetti()
        {
            return soggetti;
        }
        
        public void setSoggetti(SoggettiWeb soggetti)
        {
            this.soggetti = soggetti;
        }
        
        public String getSoggettiDescrizione()
        {
            return soggettiDescrizione;
        }
        
        public void setSoggettiDescrizione(String soggettiDescrizione)
        {
            this.soggettiDescrizione = soggettiDescrizione;
        }
        
        public String getComuniDescrizione()
        {
            return comuniDescrizione;
        }
        
        public void setComuniDescrizione(String comuniDescrizione)
        {
            this.comuniDescrizione = comuniDescrizione;
        }
        
        /**
         */
        public SoggettiindirizziWeb()
        {
        }
        
        /**
         * Copies the contents of the specified bean into this bean.
         * 
         */
        public void copy(SoggettiindirizziWeb that)
        {
            setIndirizzoId(that.getIndirizzoId());
            setSoggettiCodice(that.getSoggettiCodice());
            setSoggetti(that.getSoggetti());
            setSoggettiDescrizione(that.getSoggettiDescrizione());
            setIndirizzoDescrizione(that.getIndirizzoDescrizione());
            setIndirizzoDal(that.getIndirizzoDal());
            setIndirizzoAl(that.getIndirizzoAl());
            setDatigenerali(that.getDatigenerali());
            setDatigeneraliDescrizione(that.getDatigeneraliDescrizione());
            setComuni(that.getComuni());
            setComuniDescrizione(that.getComuniDescrizione());
        }
        
        /**
         * Returns a textual representation of a bean.
         * 
         */
        public String toString()
        {
            
            StringBuilder buffer = new StringBuilder();
            
            buffer.append("indirizzoId=[").append(indirizzoId).append("] \n");
            buffer.append("soggettiCodice=[").append(soggettiCodice).append("] \n");
            buffer.append("soggetti=[").append(soggetti).append("] \n");
            buffer.append("soggettiDescrizione=[").append(soggettiDescrizione).append("] \n");
            buffer.append("indirizzoDescrizione=[").append(indirizzoDescrizione)
                    .append("] \n");
            buffer.append("indirizzoDal=[").append(indirizzoDal).append("] \n");
            buffer.append("indirizzoAl=[").append(indirizzoAl).append("] \n");
            buffer.append("datiGenerali=[").append(datigenerali).append("] \n");
            buffer.append("datiGeneraliDescrizione=[").append(datigeneraliDescrizione).append("] \n");
            buffer.append("comuni=[").append(comuni).append("] \n");
            buffer.append("comuniDescrizione=[").append(comuniDescrizione).append("] \n");
            return buffer.toString();
        }
        
        /**
         */
        @Override
        public int hashCode()
        {
            final int prime = 31;
            int result = 1;
            result = (int)(prime * result + ((indirizzoId == null) ? 0 : indirizzoId
                    .hashCode()));
            result = (int)(prime * result + ((soggettiCodice == null) ? 0
                    : soggettiCodice.hashCode()));
            return result;
        }
        
        /**
         */
        public boolean equals(Object obj)
        {
            if(obj == this)
                return true;
            if(!(obj instanceof SoggettiindirizziWeb))
                return false;
            SoggettiindirizziWeb equalCheck = (SoggettiindirizziWeb)obj;
            if((indirizzoId == null && equalCheck.indirizzoId != null)
                    || (indirizzoId != null && equalCheck.indirizzoId == null))
                return false;
            if(indirizzoId != null && !indirizzoId.equals(equalCheck.indirizzoId))
                return false;
            if((soggettiCodice == null && equalCheck.soggettiCodice != null)
                    || (soggettiCodice != null && equalCheck.soggettiCodice == null))
                return false;
            if(soggettiCodice != null
                    && !soggettiCodice.equals(equalCheck.soggettiCodice))
                return false;
            return true;
        }
    }
    
    

    The output I get from the console is (I’ve put in the toString() method every field of the Soggettiindirizzi class):

    
    SoggettiindirizziController.searchSoggettiindirizzi(): 
    indirizzoId=[null] 
    soggettiCodice=[null] 
    soggetti=[null] 
    soggettiDescrizione=[null] 
    indirizzoDescrizione=[null] 
    indirizzoDal=[null] 
    indirizzoAl=[null] 
    datiGenerali=[null] 
    datiGeneraliDescrizione=[null] 
    comuni=[null] 
    comuniDescrizione=[null] 
    

    The fields soggetti, datiGenerali and comuni are objects, the other are strings. I don’t know if this info can be useful.

    I’m new to spring, so any stupid error you could think of could be the cause for this non-functioning method.
    I’m sorry for the indentation of the jsp page, but ctrl+shift+f outputs this…

    I need to understand something:
    if in the method listSoggettiindirizzis() I add to the view the line mav.addObject(“soggettiindirizzi”, new SoggettiindirizziWeb()); in the jsp page I have access to the object “soggettiindirizzi” of class SoggettiindirizziWeb. I have set the property commandName=”soggettiindirizzi” in the form tags, so I can use it (and in the edit page it works…)
    I really can’t see what I’ve done wrong..
    Thanks in advance for your help!
    Andrea

    #318661 Reply

    Can’t find the Edit button, otherwise I would have edited my first post.
    Found the error:
    in my refresh function, where I took the form to pass it to the search form I was using a static id for the form I was using as test, so even though I was passing it dynamically it was still the test one.

    The error is solved, mod can close this topic.
    🙂

    #318671 Reply

    jkennedy
    Member

    Thank you for updating us on your resolution, we appreciate it.

    Thanks,
    Jack

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: ModelAttribute is null

You must be logged in to post in the forum log in