1

In a previous question BalusC gave me good advice on how a button, in place of a commandButton is useful for non ajax navigation. In particular it updates the destination address in the http: position which is useful for the user to bookmark a page.

I tried to use this information to my advantage until I came upon a problem. In a button I tried to use outcome="#{backing.something}" to find out that it gives me a null result. This looks like a timing problem in that action="#{}" is evaluated only when the button is pressed whereas outcome apparently wants a fixed string which gets checked when the page is loaded.

So I went back to commandButton with ajax="false". This has a problem that my navigation address is the page I came from, not the one I am navigating to. This is the wrong bookmark for the user.

I appreciate all the help I have received in stackoverflow on my learning exercise. Ilan

Community
  • 1
  • 1
Ilan Tal
  • 457
  • 2
  • 8
  • 21

1 Answers1

3

The <h/p:button outcome> is not intented to invoke a bean action method, but to contain the outcome string directly. Any EL in there is evaluated immediately as a value expression. So the method behind it would immediately be invoked when you just open the page containing the <h/p:button>.

There are in your particular case basically two ways to invoke a bean action method on navigation. If you need to invoke it before the navigation takes place and the action isn't intented to be re-invoked everytime when the enduser reopens/reloads the GET request, then make it a POST-Redirect-GET request. It's a matter of adding faces-redirect=true to the outcome value in query string syntax.

E.g.

<p:commandButton action="#{bean.submit}" ... />

with

public String submit() {
    // ...

    return "nextpage?faces-redirect=true";
}

This way the browser will be redirected to the target page after POST, hence the enduser will see the target URL being reflected in the address bar.

Or if you need to invoke the action everytime when the enduser reopens/reloads the GET request, do the job in the (post)constructor or preRenderView listener method of the request/view scoped backing bean instead.

E.g.

<p:button outcome="nextpage" ... />

with

@ManagedBean
@RequestScoped
public class NextpageBacking {

    public NextpageBacking() {
        // In constructor.
    }

    @PostConstruct
    public void onPostConstruct() {
        // Or in postconstructor (will be invoked after construction AND injection).
    }

    public void onPreRenderView() {
        // Or before rendering the view (will be invoked after all view params are set).
    }

    // ...
}

The pre render view listener method needs to be definied as follows in the nextpage

<f:event type="preRenderView" listener="#{nextpageBacking.onPreRenderView}" />

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thanks. I used the faces-redirect=true. Now I am getting an annoying message in glassfish: WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /ww1, because request parameters have already been read, or ServletRequest.getReader() has already been called It seems reasonable that faces-redirect could case such an effect. I looked around and it seems that in web.xml I can put something like . However the format for my case isn't exact because of the web.xml which Netbeans generates. So for the moment, I have the message – Ilan Tal Jun 04 '12 at 07:05
  • The Netbeans web.xml says: So the default charset is already UTF-8. What is going on??? – Ilan Tal Jun 04 '12 at 07:09
  • 1
    http://stackoverflow.com/questions/7643484/how-do-i-get-rid-of-annoying-message-on-output-log-pwc4011-servletrequest-getr – BalusC Jun 04 '12 at 12:02
  • Thanks. My mistake was I was looking at web.xml instead of glassfish-web.xml. I used Netbeans to generate it and then modified it. Now all is well. – Ilan Tal Jun 04 '12 at 13:16
  • Since the purpose of asking help is to learn something, I didn't have a chance to try your suggestion of – Ilan Tal Jun 05 '12 at 09:41