0

I need to navigate from one page to another on button click. It works if I do it like this

<h:commandButton value="Search" action="pageTwo.xhtml" />

but I need to do other things and not just navigate. When I try to use a function

<h:commandButton value="Search" actionListener="#{myLittleBean.doForward}" />


public String doForward() {
    // ...
    return "pageTwo";
}

it just reloads the pageOne. Where is my mistake?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user1418018
  • 189
  • 1
  • 4
  • 17

2 Answers2

3

Your mistake is referencing the action method as an action listener instead of a real action. An action listener isn't intented to execute business actions and perform navigation. An action listener is intented to prepare some conditions required for the real action, such as setting a property.

If you just fix the wrong attribute,

<h:commandButton value="Search" action="#{myLittleBean.doForward}" />

then everything should go well.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have changed that and now I have an error `javax.el.PropertyNotFoundException: /pageOne.xhtml @92,68 action="#{myLittleBean.doForward}": Target Unreachable, identifier 'myLittleBean' resolved to null` – user1418018 Nov 20 '13 at 03:34
  • That may happen when there's no such bean registered. Given that you're using JSF 2.x, you need to make sure that you've a `package com.example; @javax.faces.bean.ManagedBean @javax.faces.bean.RequestScoped public class MyLittleBean { public String doForward() { return "pageTwo"; }}` class. – BalusC Nov 20 '13 at 12:52
  • On a second thought, you'd have faced exactly the same problem when using `actionListener`. So something related to bean management has changed in your project/environment between the moment you posted your question and the moment you implemented my answer. – BalusC Nov 20 '13 at 14:29
-1

I think This is the problem of your ManagedBean you can also try this one:

**public String doForward() {
// ...
return ("pageTwo.xhtml");
}**

if it does not work then use explicit ManagedBean

  • This isn't true. For future answers, if you're not sure, just test it before posting as answer. Additional bonus is, you actually learn something by true experience which is generally unforgettable. – BalusC Nov 20 '13 at 15:33