JSF variable substitution with binding f:loadBundle - jsf

I am trying to create a simple JSF application...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%#taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%# page import="java.util.*"%>
<html>
<head>
<title>Login</title>
<f:loadBundle var="Message" basename="bundle.Messages" />
</head>
<body>
<f:view>
<h:form id="loginForm">
<h:message for="loginForm" />
<br />
<h:outputText value="#{Message.username_label}" ></h:outputText>
</h:form>
</f:view>
</body>
</html>
However, when I try to run the page in my browser, I get the value #{Message.username_label}. Could someone please help me to understand why the value was not substituted into the page?

So, EL doesn't get evaluated? This can happen when the web.xml is not properly declared conform at least Servlet 2.4 (for JSF 1.0/1.1) or 2.5 (for JSF 1.2) and/or your classpath is polluted with old versioned servletcontainer specific libraries.
Since you're using legacy JSP instead of its successor Facelets, I'll bet that you're using JSF 1.2 on a Servlet 2.5 container (such as Tomcat 6.0). In this case, you need to ensure that the web.xml is declared as follows:
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5"
>
<!-- Your config here -->
</web-app>
In addition, you also need to ensure that you don't have any servletcontainer specific libraries like servlet-api.jar, j2ee.jar, javaee.jar, jsp-api.jar, etc.. in your webapp's /WEB-INF/lib folder or, worse, in the JRE/lib/ext folder. Get rid of them, they are supposed to be supplied by the servletcontainer itself, not your webapp. The /WEB-INF/lib folder should contain only the JSF libraries and other libraries specific to the webapp itself.

It is obvious to me. is not a complied staememnt in java therfore the foundation of core cant encrypt it. the id is not being compiled because you have it in these <> do you know java?

Related

Upgrading JSF 1.2 to 2.x NullPointerException [duplicate]

I am working with a rather large app written in JSF 1.2.
JSF 1.2 is around 6 years old now. I need to upgrade to JSF 2.0. How painful will this be? I noticed that some attributes in custom tags have been changed etc.
Painfulness
Painfulness of upgrading JSF 1.2 to 2.0 depends on the view technology which you are currently using and which you want to use.
JSP 2.x to JSP 2.x = Almost no effort.
Facelets 1.x to Facelets 2.0 = Little effort.
JSP 2.x to Facelets 2.0 = Lot of effort. Double this if you also have custom components.
Basic changes
Regardless of the view technology switch, at least the following steps should be done:
Remove JSF 1.2 JAR's from /WEB-INF/lib (if any).
Drop JSF 2.0 JAR's in /WEB-INF/lib (if JSF 1.2 was servletcontainer-supplied, you might want to change the classloading policy to load webapp libraries first before servletcontainer libraries, see also JSF2 classloading issues in application servers).
Update root declaration of faces-config.xml to comply JSF 2.0 spec.
<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">
Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XML snippet.
Ensure that root declaration of web.xml already complies at least Servlet 2.5. JSF 2.0 won't work on 2.4 or lower (although it's hackable).
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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_2_5.xsd"
id="YourWebappID"
version="2.5">
Note: when you're using Servlet 3.0 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XML snippet.
JSP 2.x to JSP 2.x
If you're using JSP 2.x and want to keep using it, then you basically don't need to change anything else.
Gradually upgrading
If you're already using a suffix url-pattern for the FacesServlet, like *.jsf, then it's good to know that the FacesServlet will first scan for *.xhtml file and if it is not present, then scan for *.jsp file. This provides you room to gradually convert from JSP to Facelets behind the scenes without changing the URL's.
But if you're using a prefix url-pattern, like /faces/* and you want to gradually upgrade from JSP to Facelets, then you really have to change it to *.jsf and possibly also all links in the existing JSP pages.
You only need to keep in mind that the new JSF 2.0 provided implicit navigation doesn't scan for the presence of the file, it will go to outcome.xhtml anyway. So if you want to come from or go to *.jsp, then you still need to include it in the viewid the JSF 1.x way.
Facelets 1.x to Facelets 2.0
If you're using Facelets 1.x as view technology and want to use the JSF 2.0 supplied Facelets 2.0, then you need to do the following additional steps:
Remove Facelets 1.x JAR from /WEB-INF/lib.
Remove Facelets 1.x FaceletViewHandler from faces-config.xml.
Any custom FaceletViewHandler implementation needs to be updated to extend ViewHandlerWrapper instead.
Not necessary, but just for cleanup, remove any Facelets 1.x related <context-param> values from web.xml which are already default in Facelets 2.0, like the javax.faces.DEFAULT_SUFFIX with value of *.xhtml.
Update root declaration of existing Facelet taglib XML's to comply Facelets 2.0.
<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">
Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XML snippet.
That should basically be it.
JSP 2.x to Facelets 2.0
If you're using JSP 2.x as view technology and you want to upgrade to Facelets 2.0 immediately, then you need to do a lot of changes before the site can go live. You're basically changing the view technology here.
Master page changes
On every master page, you need to change the following basic JSP template..
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%#taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html>
<f:view>
<html lang="en">
<head>
<title>JSP page</title>
</head>
<body>
<h:outputText value="JSF components here." />
</body>
</html>
</f:view>
..to the following basic Facelets template:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>XHTML page</title>
</h:head>
<h:body>
<h:outputText value="JSF components here." />
</h:body>
</html>
Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XHTML snippets.
Include page changes
If your existing JSP pages are well designed, you should not have any line of scriptlet code and you should also have only the <jsp:include> as the sole JSP-specific tag. Any of those needs to be changed from:
<jsp:include page="include.jsp" />
to
<ui:include src="include.xhtml" />
The basic JSP include page template of..
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%#taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<f:subview id="include">
<h:outputText value="JSF components here." />
</f:subview>
..should be changed to the following basic Facelets include page template:
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:outputText value="JSF components here." />
</ui:composition>
Note: when you're using JSF 2.2 or newer, use the http://xmlns.jcp.org namespace domain instead of http://java.sun.com throughout the above XHTML snippets.
Custom component changes
You need to change the JSP TLD files to Facelets TLD files as described in this Mojarra Migration Guide.
Aftermath
Regardless of the migration approach, you can gradually eliminate the faces-config.xml by the new JSF 2.0 annotations or even CDI. Any <managed-bean> can be annotated by #ManagedBean:
#ManagedBean(name="managedBeanName")
#RequestScoped
public class SomeBean {}
Next to #RequestScoped, there are also #ViewScoped, #SessionScoped and #ApplicationScoped available. If you omit the name attribute of the #ManagedBean, then it will default to classname with the 1st char lowercased.
#ManagedBean
#RequestScoped
public class SomeBean {}
In this particular example, it will be #{someBean}.
Any <managed-property> can be annotated using #ManagedProperty:
#ManagedProperty("#{otherBean}")
private OtherBean otherBean;
Any <validator> can be annotated using #FacesValidator:
#FacesValidator("someValidator")
public class SomeValidator implements Validator {}
Any <converter> can be annotated using #FacesConverter
#FacesConverter("someConverter")
public class SomeConverter implements Converter {}
Any <renderer> can be annotated using #FacesRenderer
#FacesRenderer(componentFamily="someComponentFamily", rendererType="someRendererType")
public class SomeRenderer extends Renderer {}
Any <navigation-case> which uses the filename of the XHTML page as both <from-outcome> and <to-view-id> can be removed since this will be implicitly done. This can be gradually done by changing all outcome values to match the filename of the target view.
Finally, any session scoped bean which was been put in the session with the sole reason to retain the bean data in subsequent requests in the same tab/window can better be marked #ViewScoped, because this way the bean won't be affected when the enduser opens the same page in different tabs/windows.
Component libraries
Note that I don't take any 3rd party componant libraries like PrimeFaces/RichFaces/IceFaces into account in this answer, it would then be impossible to write a reliable answer since it basically boils down to "it depends". In general it's sufficient to just upgrade the component library to a -by themselves verified- JSF 2.0 compatible version as per their instructions. Best is to just write unit tests, run them before and after the upgrade and fix any issues individually.
Here are at least some useful links with regard to migration of the specific component library:
RichFaces Migration Guide - 3.3.x to 4.x migration
IceFaces 2 Wiki - IceFaces 1.x Compatibility Guide
PrimeFaces has no migration guide for PrimeFaces 1.x to 2.x as PrimeFaces 1.x requires Facelets 1.x already, so you just have to follow Facelets 1.x to 2.x migration steps. However, there's a PrimeFaces 2.x to 3.x (and higher) migration guide which might apply as well on migrating from PrimeFaces 1.x to 3.x (or higher). Tomahawk has also no migration guide. Basically the only which you need to change are the JARs and if necessary get rid of all <t:saveState> references on a request scoped bean by making the bean view scoped.
One thing to mention is that if anyone is using JSTL with JSF 1.2 then when upgrading to JSF2 you should change the namespace from:
http://java.sun.com/jstl/core
to:
http://java.sun.com/jsp/jstl/core
JSF 2.0 have many new features and components and I don't feel migration will be painful. Only area you will find difficult is in using thrid party libraries. If your application is heavily dependant upon libraries like Richfaces then you will face problem. Not all the components from Richfaces 3 is ported to Richfaces 4.
This also might help
JSF 1.2 application migration to JSF 2.0
Also check this What is new in JSF 2?
Web.xml
Add the jars
1. jsf-api-2.0.jar
2. jsf-impl.2.0.2.jar
Step 1: Change web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<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>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Step 2: webmvc-config.xml
<!-- Handles requests mapped to the Spring Web Flow system -->
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor" />
<property name="ajaxHandler">
<bean class="org.springframework.faces.webflow.JsfAjaxHandler" />
</property>
</bean>
Step3:facess-config.xml
<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">
If you are using Apache Trinidad you'll also have to upgrade it to version 2.0 so that it will support JSF 2.0. There's more info at Hacker's Valhalla.

Cannot use a JSP custom tag library in a facelets page

I have jsp custom tag library which works fine in web projects using jsp pages. I want to use the custom library in JSF facelets based web project.
Please let know if that's possible.
Registration.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:api="http://www.my.com/taglibs/api">
<h:head>
</h:head>
<h:body>
<h2>Registration</h2> <br/>
<h:form id="myForm">
<table>
<api:function attribute="/value"/>
The custom tag (<api:function>) appears in the output html without processing. I am using jsf 2.2.8

Set HTTP headers properly to force caching on JS, CSS and PNG files

How can I tell to GlassFish server, to store all JS, CSS and PNG files into browser cache in order to reduce HTTP GET requests?
I am using JSF and PrimeFaces.
Just make use of JSF builtin resource handler. I.e. use <h:outputStylesheet name>, <h:outputScript name> and <h:graphicImage name> with files in /resources folder instead of "plain vanilla" <link rel="stylesheet">, <script> and <img>.
<h:outputStylesheet name="css/style.css" />
<h:outputScript name="js/script.js" />
<h:graphicImage name="images/logo.png" />
This way you don't need to worry about resource caching at all. JSF builtin resource handler has already set the necessary response headers. The expiration time defaults already to 1 week.
In Mojarra you can control the expiration time by the following context parameter (the value is in millis):
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
And in MyFaces:
<context-param>
<param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
See also:
How to reference CSS / JS / image resource in Facelets template?
What is the JSF resource library for and how should it be used?

PrimeFaces CSS skin not showing in login page, also JavaScript undefined errors

I am using PrimeFaces 3.4 in my web app and for a particular page the controls are not displayed with the normal PrimeFaces skin:
<!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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>VMS login</title>
</h:head>
<h:body>
  <h:form id="loginForm">
    <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
    <p:panel header="#{msgs['login.title']}">
      <p:panelGrid id="loginPanel" columns="2">
        <h:outputText value="#{msgs['login.username']}" />
        <p:inputText id="j_username" value="#{loginFormBean.userName}" required="true"></p:inputText>
        <p:message for="j_username" ></p:message>
        <h:outputText value="#{msgs['login.password']}" />
        <p:password id="j_password" value="#{loginFormBean.password}" required="true" feedback="false"></p:password>
        <p:message for="j_password"></p:message>
        <p:commandButton action="#{loginController.loginUsingSpringAuthenticationManager}" value="#{msgs['login.button']}" update="loginForm" ajax="true"></p:commandButton>
      </p:panelGrid>
    </p:panel>
  </h:form>
</h:body>
</html>
This outputs to:
The panel should have a header and so on.
The interesting thing is that in another page where I am using a <p:layout> with different panels in the layouts they display fine with their normal PrimeFaces look-and-feel.
What am I doing wrong? Thank you
Given that it only occurs on the login page, that can happen when the authentication mechanism also kicks on requests to JSF resources like CSS/JS/image files and redirects them to the login page as well. The webbrowser would then retrieve the HTML representation of the login page instead of the concrete resources. If you have investigated the HTTP traffic in the webbrowser's developer toolset, then you should have noticed that as well.
If you're using homegrown authentication with a servlet filter, then you need to tell the filter to not redirect them to the login page, but just continue them. It are those /javax.faces.resource/* URLs (you can get that URL path as constant by ResourceHandler#RESOURCE_IDENTIFIER).
if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
chain.doFilter(request, response);
return;
}
// ...
Or if you're using container managed authentication, then you should add /javax.faces.resource/* to allowed URLs which should be skipped from login checks:
<security-constraint>
<web-resource-collection>
<web-resource-name>Allowed resources</web-resource-name>
<url-pattern>/javax.faces.resource/*</url-pattern>
</web-resource-collection>
<!-- No Auth Contraint! -->
</security-constraint>
See also Exclude css & image resources in web.xml Security Constraint.
Or when you're using 3rd party authentication framework like Spring Security, then you need to tell it the following way (assuming 3.1.0 or newer)
<http security="none" pattern="/javax.faces.resource/**" />
See also Spring Security 3.0.5.
Or when you're using PicketLink, see PrimeFaces based application with PicketLink does not show style in login page.

JSF 1.2 migration to Facelets

My all application is written with jsf 1.2
I want to use from this moment on facelets where xhtml files are .
So I have some main questions:
1.I want to
face one:upgrade to jsf 1.2 with servlets
face two:from there to upgrade to 2.0
would this two changes break my application ?
2.how to do this ? can some one explain to me ? I have been trying all day long to do it i had :
changed a file to xhtml amd changed his taglibs to xmlns such as :
<%# taglib prefix="a4j" uri="http://richfaces.org/a4j" %> will be converted to : xmlns:a4j="http://richfaces.org/a4j"
adding
<view-handler>
com.sun.facelets.FaceletViewHandler
</view-handler>
inside application in the faces config
changing file name from xxx.jsp to xxx.xhtml
adding
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
to web xml
inserting
f:view tag above all
the problem i am facing now
when *.jsf page is called the server says no page like this exists in the system...
when *.xhtml is called the page itself is blank and the server asks me to download it meaning a pop up with open and save options popes.

Resources