1

I have a problem with loading Facelets pages from Javascript. I'm working with Netbeans 7.2, Glassfish 3.1.2 and Java EE 6.

I made a simple test page:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <script type="text/javascript">
        window.location.href = "index.xhtml";
    </script>
    <title>winq match!</title>
</h:head>
<h:body>
    <h1>WING MATCH!!</h1>
    <h:form>
        <h:commandButton id="Next" value="weiter" action="index"/> 
    </h:form>
</h:body>

The index.xhtml page that should be loaded with window.location.href is:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form>
        <h:outputText value="TestTestTestTestTest"/>
        msg <h:inputText id="ema" value="#{testBean.inputValue}" maxlength="1" />      
        <h:commandButton id="but" value="Submit" action="index"/>
    </h:form>
</h:body>

The page is loaded but not parsed and thus the h: tags are not interpreted by the browser. After searching the web on this it seems that I'm the only one with a problem like this. Maybe I´ve misunderstood some aspects of JSF. I hope to get some advise on this.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Noesis
  • 13
  • 3

1 Answers1

0

You need to make sure that the request URL matches the URL pattern of the FacesServlet as definied in webapp's web.xml. It's namely the one responsible for performing all the JSF/Facelets works.

For example, if you've mapped it on *.jsf, then you should open the page on exactly that URL pattern so that the FacesServlet is properly invoked and will locate the index.xhtml file and do all the necessary stuff.

window.location.href = "index.jsf";

Alternatively, you can also change the URL pattern of the FacesServlet to *.xhtml. This way you never need to worry about virtual URLs.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for your answers. This leds me to the solution. In web.xml I declared: Faces Servlet /faces/*. And I also tried window.location.href = "/faces/index.xhtml" but this leads to the same problem as only "index.xhtml". My mistake was the leading slash. It has to be window.location.href = "faces/index.xhtml". Thx again to all, you´ve saved my day. – Noesis Jan 25 '13 at 09:06
  • After changing `/faces/*` to `*.xhtml` I get: "/faces/index.xhtml Not Found in ExternalContext as a Resource" on pageload. Maybe some other parameters have to be also changed according to this (or it has to be /faces/*.xhtml, i will try this later). – Noesis Jan 26 '13 at 08:50
  • Just remove `/faces` from URL in all places. – BalusC Jan 26 '13 at 11:51