How to upload files in JSF 1.1? - jsf

I want to upload files in a JSF 1.1 project. JSF 1.1 doesn't support RichFaces file upload. I looked at Tomahawk, but I don't know how to use Tomahawk. Can anybody explain for me?
Which JARs do I have to use?
And taglibs?
And web.xml configuration?
And faces-config.xml configuration?
Or are there alternatives to Tomahawk?

Which JARs do I have to use?
The following ones:
tomahawk
commons-fileupload
commons-io
commons-logging
commons-el
I assume that you already have the JSF 1.1 JARs jsf-api and jsf-impl.
And taglibs?
Just the Tomahawk one, next to the two usual core/html tags:
<%# taglib uri="http://myfaces.apache.org/tomahawk" prefix="t" %>
And web.xml configuration?
You need the ExtensionsFilter. This filter will take care that JSF gets the right parameters out of a multipart/form-data request body.
<filter>
<filter-name>Extensions Filter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Extensions Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
And faces-config.xml configuration?
Nothing special. Just create a managed bean the usual way with a UploadedFile property which you bind to the value attribute of <t:inputFileUpload>.
See also:
How to upload files in JSF? (yes, it's targeted on JSF 1.2, but should work equally good in JSF 1.1).

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.

tomahawk panelNavigation2 does not triggered actionlistener after upgrading to JSF2

I have migrated my web application from JSF 1.2 to JSF 2.1.
In my web.xml I have set the the following context-param to true in order to use bundle jars instead of using the provided wildfly jars.
<context-param>
<param-name>org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL</param-name>
<param-value>true</param-value>
</context-param>
I have placed the following jars into the WEB-INF/libs folder:
tomahawk21-1.1.14, jsf-api-2.1.0.jar and jsf-impl-2.1.0.jar
I can start the application und log myself in. However once I click on a MenuItem built as follows:
<h:form id="nav" styleClass="nav">
<t:panelNavigation2 id="panel-nav" expandAll="true">
<t:navigationMenuItems value="#{hello.menuItems}" />
</t:panelNavigation2>
</h:form>
nothing happens. I have programmatically set actionListener for the MenuItem. It did work fine when using JSF1.2.
Using remote debugging with eclipse clearly proves that the action listener method is not called.
Best regards
Edmond

Why uploading file with tomahawk is not working?

I have an Icefaces 3.x + JSF 2.1 app where I tried to integrate upload file solution from here but I receive
14:31:24,506 WARN
[org.apache.myfaces.custom.fileupload.HtmlFileUploadRenderer]
(http-localhost-127.0.0.1-8080-3) t:inputFileUpload requires
ExtensionsFilter or TomahawkFacesContextFactory configured to handle
multipart file upload, and any of them has been detected. Please read
the instructions on
http://myfaces.apache.org/tomahawk/extensionsFilter.html for more
information about how to setup your environment correctly. Please be
sure ExtensionsFilter is the top most filter if you are using multiple
jsf libraries. 14:31:24,507 WARN
[org.apache.myfaces.custom.fileupload.HtmlFileUploadRenderer]
(http-localhost-127.0.0.1-8080-3) t:inputFileUpload requires the
current request be intercepted by the filter in order to handle forms
with multipart encode type. ExtensionsFilter was initialized but the
current request was not intercepted. Please add a filter-mapping entry
to intercept jsf page requests.
which is pretty strange because I added:
<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>
into my web.xml but still the uploaded file is null.
Any clue to what I do wrong?

LazyInitializationException in jsf project

I am facing the LazyInitializationException in my jsf project.
and I also know that fetch = FetchType.EAGER is not the right way to solve my problem.
I got some idea from this post http://blog.gmorales.net/2012/03/how-to-solve-orghibernatelazyinitializa.html that there is a solution using OpenSessionInViewFilter:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilterr</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
so in other word, I have to implement the Spring framework for my project?
I want to know is there a way that JSF provided to solve this problem?

Forwarding a request from a JSF managed bean to a Struts 2 action does not work

Within a JSF managed bean, I have the following code taken from this past question:
String uri = "/myAction";
FacesContext.getCurrentInstance().getExternalContext().dispatch(uri);
myAction is the name of a Struts action that is defined in my struts.xml.
However when I access the JSF bean, I am not forwarded to /myAction. Instead I'm getting a 404 http error: The requested resource (/MyApp/myAction) is not available.
Why doesn't it work??
When I try to access /myAction directly from a browser by going to http://localhost:8080/MyApp/myAction all works fine.
The clue is in the javadoc for the dispatchmethod:
"Parameters:
path - Context relative path to the specified resource, which must start with a slash ("/") character"
i.e it's dispatching to a url relative to your application context,not your webserver root folder
What you need is the redirect method which redirects to an absolute url.
Set the Struts 2 Filter in web.xml to accept forwards (and direct requests (or redirects)):
In your web.xml, change:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
To:
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<!-- uncomment if you plan to include struts action output -->
<!--
<dispatcher>INCLUDE</dispatcher>
-->
</filter-mapping>
Credit:
http://www.coderanch.com/t/59584/Struts/request-dispatcher-struts-action
http://docs.oracle.com/cd/B32110_01/web.1013/b28959/filters.htm#BCFIEDGB

Resources