I have a p:commandLink in my xhtml with the value toggling between "Show"/"Hide". Is there any way by which I can get the value of this commandlink from the backing bean? I mean, I want to know what value the command link is showing currently i.e. Show/Hide?
Asked
Active
Viewed 3,357 times
0
-
instead you can have parameter passed in method call on commandLink. – Pankaj Kathiriya Aug 02 '13 at 14:54
-
What if you ever need to localize your webapp and thus provide button labels in different languages like so `value="#{msg['button.label']}"`? The need to know the button's value is then absolutely not a good solution as you'd have to take all those localized values into account as well. I recommend to reformulate your question to instead ask how to distinguish the pressed button if multiple buttons invoke the same method. – BalusC Aug 02 '13 at 16:16
-
@BalusC-I agree with you and you have correctly understood my requirement.From thecommand link I am invoking and actionListener method in which I want to distinguish whether it is currently showing "Show"/"Hide".Because based on this,I have to show a popup dialog to the user. e.g. if it is "Show",I want the dialog to come.However if "Hide", I just want to bypass the bean code which invokes this dialog.FYI, i am showing the dialog from backend.Is it possible assuming that there is no other language support is there? – AC_1985 Aug 03 '13 at 04:23
-
@PankajKathiriya-Could you please provide an example of that?e.g in my actionlistener method,I want to pass "Show" when the commandLink is reading as "Show"? – AC_1985 Aug 03 '13 at 04:42
2 Answers
1
To the point, the invoking component is just available in ActionEvent
argument:
<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle}" />
with
public void toggle(ActionEvent event) {
UIComponent component = event.getComponent();
String value = (String) component.getAttributes().get("value");
// ...
}
However, this is a poor design. Localizable text should absolutely not be used as part of business logic.
Rather, either hook on component ID:
String id = (String) component.getId();
or pass a method argument (EL 2.2 or JBoss EL required):
<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle(true)}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle(false)}" />
with
public void toggle(boolean show) {
this.show = show;
}
or even just call the setter method directly without the need for an additional action listener method:
<h:commandLink id="show" value="Show it" actionListener="#{bean.setShow(true)}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.setShow(false)}" />
0
As @BalusC suggested, your approach is not a good solution. But if you really want to do that, you can bind the component (p:commandLink
) to your backingbean, as seen in What is the advantages of using binding attribute in JSF?.
After the component was bound, you can access the value
attribute from the p:commandLink
.