tags:

views:

248

answers:

3

OK, so I am trying to set up a simple JSF application. I'm using NetBeans 6.8, Glassfishv3 and Maven2. I made a JSP document like so:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"&gt;
<f:view>
    <head>
        <title><h:outputText value="#{Welcome.title}"/></title>
    </head>
    <body>
        <h:outputText value="Welcome"/>
    </body>
</f:view>
</html>

Problem is, if I navigate to this page (http://myHost/myApp/faces/welcome.jspx), it is returned as an XML document, but with the ${Welcome.title} value populated:

<?xml version="1.0" encoding="UTF-8"?>
<html><head><title>Gymix - Welcome</title></head><body>Welcome</body></html>

In Internet Explorer this looks like I would have opened an XML document. In Google Chrome the title is printed next to the text Welcome and instead of the title the URL to the page is printed on the tab.

If I change the JSP document to a plain JSP page (taglibs instead of xmlns and so on) it works and I get a proper page returned. Any ideas on what's wrong? Thanks!

Edit: sadly none of the quick fixes fixed this, so I'll look into this more. BTW, my pom.xml has jsf-api and jsf-impl dependencies with the version for both set to 1.2_14

+2  A: 

I think you need to put in a valid doctype.

This would go below your xml declaration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
darren
Hmm I tried this, but NetBeans is giving me an error (can't open file or something) that looks like a compilation error...
Sevas
A: 

You have to tell the browser what you're sending. I'm not exactly sure of how to do it inside a JSP though, so you'll have to figure out yourself or wait until someone more knowledgeable than me tells you.

You have to send the Content-Type HTTP header indicating your file is a text/html; charset=UTF-8.

Content-Type: text/html, charset=UTF-8

zneak
Oh, and as Darren said, setting a doctype wouldn't hurt either. However, Internet Explorer will still fail at opening the page if you keep sending it as XML.
zneak
You can set the content-type based on the extension of the request (.jspx) with a <mime-mapping> element in web.xml
matt b
You'll also want to lose the `<?xml` declaration, which if output will trip up IE6-7's doctype sniffer, putting it in Quirks Mode. `<?xml version="1.0" encoding="utf-8"?>` does nothing anyway as those are the default properties of an XML document.
bobince
bobince is entirely correct, but I would add: Facelets **removes** the XML declaration during rendering the page. The XML declaration is simply there because Facelets needs to parse the page using a XML based tool first :)
BalusC
+2  A: 

Aside from the fact that you need to set the proper doctype and content type so that the browser knows what to do with the page, you also should get rid of the old fashioned jspx format and use xhtml format to get the most benefit of Java EE 6-shipped JSF 2.0 and Facelets.

The given code should be changed to:

<?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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"&gt;
    <h:head>
        <title>#{welcome.title}</title>
    </h:head>
    <h:body>
        Welcome
    </h:body>
</html>

Note that the doctype is included and that JSF 2.0 / Facelets will automatically take care about the right content type with help of the <h:head> component. Also note the absence of the <f:view> tag, this isn't needed anymore in Facelets.

You probably also need to reconfigure your webapp to make use of the full powers of JSF 2.0 and Facelets. To learn more about JSF 2.0 and Facelets, I strongly recommend to go through the Java EE 6 tutorial part II chapters 4-9.

Good luck.

Update: as per the comment of bobince: I would add an important note; it is true that the XML declaration (the first line) would mess the rendering mode of some webbrowsers (also see the site behind the doctype link here above), but that's certainly not an issue here. Facelets removes the XML declaration during generating the HTML of the page. The XML declaration is simply there because Facelets needs to parse the page using a XML based tool first. We're talking about a component based MVC framework and XML based templating technology, not about a plain vanilla HTML page ;)

BalusC
Hmm NetBeans doesn't recognize h:head and h:body. Am I possibly using an old version of jsf-impl and jsf-api (1.2_14)?
Sevas
Glassfish v3 already ships with JSF 2.0. You don't need to include any libraries yourself. Just follow the Java EE 6 and the Netbeans 6.8 tutorials.
BalusC