javax.faces.DEFAULT_SUFFIX not working - jsf

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

Related

Arabic is showing as ???? ???? in console and in MySQL [duplicate]

I am helping to one of my friend. He is creating web-application using JSF 2.0 & mysql.
While creating database he have used below query.
CREATE DATABASE dbName DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
Till date website is working fine. Today client tried entering Arabic text and they said that the output is coming weird. What my friend do is after entering the data to DB, he also prints the same data on another page saying Congratulations, XYZ ABC is added successfully. However he see output as Congratulations, Ù?ظاÙ? تÙ?Ù?Ù?Ø© Ù?تÙ?Ù?Ù? صدÙ?Ù? Ù?Ù?بÙ?ئة is added successfully. I don't understand why he get like that when Database characters are set properly.
web.xml content is as below.
<?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">
<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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
600
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>restrict</filter-name>
<filter-class>com.sac.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>restrict</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>DisplayImage</servlet-name>
<servlet-class>com.sac.databean.DisplayImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayImage</servlet-name>
<url-pattern>/DisplayImage</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SaveMyImage</servlet-name>
<servlet-class>com.sac.databean.SaveMyImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SaveMyImage</servlet-name>
<url-pattern>/SaveMyImage</url-pattern>
</servlet-mapping>
<!-- for not using css and js of default richfaces -->
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>plain</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.LoadStyleStrategy</param-name>
<param-value>None</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.enableControlSkinning</param-name>
<param-value>false</param-value>
</context-param>
<!-- for not using css and js of default richfaces -->
</web-app>
On each .xhtml page, he have <?xml version='1.0' encoding='UTF-8' ?>
Please let me know if you need anything else.
Edit 1
In my JSF filter, I also added req.setCharacterEncoding("UTF-8"); in doFilter(). Still in Database I see ???????????
Edit 2
In JSF page I have <h:inputText value="#{PersonalInformationDataBean.fullName}"> and when I print the fullName value in Java bean as System.out.println("my name while entering is " + fullName);m I get output as my name while entering is ???????????? ????.
This means there is problem while entering data
Can someone help what is going ODD?
However he see output as Congratulations, Ù?ظاÙ? تÙ?Ù?Ù?Ø© Ù?تÙ?Ù?Ù? صدÙ?Ù? Ù?Ù?بÙ?ئة is added successfully. I don't understand why he get like that when Database characters are set properly.
This is known as Mojibake. This is not a DB encoding problem, but a HTTP encoding problem. Setting the POST request character encoding as you did is indeed the proper solution.
Edit 1: In my JSF filter, I also added req.setCharacterEncoding("UTF-8"); in doFilter(). Still in Database I see ???????????
Question marks occur when the both sides of the connection are aware of their own encoding. Sent/retrieved characters which are not covered by the encoding of one side will be replaced by question marks. Arabic characters doesn't occur in ISO-8859-1 and hence they're replaced by question marks. That's the difference with Mojibake whereby characters are been sent without checking if the encoding used by the other side really supports the character. You'll end up incorrectly encoded characters which presents itself as an unintelligible sequence of characters.
In this particular case, the JDBC driver is by itself aware that it's using ISO-8859-1 by default to transmit the characters to DB, while the retrieved characters are in UTF-8 (the MySQL JDBC driver doesn't look at the DB table encoding, even though it's properly been set to UTF-8 in your case). You need to explicitly tell the JDBC driver to use UTF-8 to decode characters before transmitting data to DB. This is to be done as JDBC connection properties which are definied as query string parameters in the JDBC URL like so:
jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
If you're using a container-managed datasource, then just specify those properties separately the same way as you did for the username and password
useUnicode=yes
characterEncoding=UTF-8
See also:
Unicode - How to get the characters right?

IDE creates xhtml instead of .jsf as extension [duplicate]

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

Use custom URL extension with JSF [duplicate]

This question already has an answer here:
Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
(1 answer)
Closed 8 years ago.
I want to use a custom URL extension for my facelets. Like, let's say, .asdf instead of .xhtml or .jsf.
I added the mapping to my web.xml:
<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>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.asdf</url-pattern>
</servlet-mapping>
The index.asdf is located in WebContent/facelets/common/index.asdf but if I try to access it with the browser (http://localhost:8080/Project/facelets/common/index.asdf)I get the following error message:
HTTP Status 404 - /Project/facelets/common/index.jsp
type Status report
message /Project/facelets/common/index.jsp
description The requested resource is not available.
Apache Tomcat/7.0.39
Note that it says .jsp even though I haven't used the .jsp extension anywhere. Any pointers what's wrong there?
Ok, it looks like I misunderstood some of the basics here. I renamed my files on disk to index.asdf, which I shouldn't have. They should keep the .xhtml extension and only the extension I access them with is the new one. Now everything works as expected. If you want to restrict access to these raw .xhtml files use this https://stackoverflow.com/a/5675913/989919

why don't my jsf tags work? [duplicate]

This question already has an answer here:
JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output
(1 answer)
Closed 5 years ago.
In Eclipse JUNO, I started:
new Dynamic Web Project
Name: JSFTest; Configuration: JavaServer Faces v2.0 Project
JSF Capabilites:
Copy jars to WEB-INF/lib (jstl-api, jstl-impl, jsf-api, jsf-impl):
new HTML to WebContent: index.xhtml with "New Facelet Composition Page", and the code inside:
in web.xml, I write the index.xhtml into the welcome part
when I run it on my apache tomcat 7 server, the result (don't bother about the h1 title):
So why doesn't it show the outtext and the button? What did I wrong? I saw a lot of video on youtube, I followed them, in the video it worked, but at me.
Because you have to access to the index.xhtml page under /faces/ virtual folder as stated in your web.xml file for the FacesServlet url mapping.
A simple way to make your JSF page to work is to change the url mapping in your web.xml
From:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
To
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
If you don't want to change this, you must change your welcome-file to /faces/index.xhtml.
IMO it will be better the first option, in this way the FacesServlet will only process the xhtml pages, using the /faces/* will make it process any other resource as images, CSS and JS files.

JSF works only with the .xhtml ending

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.

Resources