4

I have the following page:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:o="http://omnifaces.org/ui"
    xmlns:thehub="http://java.sun.com/jsf/composite/components/thehub"
    template="/templates/masterTemplate.xhtml">
    <f:metadata>
        <f:viewParam
            id="returnToViewParam"
            name="returnTo"
            value="#{loginMB.returnTo}"
            required="true" />
        <f:viewParam
            id="oauth_verifierViewParam"
            name="oauth_verifier"
            value="#{loginMB.oauth_verifier}" />
        <f:viewParam
            id="oauth_tokenViewParam"
            name="oauth_token"
            value="#{loginMB.oauth_token}" />
        <f:event
            type="preRenderView"
            listener="#{loginMB.preRenderView()}" />
    </f:metadata>

    <ui:define name="body">
        <o:form
            id="loginForm"
            includeViewParams="true">
            <div class="form-vertical well">
                <h4>New Users</h4>
                <h5>
                    <h:link outcome="signup">Click here to create an account</h:link>
                </h5>
                <hr />
                <h4>Existing Users</h4>
                <h:commandButton
                    id="googleLoginCommandLink"
                    styleClass="btn"
                    action="#{loginMB.redirect()}"
                    value="Google">
                    <f:param
                        name="returnTo"
                        value="#{param.returnTo}" />
                </h:commandButton>
                <div class="clearfix"></div>
            </div>
        </o:form>
    </ui:define>
</ui:composition>

And the following bean:

@ManagedBean
@RequestScoped
public class LoginMB implements Serializable {
private static final long serialVersionUID = 1L;

private String returnTo;

public void redirect() {
    log.debug("redirect() returnTo:{}", returnTo);

......getter/setters
}

No matter what I do, I can't seem to get returnTo bound once the commandButton is clicked. Since this is a login page, I'd really not like to have LoginMB be a @ViewScoped bean.

Advice? Is there a better way to handle this scenario?

EDIT:

  • I'm running this on TomEE+ Server v1.5.1 which is served up by MyFaces 2.1.10
  • Added full page
  • Clarified the problem: Inside the redirect() function, returnTo is null
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • This should work just fine. Even more, the `` is completely unnecessary if you use ``. You only need it if you aren't using ``. To be sure, I have even tried it locally and it works just fine. Your concrete problem is likely caused elsewhere not visible in the information provided so far. What JSF impl/version are you using? Is the `` properly declared inside the template composition (and thus not outside)? – BalusC Mar 14 '13 at 20:54
  • Do you have getter and setter methods for `returnTo`? – Aritz Mar 14 '13 at 20:59
  • yes, there are getter/setters – Jonathan S. Fisher Mar 14 '13 at 21:17
  • @BalusC I added the full page... – Jonathan S. Fisher Mar 14 '13 at 21:23
  • AFAIK, MyFaces doesn't support `` inside ``. Try this instead: http://stackoverflow.com/questions/9856847/where-is-the-best-place-for-the-fmetadata-element/9857186#9857186 – BalusC Mar 14 '13 at 21:26
  • @BalusC What's funny is that the o:viewParams work on ViewScoped pages just fine, with the way it is. I did try the ui:define metadata approach however and it did not work – Jonathan S. Fisher Mar 14 '13 at 21:31
  • Things just got more hilarious... I copy/pasted the f:metadata into the masterTemplate and things worked as expected. Sigh... – Jonathan S. Fisher Mar 14 '13 at 21:34
  • oh snap... N/m I got it working with metadata. Not sure what I did wrong, but it's working now. Thanks! – Jonathan S. Fisher Mar 14 '13 at 21:39

1 Answers1

6

Your <f:metadata> is outside <ui:define> and is therefore completely ignored. Add an <ui:insert name="metadata"> to the master template and put the <f:metadata> inside an <ui:define name="metadata"> of the template client.

See also:


Once you fixed that, you can safely remove the <f:param> from the command button. This job is already done by <o:form includeViewParams="true">. If you didn't have it, then the <f:param> would indeed have been mandatory and you'd need to copypaste the same over all command links and buttons in the same form.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555