Raj
Raj

Reputation: 2398

How to retrieve Entity property in JSP(Passed from Servlet)

I have created a list in Servlet(List of entities). I am trying to iterate through the list and fetch the properties in JSP

I am able to iterate through the list in JSP, but not sure how to retrieve the Properties of the Entity. What am I missing here?

Servlet that inserts data into Datastore,

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity adminUser = new Entity("AdminUser");
adminUser.setProperty("mail_id", "[email protected]");
ds.put(adminUser);

Servlet that creates the List,

public void doGet(..) {
   ...
   PreparedQuery pq = ds.prepare(q);
   List<Entity> adminList = pq.asList(FetchOptions.Builder.withLimit(10));
   req.setAttribute("adminList", adminList);
   resp.setContentType("text/html");
   RequestDispatcher jsp = req.getRequestDispatcher("/WEB-INF/DisplayAdminPage.jsp");
   jsp.forward(req, resp);
   ...
}   

JSP that iterates through the list

<c:forEach items="${adminList}" var="adminEntity">
    <tr>
    //This displays the entire entity, but not sure how to fetch
    //the individual property??
    <td>${fn:escapeXml(adminEntity)}</td>   
    </tr>       
</c:forEach>

I also tried something like this to fetch the property ; ${fn:escapeXml(adminEntity.mail_id)}, but is not working

PS : I have followed the suggestion given in this post

Upvotes: 1

Views: 949

Answers (1)

Romin
Romin

Reputation: 8816

You cannot access the property directly like that, since the EL Expression will try to look for a getXXX() method on the Entity class, which it does not have since you have dynamically created the Entity.

Having said that, there is a solution to what you want to do. If you look at the documentation for the PropertyContainer of the Datastore class, you will find that it has a public method named getProperties() that returns a java.util.Map<java.lang.String,java.lang.Object> of all the properties of the Container.

What this means is that your entity will have a property named properties and you can use that to further reference the properties that you defined on your Container.

So, in your code, instead of adminEntity.mail_id, you should do adminEntity.properties.mail_id and it should work. In a similar fashion, you can replace substitute mail_id with any other properties that you may define.

Upvotes: 2

Related Questions