java - jstl core for Each over heterogeneous collection -


i have controller returning jsp page object of type arraylist of type myclass. objects inside arraylist belong different classes, let's myclass1 , myclass2 each 1 of them extending myclass. able iterate on collection foreach tag , current element type field type in myclass when try access specific field of myclass1 got error.

javax.el.propertynotfoundexception: property 'nocontentmessage' not found on type it.sei.core.rinterface.myclass1.

here code:

class myclass {     string type;     string variable; }  class myclass1 extends myclass{     string someotherfield; }  class myclass2 extends myclass{     string nocontentmessage; }    <core:foreach items="${model.graphlist}" var="element" varstatus="index">  <script language="javascript" type="text/javascript">   var  type = '${element.type}';  switch (type)             {                 case "type1":                     {                        var variable = '${element.variable}';                      }                     break;                  case "type2":                     {                        var message = '${element.nocontentmessage}';                      }                     break;              }              </script>              </core:foreach> 

so why happening? not possible in jstl handle situation?

the jsp el never accesses fields. properties, i.e. public getters.

add public getter fields want use jsp el:

public string getnocontentmessage() {     return this.nocontentmessage; } 

edit:

also, switch block javascript code, executed @ client side, long after page has been generated. jsp engine, javascript code plain text, , branches of switch block generated. so, every object in collection, jsp el expressions evaluated.

the code should rewritten as

<script language="javascript" type="text/javascript">      var  type = '${element.type}'; // necessary?      <c:if test="${element.type == 'type1'}">          var variable = '${element.variable}';      </c:if>      <c:if test="${element.type == 'type2'}">          var message = '${element.nocontentmessage}';      </c:if> </script>   

although don't understand how makes sense redefine same js variable, again , again, every of elements in collection.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -