I want to use JSP page to return dynamic xml.The problem I’m facing is that I’ve model where some values are null and when they are null I don’t wont to display them. So I can do following:
if(ampApInfo.getColourcd().equals(null)){ %>
<ColourCd><%= ampApInfo.getColourcd() %></ColourCd>
<%} else if(ampApInfo.getSzWeightColourcd().equals(null)){ %>
<SzWeightColourcd><%= ampApInfo.getSzWeightColourcd() %></SzWeightColourcd>
<%}%> //and so forth
But this makes my code ugly and not practical. Is there a way to avoid that??
Can you please provide me with an example how to do it or point to one.
Thanks a lot.
The ugliness is caused by using old fashioned scriptlets instead of JSTL/EL. With JSTL, it would look more self-documenting (as it uses XML-like markup). With EL you can use the
${}notation to access bean properties.Something like this:
(note that I inversed the condition, you were printing the elements when the value is
null, which contradicts your own functional requirement)See also: