0

Currently I'm getting data in jsp using scriptlet but now want to use JSTL so I'm trying to convert my scriptlet code to JSTL. But for following scenario I don't know how to get data in jstl.

Let me explain by example:

There are 3 objects

  1. User (user_id, username, password)
  2. Box (box_id, box_name,list_of_boxCat)
  3. BoxCat (box_cat_id,box_id,user_id,cat_name)

Now I need to display list of boxes a user own. So i have created a list of Box objects by firing query in servlet and pass that list in jsp in request attribute then access it in jsp. Until this everything goes fine. But now I have to access BoxCat object which do not have any reference from Box object directly. To get BoxCat object I have to combine Userid and Box id and then I can get BoxCat id. So in scriptlet I call my DAO and get list by running query. But I don't know how to do this JSTL. Anyone please help me on how to do this?

Harry Joy
  • 58,650
  • 30
  • 162
  • 207

1 Answers1

3

You should redesign or map your model so that it suits whatever your view needs. Does the view need a List<BoxCat> as a property of User or maybe BoxCat as a property of Box? If so, then make that so and change your controller and DAO to fill that beforehand.

Otherwise you will end up in clumsy and potentially memory-inefficient workarounds using a mapping of the entities by their ID such as Map<Long, Entity>.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Mappings are like this: User can have multiple Box. Box can have Multiple BoxCat. There is only one BoxCat for a unique combination of user and box. On the jsp I have list of box and have to show boxCat name associated with that box for logged in user. How can I do that? – Harry Joy Aug 19 '11 at 11:39
  • In scriptlet I run this query: `"select * from boxcat where box_id="+box.getId()+" and user_id="+user.getUserId();"` and get boxCat object but I'm not able to do same in JSTL. – Harry Joy Aug 19 '11 at 11:45
  • If you really can't change the model accordingly, then your best bet is to load all `BoxCat` items of the current user into a `Map` where the key is the `Box` ID. Then, in EL you could just do `${boxCats[box.id].name}` to display the name. Or you could even have a `Map` where the value is the box name. – BalusC Aug 19 '11 at 11:50
  • Should I have to pass that map in request attributes? – Harry Joy Aug 19 '11 at 11:59
  • Oh sure, otherwise `${boxCats}` will give nothing :) – BalusC Aug 19 '11 at 12:00
  • You have a great knowledge of JSP. Awesome !!!!. One last thing: I use `<%= MyUtils.generateCode()%>` to access a static method of a class but when I write it in JSTL it gives error. – Harry Joy Aug 19 '11 at 12:16
  • How to solve it depends on what exactly it is doing. You could make it a bean instead, you could get it in the servlet instead, you could make it an EL function, etc. – BalusC Aug 19 '11 at 12:19
  • how can I make an EL function for this? – Harry Joy Aug 19 '11 at 12:29
  • If you don't need to pass any arguments, it likely doesn't need to be an EL function at all. But at any way, you can find an example somewhere near the bottom of this answer: http://stackoverflow.com/questions/2523430/hidden-features-of-jsp-servlet/2525995#2525995 Again, the proper approach depends on what exactly it is doing. Is it returning a bunch of HTML code? Then it should actually be done by a JSP include or tag file. – BalusC Aug 19 '11 at 12:35
  • Yes there are no arguments in it. It is just returning a 5 character long code. – Harry Joy Aug 19 '11 at 12:37
  • What does that code represent? What is it used for? Does it return a constant value or a random value or a request/session dependent value? – BalusC Aug 19 '11 at 12:41