• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get name of bean into .tag file through attribute

 
Jacob Fenwick
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to find a way to pass the name of a bean into a .tag file.
I might want to use the same .tags multiple times for different beans, so I don't want to hardcode then name of the bean into the .tags.
However, when I simply pass the name of the bean into the .tag as an attribute, then make an el reference, it fails.
Is there a way to do this?
I also tried setting a c:set as a pointer to a bean and passing that as the value into the .tag attribute, but that didn't work either.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Jacob Fenwick
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TestBean.java


test.tag



tagtest.jsp



When I run the jsp I get a JspTagException
Invalid property in <set>: "table"
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume this is a continuation of this thread here?
http://www.coderanch.com/t/290645/JSP/java/params-jsp-setProperty-tag-file
It might have helped to keep it there rather than start a new thread.

In any case, the issue is with the following statement:
<c:set target="${beanname}" property="table" value="${param[paramName]}" />

The "target" attribute requires the actual object you want the property to be set on. I believe you are passing it the name of that object in scope, which is not quite the same thing.

If you want to pass the name, then you also have to know what scope the bean is in, and you would access it with the expression ${requestScope[beanName]} (for request scope)
ie the code would be:
<c:set target="${requestScope[beanname]}" property="table" value="${param[paramName]}" />

The alternative would be to change your tag to pass in an object rather than the name of that object in scope.
So your call would be <my:test beanname="${TestBean}" prefix="foo" />
You would also have to change your tag file appropriately to declare the attribute as an object other than String.

Hope this helps,
evnafets
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Evans:

The alternative would be to change your tag to pass in an object rather than the name of that object in scope.


Exactly. Why needlessly complicate the matter by adding an uneeded extra level of indirection?
 
Jacob Fenwick
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bear. I don't know why I didn't realize what the problem was before, it should have been obvious
Stefan, I probably should have kept it in the same thread, I don't know why I didn't.
Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!