2

I am using eclipse juno and I am trying to build a website. I am building the User interface and I would like to use JSF. The following code runs and when I run the file the information is displayed correctly on the screen.

<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 <link rel="stylesheet" type="text/css" href="Stylesheets/flashcard.css" />

<title>Insert title here</title>
</head>
<body>
<f:view>
        <h:form>
        <h:commandButton value="Click"></h:commandButton>

        </h:form>
</f:view>

However I have read that it is better practice to write the code like this. This code just displays a blank page.

<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

         <link rel="stylesheet" type="text/css" href="Stylesheets/flashcard.css" />

        <title>Insert title here</title>
        </head>
        <body>
        <f:view>
                <h:form>
                <h:commandButton value="Click"></h:commandButton>

                </h:form>
        </f:view>
    </body>
    </html>

The difference is subtle but in the second file I don't use jsp <%@ tags if this makes sense. When creating the second file I chose JSF xhtml or something. Does anybody know what the problem might be?

KennyBartMan
  • 940
  • 9
  • 21

1 Answers1

2

Just rename the extension of the physical view file from some.jsp to some.xhtml. You don't need to change the extension in the URL which you originally used. If you opened it by /some.jsf, then you should keep opening it by /some.jsf.

Note that I assume that you're using JSF2 and not the legacy JSF 1.x. Facelets is namely not natively supported by JSF 1.x. Also note that Facelets is not "just a better practice", you must use it in JSF2 as JSP is deprecated.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You're welcome. You're however open to change the URL mapping of faces servlet from `*.jsf` to `*.xhtml`. This way the enduser won't be able to see the raw Facelets source code when purposefully changing the extension in the URL. Mapping on the same extension as physical file is not possible with JSP. – BalusC Feb 06 '13 at 13:03
  • The only problem is. When I change the extension and run the file it works only once. Any more changes do not become visibale. I clearly neeed to change something else? – KennyBartMan Feb 06 '13 at 14:08
  • That can happen if you're using MyFaces and the `javax.faces.PROJECT_STAGE` is not set to `Development`. MyFaces caches fairly agressively. If you don't have patience to wait some time for the cache to be flushed, then you'd basically need to restart the whole server to see changes in Facelets files. – BalusC Feb 06 '13 at 14:10