spring - How to show values only from specific java classes? -
i have table contains sorts of attributes set in constructors of java classes. need show attributes when want easy way nullpointers. have tried jstl if , jstl choose trick.
<c:choose> <c:when test="${empty animal.getearsize()}"> not set! </c:when> <c:otherwise> ${animal.getearsize()} </c:otherwise> </c:choose> but when try run http status 500 - exception occurred processing jsp page. animal variable in jstl loop , not animals have earsize in constructor.
what doing wrong here? there way or way jstl choose or if?
edit: i'm using method in controller gives map animal objects i'm looping through.
public africanelephant(string color, string bodycovering, string name, double weight, gender gender, int earsize) { super(color, bodycovering, name, weight, gender, maxnumberofeggs, earsize); } public parrot(string color, string bodycovering, string name, double weight, gender gender) { super(color, bodycovering, name, weight, gender, maxnumberofeggs); } these 2 sample constructors in elephant constructor there variable earsize in parrot constructor there no variable earsize.
edit: have changed choose this:
<td> <c:if test="${animal.getclass().name eq 'mypackage.mypath.africanelephant'}">${animal.earsize}</c:if> this fixes http status 500 error displays 0 in table instead of earsize set me.
my controller:
@controller("animalcontroller") public class animalcontroller {
arraylist<animal> animals = new arraylist<animal>(); @requestmapping(value = "/animaloverview", method = requestmethod.get) public string animals(map<string, object> model, @requestparam(value = "race", required = false, defaultvalue = "") string race){ if(race.isempty()){ model.put("animals", zoo.getinstance().getallanimals()); } else{ model.put("animals", zoo.getinstance().getanimalsbyrace(race)); } return "animalkingdom"; }
you cannot check way. el empty works empty string or null, not non-existent property. either add getearsize() method parrot returns null, or check if animal instanceof africanelephant , print ${animal.earsize} (not getearsize()!).
something like:
<c:if test="${animal.getclass().name eq 'package.path.africanelephant'}"> also, amit goel's answer wrong. el using getter method, , not accessing property directly (and should private). if have method e.g.: getfullsize() returns calculation's result, don't have fullsize property, can still ${animal.fullsize} , display result.
Comments
Post a Comment