Sanjay
Sanjay

Reputation: 113

Redirect to XHTML file in a JSF Composite Component

I have a requirement to add a button in one of the xhtml component. When a user clicks the button, a new window should open redirecting the user to another xhtml file which is also in the same component project.

This is what I have in my component project,

|->src
  |-> main
    |->java
      |->META-INF
        |->faces-config.xml
          |->resources
             |->components
                  |->A.xhtml
                  |->B.xhtml

I need to add a button in A.xhtml file which redirects the user to B.xhtml opening a new window. This component is being used in other projects. I tried commandButton with target=_blank, a new window opens but doesn't redirect to B.xhtml.

I observed that if I use ui:include src="B.xhtml" tag in A.xhtml file, then the content of B appears in A. But couldn't find why it is not able to redirect in a new window. Not sure what I am missing and would like to know how this can be achieved.

Upvotes: 0

Views: 976

Answers (2)

Michele Mariotti
Michele Mariotti

Reputation: 7459

you can't redirect to a component, you have to redirect to a PAGE that contains that component.

imagine you have

application
    resources
        components
            A.xhtml
            B.xhtml
    pages
        page1.xhtml
        page2.xhtml

and

page1.xhtml contains component A

page2.xhtml contains component B

page1.xhtml contains

<!DOCTYPE html>
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:p="http://primefaces.org/ui" 
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions"
    xmlns:cc="http://java.sun.com/jsf/composite/components">

    <ui:composition template="/WEB-INF/templates/template.xhtml">
        <ui:define name="content">

            <cc:a value="someValue" foo="someBar"/>

        </ui:define>
    </ui:composition>
</html>

and A.xtml is

<h:form prependId="false" id="form" target="_blank" >

    <p:commandButton value="open" ajax="false" action="page2.xhtml"/>

</h:form>

so, upvote for @Java, since your answer is almost correct, just change action to address a page and not a component in resources

Upvotes: 0

Java
Java

Reputation: 2489

@Sanjay you can easily achieve this by using primefaces commandbutton component by setting target as '_blank' and ajax as 'false'.

<h:form prependId="false" id="form" target="_blank" >

<p:commandButton value="Click me to open new url"  ajax="false" action="B.xhtml"/>

</h:form>

Hope this helps .

Upvotes: 1

Related Questions