You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Pich <pe...@edb.com> on 2007/05/26 00:49:10 UTC

Find current component

Hi,

Is it possible to, by using FacesContext.getCurrentInstance()..., find the
"current" component. To be more precise I want to be able to find out, in
the method in the rendered attribute for a specific component, find out by
using FacesContext... which component in the ViewRoot I am currently working
with?

Best regards

Pichdude
-- 
View this message in context: http://www.nabble.com/Find-current-component-tf3818677.html#a10811350
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Find current component

Posted by Andrew Robinson <an...@gmail.com>.
There have been easier solutions using security, and you can create a
backing bean as well to do this if you want. There was just a recent
thread on this (rendering and security) here:

http://www.nabble.com/How-to-Secure-Views-in-JSF-t3811634.html

If you are using facelets (or JSF 1.2 as well?) you can use custom EL
functions to see if a set/map/list contains a string (permission in
your case). For example:

<t:inputText rendered="#{my:collectionContains(myBean.permissions, 'Admin')}" />

If you use a map, you don't need EL:

public class UserPermissionBean {
  private Map<String, Boolean> permissions;
  public Map<String, Boolean> getPermissions() { return permissions; }
}

<t:inputText rendered="#{not empty userPermBean.permissions['Admin']" />

(The boolean is just a place holder)





On 5/27/07, Pich <pe...@edb.com> wrote:
>
> Ok, I realize this solution might not be a good design.
>
> But what I am trying to do is to decide whether a specific component, must
> of the time commandButton, should be rendered or not. Deciding this is a
> matter of which permissions a specific user has. A user has a set of
> permissions and my thought was that I could specify an attribute for the
> component called "permission" holding the specific permission key. In the
> rendered attribute a method on a permissionBean is called. This method would
> in its turn call a service with the value of the permission attribute. The
> service knows the users permissions and would return the result to the bean,
> and the bean would return true or false.
>
> However, when I started implenting I realized I was not able to get the
> attribute "permission" from anywhere, unless I bind all components in a
> componentMap<UIComponent>, so that is why I dit that solution. So now I have
> all components using the attribute "permission" binded from 1, 2, 3 and so
> on in the map. In the rendered method permissionBean.getPermission() I have
> a counter going from 1, 2, 3 and so on. For each call to the method the
> counter is added with 1. I use this counter to get the "current component"
> componentMap.get(counter).
>
> Since I dit not like this design I was trying to figure out if it was a way
> to find the "current component" using the FacesContext. But I guess there is
> not.
>
> But do you have any idea on how I could design this and still solve my
> "permission to render" problem?
>
> Best regards
>
> Pich
>
>
>
>
> David Delbecq-2 wrote:
> >
> > Pich a écrit :
> >>> The renderer has access to the component and thus the attributes of that
> >>>
> >> component.
> >>
> >> This is what I want to do. How do I have accecss to the component and its
> >> attribute in the code of the rendered method? And remember that I do not
> >> use
> >> one component binding for each component. I have a Map<UICompontent> that
> >> I
> >> have all components in. How do I know which one, perhaps out of 10
> >> components, is calling the rendered attribute?
> >>
> >> Best regards
> >>
> >> Pichdude
> >>
> > Hi,
> >
> > i think you have a big design issue. You are mixing the value binding of
> > the rendered attribute (which is a method use to know if a component
> > need to be rendered depending on "the state of a given bean"), whith
> > what andrew told you which is related on the "renderer" (the renderer
> > has access to current component because it's passed as a parameter).
> >
> > I think, in you case, as you already have component bindings in a map,
> > just create another Map for the rendered attribute. JSF by itself do not
> > write somewhere the "current component". However, as andrew suggested,
> > the datatable store the current index and current value in a request
> > scoped bean so that inner component can use it, maybe you can create
> > your own component bases on this code.
> >
> > However, as said, rendered attribute should be linked to a bean status,
> > not the current component, otherwise that would mean you would mix view
> > logic and buisness logic...
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Find-current-component-tf3818677.html#a10826713
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>

Re: Find current component

Posted by Pich <pe...@edb.com>.
Ok, I realize this solution might not be a good design.

But what I am trying to do is to decide whether a specific component, must
of the time commandButton, should be rendered or not. Deciding this is a
matter of which permissions a specific user has. A user has a set of
permissions and my thought was that I could specify an attribute for the
component called "permission" holding the specific permission key. In the
rendered attribute a method on a permissionBean is called. This method would
in its turn call a service with the value of the permission attribute. The
service knows the users permissions and would return the result to the bean,
and the bean would return true or false.

However, when I started implenting I realized I was not able to get the
attribute "permission" from anywhere, unless I bind all components in a
componentMap<UIComponent>, so that is why I dit that solution. So now I have
all components using the attribute "permission" binded from 1, 2, 3 and so
on in the map. In the rendered method permissionBean.getPermission() I have
a counter going from 1, 2, 3 and so on. For each call to the method the
counter is added with 1. I use this counter to get the "current component"
componentMap.get(counter).

Since I dit not like this design I was trying to figure out if it was a way
to find the "current component" using the FacesContext. But I guess there is
not.

But do you have any idea on how I could design this and still solve my
"permission to render" problem?

Best regards

Pich




David Delbecq-2 wrote:
> 
> Pich a écrit :
>>> The renderer has access to the component and thus the attributes of that
>>>     
>> component.
>>
>> This is what I want to do. How do I have accecss to the component and its
>> attribute in the code of the rendered method? And remember that I do not
>> use
>> one component binding for each component. I have a Map<UICompontent> that
>> I
>> have all components in. How do I know which one, perhaps out of 10
>> components, is calling the rendered attribute?
>>
>> Best regards
>>
>> Pichdude
>>   
> Hi,
> 
> i think you have a big design issue. You are mixing the value binding of
> the rendered attribute (which is a method use to know if a component
> need to be rendered depending on "the state of a given bean"), whith
> what andrew told you which is related on the "renderer" (the renderer
> has access to current component because it's passed as a parameter).
> 
> I think, in you case, as you already have component bindings in a map,
> just create another Map for the rendered attribute. JSF by itself do not
> write somewhere the "current component". However, as andrew suggested,
> the datatable store the current index and current value in a request
> scoped bean so that inner component can use it, maybe you can create
> your own component bases on this code.
> 
> However, as said, rendered attribute should be linked to a bean status,
> not the current component, otherwise that would mean you would mix view
> logic and buisness logic...
> 
> 

-- 
View this message in context: http://www.nabble.com/Find-current-component-tf3818677.html#a10826713
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Find current component

Posted by David Delbecq <de...@oma.be>.
Pich a écrit :
>> The renderer has access to the component and thus the attributes of that
>>     
> component.
>
> This is what I want to do. How do I have accecss to the component and its
> attribute in the code of the rendered method? And remember that I do not use
> one component binding for each component. I have a Map<UICompontent> that I
> have all components in. How do I know which one, perhaps out of 10
> components, is calling the rendered attribute?
>
> Best regards
>
> Pichdude
>   
Hi,

i think you have a big design issue. You are mixing the value binding of
the rendered attribute (which is a method use to know if a component
need to be rendered depending on "the state of a given bean"), whith
what andrew told you which is related on the "renderer" (the renderer
has access to current component because it's passed as a parameter).

I think, in you case, as you already have component bindings in a map,
just create another Map for the rendered attribute. JSF by itself do not
write somewhere the "current component". However, as andrew suggested,
the datatable store the current index and current value in a request
scoped bean so that inner component can use it, maybe you can create
your own component bases on this code.

However, as said, rendered attribute should be linked to a bean status,
not the current component, otherwise that would mean you would mix view
logic and buisness logic...

Re: Find current component

Posted by Pich <pe...@edb.com>.
> The renderer has access to the component and thus the attributes of that
component.

This is what I want to do. How do I have accecss to the component and its
attribute in the code of the rendered method? And remember that I do not use
one component binding for each component. I have a Map<UICompontent> that I
have all components in. How do I know which one, perhaps out of 10
components, is calling the rendered attribute?

Best regards

Pichdude
-- 
View this message in context: http://www.nabble.com/Find-current-component-tf3818677.html#a10822455
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Find current component

Posted by Andrew Robinson <an...@gmail.com>.
> So my question is, can I in the rendered method get the calling component
> from FacesContext?

No you can't

I can't figure out what you are doing though. The renderer has access
to the component and thus the attributes of that component. Why do you
need the faces context to give you the component when you already have
access to it? Are you trying to get a reference for use in EL? If so
that is not easy (I have tried, and have not found an adequate
solution)

Perhaps you can create a dummy component to surround the components
you want to access:

<my:captureComponent var="mycomponent">
</my:captureComponent>

Then in the decode, validate, update, encodeBegin, encodeChildren and
encodeEnd, you add that variable to the variable mapper (dataTable
does this with it's "var" attribute, so you can look at that code for
an example). Then at the end of that method set it back to what it was
before (push and pop a stack), that way it works for nested usage.

Then evaluating "#{mycomponent}" as a value binding will give you that component

RE: Find current component

Posted by Pich <pe...@edb.com>.
Hi, maybe I was bit short in my question but I'll try to be more precise. My
request is quite connected with your first solution. However, my
requirements are unfortunately not that simple that I can have 1-1
connection between UI component on JSF page and binding component in my
backing bean.

I want to be able to check dynamically 0 or many components on JSF page if
they should be rendered. If a component should be rendered or not is decided
using f:attribute for the specific component. Since it should be easy to
have as many components with this attribute I have the binding to an element
in a map of UIComponent's [binding="#{mybean.componentMap[x]}"]. x is 0, 1,
2, 3 etc depending on how many components is on the JSF page. I also use the
rendered attribute, and have a method in which my problem is situated. Since
I do not have, as I already mentioned, a 1-1 connection between JSF
component and binding bean component I am not sure which component in my
component map actually is calling the rendered attribute. Is it component 1,
2, 3 or 4?

So my question is, can I in the rendered method get the calling component
from FacesContext? If this is possible, I guess I do not really need the
component binding.

Hope you understand what I mean.

Thanks in advance!

/Pichdude



Nebinger, David wrote:
> 
>> Is it possible to find out which component 
>> in the ViewRoot I am currently working with?
> 
> What do you mean, which component you're working with?
> 
> Perhaps you're really asking a binding question, i.e. what UI component
> is my particular backing bean member populated from or somesuch.  For
> this, you add a UIComponent member (with appropriate getter/setter) then
> use the [binding="#{mybean.component}"] in your jsf tag.  This way your
> backing bean has direct access to the component that is bound to a
> field.
> 
> If you're asking a question about custom component development and
> 'where in the tree am I?', this info is available by existing inherited
> methods for retrieving the parent component, you can repeatedly call
> this until you get to the root.
> 
> If neither of these are really the question you were asking, well it
> would be a reflection of the ambiguity in the original post...
> 
> 

-- 
View this message in context: http://www.nabble.com/Find-current-component-tf3818677.html#a10817632
Sent from the MyFaces - Users mailing list archive at Nabble.com.


RE: Find current component

Posted by "Nebinger, David" <dn...@tbbgl.com>.
> Is it possible to find out which component 
> in the ViewRoot I am currently working with?

What do you mean, which component you're working with?

Perhaps you're really asking a binding question, i.e. what UI component
is my particular backing bean member populated from or somesuch.  For
this, you add a UIComponent member (with appropriate getter/setter) then
use the [binding="#{mybean.component}"] in your jsf tag.  This way your
backing bean has direct access to the component that is bound to a
field.

If you're asking a question about custom component development and
'where in the tree am I?', this info is available by existing inherited
methods for retrieving the parent component, you can repeatedly call
this until you get to the root.

If neither of these are really the question you were asking, well it
would be a reflection of the ambiguity in the original post...