Reputation: 7386
i have a working <p:button>
that invokes a method inside a managed bean as follows:
<h:form id="loginForm">
<p:button value="Facebook Connect"
href="#{loginPageCode.facebookUrlAuth}" />
<br />
<h:outputText value="#{loginPageCode.userFromSession}"/>
</h:form>
and i decided to replace it with a link, so i did the following:
<h:form id="loginForm">
<h:commandLink action="#{loginPageCode.getFacebookUrlAuth}"
value="#{loginPageCode.userFromSession}" />
</h:form>
but unfortunately the <h:commandLink>
doesn't invoke the method but i don't know why?
Note: the method inside the managed bean returns a URL to a servlet, so the commandLink have to call these servlet when its clicked using the URL returned>
Upvotes: 0
Views: 1435
Reputation: 1108672
There'a a major misunderstanding going here. The <p:button>
does not invoke an action method in a managed bean at all. The EL expression in the href
attribute is not evaluated when the button is pressed. It is evaluated when the HTML representation of the button is to be rendered. If you put a break point on the getter of facebookUrlAuth
, then you'll see that it's invoked when the page with the button is displayed and not when the button is pressed. If you check the JSF-generated HTML output by rightclick, view source, then you'll see that the button navigates by JavaScript in onclick. It's a pure navigation button, not a submit button.
The <h:commandLink>
generates a link which uses JavaScript to submit a parent form. It is not designed to perform pure navigation. For that you should be using <h:outputLink>
, <h:link>
or even <a>
instead. As it's apparently an external URL, the <h:link>
is insuitable.
Thus, so
<h:outputLink value="#{loginPageCode.facebookUrlAuth}">Facebook Connect</h:outputLink>
or just
<a href="#{loginPageCode.facebookUrlAuth}">Facebook Connect</a>
This all would have been more straightforward if you understand that JSF is merely a HTML code generator and when you're familiar with basic HTML.
Upvotes: 2
Reputation: 1518
When using you are just creating a simple HTML link on the page. when you click on it, an HTTP GET is executed from the browser. And with this syntax you don't need any at all. There is no form submit
When using and you are using a standard JSF form submit with the input you could have keyed in. In your case : value is the label of yout button execute is a java method on your bean.
public String execute()
{
return "nextPage";
}
JSF uses the return of this mececute method to navigate "inside" JSF, So "nextPage" could be an alias to a navigation rule in the faces-config.xml or with JSF2 the name of JSF page : nextPage.xhtml.
In your case #{loginPageCode.getFacebookUrlAuth} would return a String like : "http://www.facebook.com/help/usernames"
and JSF cannot navigate with that : it is not an alias nor a page.
Upvotes: 0