I'm using JSF to make a website for a project, and I use subdirectories to manage what users are able to access which pages.
My WAR project is organized as follows:
+Web Pages
+User
movies.xhtml
+WEB-INF
//beans, faces-config, glassfish-web, and web.xml here
+images
+resources
+css
header.css
+templates
header.xhtml
index.xhtml
//other views
My navigation rules are as follows
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>movies</from-outcome>
<to-view-id>/User/movies.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-view-id>*</from-view-id>
<from-outcome>index</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
<from-outcome>create</from-outcome>
<to-view-id>/create.xhtml</to-view-id>
<from-outcome>help</from-outcome>
<to-view-id>/help.xhtml</to-view-id>
<from-outcome>login</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
<from-outcome>search</from-outcome>
<to-view-id>/search.xhtml</to-view-id>
</navigation-case>
My web.xml servlet-mapping is as follows:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/vd/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>vd/index.xhtml</welcome-file>
</welcome-file-list>
Now, I have no problem with the navigation rule that brings me to /User/movies.xhtml. However, once I am in /User/movies.xhtml, JSF cannot find any of my pages (IE. index.xhtml). However, it can still find the template that movies.xhtml uses.
Why can't JSF find where other views are? How should I change my navigation rules so that it can find them?
I'm running on Glassfish v3.1.2.
I figured out what the problem is. I stopped using navigation cases and uses JSF 2's implicit navigation and everything worked as it should.
Related
This question already has answers here:
Can we use regular expressions in web.xml URL patterns?
(6 answers)
Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
(1 answer)
Closed 4 years ago.
I use JSF, Primefaces and websphere.
I made a web project in maven with two maven modules.
But when i start the websphere server and the url appears and i click on it, does the webbrowser show that he doesn't find the page.
Is there something wrong in my web.xml?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Tinnenhoek-BackOffice</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<enabled>true</enabled>
<async-supported>false</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>
/faces/*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
The page that should be seen is main.xhtml
This question already has an answer here:
Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
(1 answer)
Closed 6 years ago.
I'm so new for JavaServer Faces. I'm trying to create a project in Netbeans. (New Project >Java Web>Web Application). While creating I changed JSF Servlet URL Pattern.
It was like this:
And I changed it as "*.jsf" then created. Netbeans edited web.xml file.
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
</web-app>
But my index page is still xHTML.
Lastly, when I run to file, IDE trying to open index.html and browser can't find index.HTML so I go index.jsf and page opens.
I've been searching for a while, I just find about people suggests editing to web.xml but its already edited. So do you have any suggestion?
Then you can use below servlet-mapping
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
you can use more than one pattern as well please check below
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
For more and detail information you can check BalusC's evergreen answers for following questions.
What is the difference between creating .xhtml or .jsp .or .jsf for JSF pages
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
JSF 2 with HTML pages instead of XHTML
For your question why its created index.html rather than index.xhtml you can check following link
Creating a new JSF project
it will tell you step by step process to create a JSF project and at last it creating index.xhtml page rather than index.html. So from this link you can get which step you missed in your case.
Please check below two screens
1. Step 1
2. Step 2
You can check highlighted(Yellow Color) text its extension is .XHTML and not .HTML
Ive been reading some posts about javax.faces.default_suffix but without success when trying to implement it.
Using : jsf 2.0, jboss 7.1, Mojarra 2.1.5
I need to show in URL the following : localhost:8080/myproject/index.jsf
when navigating also need show the xxx.jsf
web.xml
<welcome-file-list>
<welcome-file>/comum/inicio/index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern> **have tried *.jsf but with no success**
</servlet-mapping>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsf</param-value>
</context-param>
Would you help me on this issue please ? thanks
You're mixing the meaning of the default suffix and the URL pattern.
The javax.faces.DEFAULT_SUFFIX represents the default suffix of the physical file you've in your webapplication which represents a JSF file. This defaults in JSF 2.0 to .xhtml. If you change it to .jsf, then you should rename all physical files from some.xhtml to some.jsf. This makes generally no utter sense. You should not do that, just get rid of that context param altogether.
The <url-pattern> represents the default URL pattern which the enduser has to use in request URL in order to invoke the FacesServlet (which in turn uses the default suffix configuration to locate the physical file based on the URL). You said that you want to use *.jsf in URLs, however you have set it to *.xhtml. This is not right and changing the default suffix is not the right solution.
You should just set the URL pattern alone, not the default suffix.
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
This way http://localhost:8080/myproject/index.jsf will work.
Then there's a third problem: you're completely misunderstanding the purpose of the welcome file. It should not represent the path to the homepage. It should represent the filename of the physical file which you'd like to serve up as default file when a folder like /, /foo/, /foo/bar/, etc is requested. Just set it to index.jsf.
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
However, you should keep in mind that the container will verify the existence of the physical file before continuing the request, so that it can properly show a 404 error if absent. As *.jsf is actually a virtual URL, that step will fail. You can solve that by fooling the container by placing a physically existing but empty index.jsf file next to the index.xhtml file in the desired folder.
This way http://localhost:8080/myproject/ will work, provided that you have a real index.xhtml file and empty index.jsf file in the root folder.
Much easier is to just get rid of virtual URLs and stick to *.xhtml all the time.
See also:
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
JSF welcome file is not recognized
richfaces + index.xhtml having errors
I am trying to set up environment for Java/JSF app in my windows 7 machine. I am using eclipse juno with Jboss AS 7 downloaded from the eclipse market. Also I am using JDK 1.7.
I created a very simple app containing just a h:outputLabel tag. Everything looks fine until deployed and run, but the tag doesn't render. What I mean to say is that I am getting a blank page.
The jars I included for JSF are : jsf-api-2.1, jsf-impl-2.1.0-b03.jar,jsf-facelets-1.1.14.jar.
And the usual commons and jstl jars.
There are no errors or any exceptions. Am I missing anything here? Please pardon me if its too simple but this is just taking too much time.
EDIT:
My auto-created faces-config xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
My web xml:
<?xml version="1.0"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>NewFile.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Thanks in advance.
The jars I included for JSF are : jsf-api-2.1, jsf-impl-2.1.0-b03.jar,jsf-facelets-1.1.14.jar. And the usual commons and jstl jars.
There are quite a lot of mistakes right here. First of all, you don't need and even should not include the JSF jars. Those are already part of Java EE, which is implemented by JBoss AS 7.
Secondly, you definitely don't need and absolutely should not use the separate Facelets jar in combination with JSF 2.x. Facelets is already part of JSF 2.x.
You also should not include the JSTL jar. That one too is provided by Java EE/Jboss AS. If with commons jars you mean Apache commons, then those are fine but they are NOT needed for JSF. Include them only if you want to use them directly in your application code.
If you would need any of those jars (you don't, but suppose), it's also best practice to take the latest ones if you're just starting. From the version numbers it kinda looks like you just took a random old version. But again, you don't need any of those jars. They are provided by Java EE/JBoss AS 7.
My auto-created faces-config xml:
You don't need an empty faces-config.xml. If you're just starting, it might be better to remove everything you don't need. If there's later something you need to configure, you can always add it.
My web xml:
For JSF you don't really need to map the FacesServlet to the extensions you used. Those are already the default. If you leave out the entire web.xml, your Facelets (.xhtml) page can be requested by changing the .xhtml extension to .jsf or .faces. E.g. if your page is 'NewFile.xhtml' you can request it using localhost:8080/NewFile.jsf or 'localhost:8080/NewFile.faces` or 'localhost:8080/faces/NewFile.xhtml'.
Unfortunately, the only (IMHO) useful mapping is NOT provided as a default by JSF 2.1 and for that one you do need to add a mapping entry in web.xml:
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Because your welcome page is NewFile.xhtml, you need either this *.xhtml mapping, OR you can remove the mapping entirely and change the welcome-file content to e.g. NewFile.jsf.
Update:
If the welcome page still doesn't show, there must be something else in your project that you either don't know about yourself or aren't showing us.
Try to start over with a very simple project and see if it works there:
In Eclipse, create a new Dynamic Web Project
Use project name: welcome and Target runtime: JBoss 7.1 Runtime
Delete WebContent/META-INF and WebContent/WEB-INF/lib
Copy the 3 files from http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java-ee-app-without-any.html to your project. Afterwards your workspace should look exactly like the one in the picture.
Add a WebContent/WEB-INF/web.xml with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>page.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Double check you have exactly 4 files in your entire 'welcome' project, not more and not less.
Deploy your project to JBoss AS 7.1. To be sure, right click on the runtime server in the Servers view and click on "Clean..." Start your server and request localhost:8080/welcome or localhost:8080/welcome/.
I just tested this locally using the exact same steps I outlined, and it worked.
i start with the programming of a JSF Website. At the moment all files have the .xhtml ending. When i go to http://localhost:8080/myProject/start.jsf everything is all right. But when i rename the file from start.xhtml to start.jsf i became a NoClassDefFound Error.
What is my mistake?
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
You have to change the javax.faces.DEFAULT_SUFFIX parameter (in web.xml)
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsf</param-value>
</context-param>
However, this is not advisable - either use .xhtml or .jsp for your files.
Note that you can use .jsp with facelets with no problems (if, for example the auto-complete of your IDE doesn't work for .xhtml).
Also note that:
the faces servlet mapping determines how the jsf pages are referred to from http perspective
the DEFAULT_SUFFIX parameter indicates what's the extension of the files.
Why do you want to rename the file start.jsf? The correct extension of the JSF files are .xhtml (but you can modify this default extension, as stated by Bozho).
In fact, to be precise, this extension is defined by Facelets (or JSF 2.0, as it natively integrate Facelets), which is different if you use "basic" JSP files.
It's best to stay with .xhtml because that's the right way to do it, but you can configure it with the javax.faces.DEFAULT_SUFFIX context-param in web.xml.