JSP question about accessing keys in an object?

93 Views Asked by At

Let's say you have something like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<table>
     <c:forEach items="${products}" var="product">
         <tr>
             <td>${product.name}</td>
             <td>${product.description}</td>
          </tr>
      </c:forEach>
</table>

How can I see what other keys (like the keys: name or description) that this "product" object has? I'm mostly a JavaScript engineer, so JSP is still new to me.

Let's say for instance I wanted the price in a new <td>, but product.price doesn't work and I can't figure out the right key to access it's true name.

Tried searching for an answer online but not seeing it.

2

There are 2 best solutions below

0
Roman C On

Something like this is a fragment of JSP code using JSTL tags. JSTL uses havily EL expressions in their tags. It is <c:forEach> tag from your code. Even if a whole JSP page is defined, you might not know the attributes used there. If you try to access such pages in the browser then it fails with empty content or returns 404 error code. The last case is identified if your JSP pages under WEB-INF folder.

For what reason you can't still access the JSP pages in the view directory?

The fact is that WEB-INF is a protected directory on the server; access to files from the outside is impossible. The application can store its files in this directory to ensure that they are not accessible to users. This primarily applies to configuration files and other internal files. JSP files in some MVC frameworks began to be placed there in order to limit direct access to the View, bypassing the controller. The same thing applies to servlets that implement the MVC pattern using a dispatcher. Since these JSPs cannot run without initialization on the controller, directly accessing such pages will throw exceptions.

Initializing JSP pages performed by a controller where all attributes are prepared and put into a JSP scope. Once it prepared for showing, the controller uses a dispatcher to forward its request to the JSP page. All attributes are transfered to the new request and can be found by EL expressions in JSTL tags. So, you should look into controller code to find objects it passing to the JSP as an attribute. Generally a controller doesn't care about object structure because it saved them as Object. EL expressions use an introspection mechanizms to look into the object and figure out its properties.

2
BalusC On

How can I see what other keys (like the keys: name or description) that this "product" object has?

A smart IDE can see and autocomplete that for you while hovering/completing the ${product.***}. If yours doesn't then it's most probably not been placed in the EL scope via a standard way or via a standard MVC framework (e.g. JSF, Faces, Spring MVC, etc) but rather in a homegrown way which the IDE doesn't recognize.

what would be your process for identifying which is the right class? I know only the basics of classes. generally what they look like and to look for them in a .java file. i dont know how to identify which one leads to the products collection though.

In EL, you can debug/print the FQN of the concrete Class of the object behind ${product} as follows:

${product['class']}

Temporarily put it in the same JSP file and it'll print something like com.example.model.Product which you can then use to identify the associated source code.

See also: