I have some problems with the default URL that jsf shows:
The url is displayed like this:
www.URL.com/PROYECT_NAME/
And I want something like this
www.URL.com/PROYECT_NAME/home
I sent up the welcome file like this.
<welcome-file-list>
<welcome-file >faces/views/home.xhtml</welcome-file>
</welcome-file-list>
So what I really want is that when jsf shows the welcome file show and url like this www.URL.com/PROYECT_NAME/home or the complete route faces/views/home.xhtml.
I know is a dumb question but Im stock in it
It is possible to achieve that using a filter-based servlet extension like PrettyFaces.
It is simple to use, has good documentation and examples, but to illustrate your case you could do something like this:
Download prettyfaces.jar and add to your classpath. Usually /WEB-INF/lib folder.
Add a pretty-config.xml file containing the URL mappings to the /WEB-INF folder.
Example of pretty-config.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.3 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd">
<url-mapping id="home">
<pattern value="/home" />
<view-id value="/home.xhtml" />
</url-mapping>
</pretty-config>
To redirect to this mapping from a controller you should use a string like pretty: + url-mapping-id.
Example of controller bean:
#ManagedBean
#ViewScoped
public class HomeBean
{
public String goHome()
{
return "pretty:home";
}
}
That's it. Whenever you fire a request, if PrettyFaces filter finds the url mapping pattern /home it will display the view id home.xhtml but keep the URL as /home. Pretty.
Also, as a suggestion, for your welcome-file-list you could add only index.html.
Example of web.xml welcome-file-list tag:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
And add an index.html file like this to your application root folder.
Example of index.html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Application</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=/myapplication/home" />
</head>
<body>
<h3>Loading...</h3>
</body>
</html>
By doing this, whenever someone requests your application it will get a fast loading page and will be redirected to /home.
I hope it helps.
Related
How can I make JSF send http header
Content-Type: application/xhtml+xml;charset=UTF-8 instead of current
Content-Type: text/html;charset=UTF-8?
Adding following snippet in web.xml had no effect.
<mime-mapping>
<extension>xhtml</extension>
<mime-type>application/xhtml+xml</mime-type>
</mime-mapping>
My sample file webapp/sample.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:outputLabel value="Hello, world"/>
</f:view>
</html>
Environment:
JSF 2.2, WildFly 8.2
You can set it in <f:view> as below:
<f:view contentType="application/xhtml+xml">
However, this is the wrong value for HTML. JSF/XHTML generates HTML output which should really have text/html content type. Explanation can be found in the answer to When to use f:view and f:subview, particularly in the "See also" links in that answer.
I have a Primefaces+JSF web site whose home page is
localhost:81/Mywebapp/login.jsf
I want to redirect the user to that home page if he goes to: localhost:81/Mywebapp/ so he doesnt need to write the complete URL.
How can I do that? I suppose it is some configuration in JBoss
Any help? thanks
Actually it should be in your web.xmlfile, try to put to it's end this entry
<welcome-file-list>
<welcome-file>/login.jsf</welcome-file>
</welcome-file-list>
EDIT
I also solved this once by making a new index.jsp like this
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:forward page="/login.jsf" />
</body>
</html>
Or put there a html file with this meta
<meta http-equiv="Refresh" content= "0; URL=login.jsf"/>
and set one of them as your welcome file. One of these solutions should do the job for you:-)
Sorry for the second answer, the formatting in the comments doesn't really cut it:
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
...
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
...
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>subdir/*</url-pattern>
</servlet-mapping>
Peter's answer should work just fine, but if you're really concerned about the extensions (it doesn't really matter) then just add the places you want mapped under the servlet. This maps *.xhtml, *.jsf and anything under 'subdir/'.
If you had a hard file:
- index.xhtml
Myapp/index.xhtml and
Myapp/index.jsf
would map to the same file. xhtml is since your default suffix is xhtml
I hope this helps, honestly, with Primefaces 2 I'd definitely be using xhtml as that's the spec. With 3 it doesn't matter as much, but I still stay with xhtml as it isn't something I'd normally have on my server.
Best of luck (Petr's forwarding trick isn't a bad idea--it can really help in tough situations)
You can always do this with URL rewriting engines like PrettyFaces. You have to activate it in your web.xml and then define rewriting rules like this:
<url-mapping id="login">
<pattern value="/login" />
<view-id value="/legacy/user/login.xhtml" />
</url-mapping>
Because you mentioned explicitly login.jsf I think it's a good idea to think about including a security framework which is doing this (and many other things) for you. Seam security provides typesafe annotations for this:
#ViewConfig
public interface SeamPages
{
static enum Pages0
{
#ViewPattern("/jsf/admin/important.xhtml")
#UrlMapping(pattern="/admin/important")
#LoginView("/jsf/access/loginRequired.xhtml")
#AccessDeniedView("/jsf/access/accessDenied.xhtml")
#RestrictAtPhase(PhaseIdType.RESTORE_VIEW)
#LoggedIn
ENUM0;
}
}
This will solve three common requirements:
The URL /yourApp/admin/important will be redirected to the JSF page /jsf/admin/important.xhtml.
If the user is not #LoggedIn he will be redirect to /jsf/access/loginRequired.xhtml
IF the user does not have the rights to access the page he will be redirected to /jsf/access/accessDenied.xhtml
I'm writing a simple custom function in Facelets with a sample method. The problem is that the JSF 2 application fails to locate that function. The error message is:
/test.xhtml #15,73 rendered="#{test:isGranted('ONE_ROLE')}" Function 'test:isGranted' not found.
I've been checking and rechecking and can't find the problem. Any comment here would be really appreciated as it's clear that I'm missing something (but it seems that the steps involved are really simple).
Do you know if there are other requisites?
Thanks in advance.
The relevant code:
In the web.xml the tag XML descriptor is declared
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/test.taglib.xml</param-value>
</context-param>
The file test.taglib.xml:
<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://www.test.com/security/tags</namespace>
<function>
<function-name>isGranted</function-name>
<function-class>com.test.security.taglibs.IsGranted</function-class>
<function-signature>boolean isGranted(java.lang.String role)</function-signature>
</function>
</facelet-taglib>
The tag class:
public class IsGranted extends TagHandler {
public static boolean isGranted(String role) {
// Do nothing. Just a test.
return false;
}
}
And the test file:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:test="http://www.test.com/security/tags">
<body>
<h:outputText value="You should NOT see this." rendered="#{test:isGranted('ONE_ROLE')}"/>
</body>
</html>
In your example you are declaring the sec namespace prefix but use the test prefix in your function call. But maybe that was just a copying mistake.
Another possible cause would be the header of your taglib file, which uses the facelets 1.0 DTD instead of the JSF 2.0 version. This might be problematic depending on your JSF implementation, for example for MyFaces see this bug report and discussion thread. The header for a JSF 2.0 taglib would be:
<facelet-taglib version="2.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-facelettaglibrary_2_0.xsd">
I'm trying with Javaee6 with GlassFish3.1.
I created a web project (with JSF2.0), with only one file, one.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
<title>ONE</title>
</h:head>
<h:body>
<h:form id="oneForm">
<h:commandButton id="oneButton" value="To Two" action="two" />
</h:form>
</h:body>
</html>
I've not added web.xml, as it is optional for javaee6.
I added faces-config.xml, so that default servlet mapping works. (with only top element)
<faces-config version="2.0" .../>
When I hit command-button on the page, I'm expecting 'Page Not Found' error. But the page just reloads. If I actually put file two.xhtml, it works correctly, but in it's absence, does not give error, which I'm expecting.
Am I missing something, some config?
Thanks in advance.
This is expected. JSF works with so-called navigation cases. If it doesn't find a matching case, it will just postback to the same view. This is specified in the JSF 2.0 specification (pick the one for evaluation).
7.4.2 Default NavigationHandler Algorithm
...
The default NavigationHandler implementation must behave as if it were performing the following algorithm (although optimized implementation techniques may be utilized):
If no navigation case is matched by a call to the handleNavigation() method, this is an indication that the current view should be redisplayed. As of JSF 2.0, a null outcome does not unconditionally cause all navigation rules to be
skipped.
...
However, when you set the JSF project stage to Development by a context parameter in web.xml,
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
then clicking the button should then result in a development warning message in the postback,
Unable to find matching navigation case with from-view-id '/one.xhtml' for action 'two' with outcome 'two'
so that eventual developer mistakes are at least put in attention.
I'm trying to create a custom composite component taglib in my office but i get a strange issue with EL. It seems expressions as #{cc.attrs.[var] } are already resolve as empty.
I try to create my taglib in a jar. In my jar i have my files ordered as following:
|_ /
.....|_ META-INF
..........|_ compo.taglib.xml
..........|_ resources
...............|_ components
....................|_ hello.xhtml
compo.taglib.xml contains:
<facelet-taglib 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-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://www.example.com/jsf/compo</namespace>
<composite-library-name>compo</composite-library-name>
<tag>
<tag-name>hello</tag-name>
<source>./components/hello.xhtml</source>
</tag>
</facelet-taglib>
hello.xhtml contains:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface name="hello" displayName="hello">
<composite:attribute name="name" required="true" type="String"/>
</composite:interface>
<composite:implementation >
hello #{cc.attrs.name}!
</composite:implementation>
</html>
My web project contains in WEB-INF lib my taglib as a jar, jsf-impl.jar and jsf-api.jar (from Mojarra) my page is simply that:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:comp="http://www.example.com/jsf/compo" >
<body>
<comp:hello name="John"></comp:hello>
</body>
</html>
At rendering i see "hello !" but not "hello John!". Attributes values seems be lost somewhere. I try this sample on tomcat 6.0.29 and Websphere 7.
I made something wrong?
Have you tested your control in web application rather than from taglib (jar file)?
I can only guess but I think your attribute is not showing because you named it name. In some cases "name" attribute is beeing used by JSF (for example in f:attribute or f:param or even ui:param uses attribute name). Try to replace attribute name with oder word.
You need to look here and check if attribute name is available.
Yes this case appears also with other attributes.
I tried with the component in web application and issue doesn't appear.
I also tried with the component in a jar with default namespace: issue doesn't appear. I think there is bug when using composite component in a jar with custom namespace.