6

I'm using JSF 2.0 and I have a problem with navigation after both commandLink and commandButton. I use following code:

<h:commandLink action="login?faces-redirect=true" 
    value="#{showElementBean.showElement()}"> Login </h:commandLink>  

<h:commandButton action="login?faces-redirect=true" value="Move to login.xhtml" />

These tags are inside a form, login is just an example. Result of clicking on rendered controls is always POST with refresh of a current page. What do I wrong?

Edit: According to comments of BalusC I' adding real code fragment:

<h:commandLink actionListener="#{showElementBean.showElement(element)}" 
    value="View" > </h:commandLink>

I have a page with a list of elements and I want to add links that leads to element view page. Thus I need to pass this element to a show page. I'm JSF primer, e.g. in Rails I'd use GET and URL params, but I don't know how to do it 'in JSF-way'.

mrzasa
  • 22,895
  • 11
  • 56
  • 94

4 Answers4

11

There are a lot of possible causes for this behaviour. They are all cited in the following answer, along with solutions: commandButton/commandLink/ajax action/listener method not invoked or input value not updated.

However, in your particular case, you seem rather to be interested in plain GET requests instead of POST requests, as all you want is simple page-to-page navigation. In that case, you need a <h:link> or <h:button> instead:

<h:link outcome="login" value="Login" />

<h:button outcome="login" value="Move to login.xhtml" />

(I have no idea what you're trying to do with both #{showElementBean.showElement()} and Login as command link value, so I omitted the former)

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Login is maybe not the best example. I have a list of items and I want to pass one of them to show item view. I think GET is sufficient, but I don't know how to pass params and read them before rendering show page. Btw. normal link and button works perfectly. – mrzasa Nov 22 '11 at 13:20
  • 1
    Use `` in link/button and `` in target page. See also [Communication in JSF 2.0 - Processing GET request parameters](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters). By the way, you should really be certain whether it's supposed to be either GET (bookmarkable, searchbot-indexable) or POST (not bookmarkable/indexable). In your examples you were however not invoking any bean action methods, so I understood that you just want GET after all. – BalusC Nov 22 '11 at 13:22
  • Thanks I added more detail to explain using POST/GET. – mrzasa Nov 22 '11 at 13:47
  • So you want to use GET and URL params? Then the `` recommendation still applies. – BalusC Nov 22 '11 at 13:48
  • Finally I used GET with solution described in your blog. – mrzasa Nov 22 '11 at 15:17
0

Noticed that backing bean method is not called if the form is for file upload: <h:form name="searchForm" enctype="multipart/form-data" method="post" action="/search">

Vikram
  • 11
  • 1
    Your problem is caused by something else than the OP's concrete problem. If you need help on it, just ask a new question instead of posting it as an irrelevant answer. – BalusC Jul 16 '12 at 13:22
  • I saw the same behavior. If I add enctype as multipart, the backing bean is not called anymore. Not sure how I can fix it. – A M Oct 09 '14 at 01:23
0

Refer this info: JSF HTML Tags

h:commandButton

The commandButton tag renders an HTML submit button that can be associated with a backing bean or ActionListener class for event handling purposes. The display value of the button can also be obtained from a message bundle to support internationalization (I18N).

Example

<h:commandButton id="button1" value="#{bundle.checkoutLabel}" action="#{shoppingCartBean.checkout}" />

HTML Output

<input id="form:button1" name="form:button1" type="submit" value="Check Out" onclick="someEvent();" /> 


h:commandLink

The commandLink tag renders an HTML anchor tag that behaves like a form submit button and that can be associated with a backing bean or ActionListener class for event handling purposes. The display value of the link can also be obtained from a message bundle to support internationalization (I18N).

Example

<h:commandLink id="link1" value="#{bundle.checkoutLabel}" action="#{shoppingCartBean.checkout}" /> 

HTML Output

<a href="#" onclick="someEvent();" id="form:link1">Check Out</a>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • -1: You are not answering his concrete question at all and you were just copypasting from jsftoolbox.com website without citing the source. – BalusC Nov 22 '11 at 13:07
  • @Balu: Nothing like that. Am trying to explain this so he might understand, what he is missing on his full code that which we haven't seen. – Siva Charan Nov 22 '11 at 13:11
  • 1
    This is not an answer. You are not explaining why the OP is facing the particular problem. Just post a link to jsftoolbox.com as a comment on the question then. – BalusC Nov 22 '11 at 13:14
  • Thanks Balu for your concern. Actually I thought, if I post the content then it may help him and others too. Ok then I will wait for some time, if any more downvotes are there and others too feeling this is not useful information then I will remove my post. – Siva Charan Nov 22 '11 at 13:20
-3

I also faced with that issue and adding the <h:form><h:commandLink></h:commandLink> </h:form> solved my problem.

tviet
  • 1
  • 1
    You're not answering the OP's concrete problem at all. OP clearly mentioned that he already has them in a form. In any case, Stack Overflow is not the right place for "Me too!" clutter like as on those old fashioned discussion forums which made those places a hard-to-search mess. Just upvote the helpful answer if you can and move on. – BalusC Aug 06 '13 at 11:08