What is an alternative of a4j:JsFunction in JSF 2.0? - jsf

I am new to JSF 2.0.
My current project is build on JSF 1.3 and Richfaces 3.3.
We are trying to migrate to JSF 2.0.
There is limitation to not use Richface 3.3 or 4.0 in Current Project or any third party tool/library. Our goal is to only use JSF 2.0 functionality/feature.
In my project there is lot of use of
<a4j:jsFunction data="action" oncomplete="JavaScriptFunction(data); >
<a4j:actionaram name="nm" assignTo="#beanName.methodName"> </a4j:actionparam>
</a4j:jsFunction>
I want to remove this type of a4j:jsFunction with h:commandButton or
any jsf 2.0 standard mechanism.
I am not able to find out best way to call Bean function which return some value and assign this return value to JavaScript function.

JSF 1.3? I thought 1.2 was the latest?
On to your question. The idea to only use JSF 2.x is invalid and you should have the decision maker reconsider because skimping on OmniFaces will impede your productivity and the general code quality.
The way I see it you have three choices:
OmniFaces
Click button with display:none using javascript. Said button can have f:ajax as a child
Rip o:commandScript http://showcase.omnifaces.org/components/commandScript it's open source.
I can fully understand that a company today says no to Primefaces or Richfaces and honestly I would recommend it for many applications. Omnifaces however is to be seen as a proper for JSF.

Related

role attribute of h:panelGrid

I'm using exaples from the official Java EE tutorial In which contains the follow:
<h:panelGrid columns="2"
headerClass="list-header"
styleClass="list-background"
rowClasses="list-row-even, list-row-odd"
summary="#{bundle.CustomerInfo}"
title="#{bundle.Checkout}"
role="presentation">
But compiler says that attribute role is not defined for h:panelGrid component. How to fix this?
That attribute was introduced in JSF 2.2. As evidence, the role attribute is mentioned in JSF 2.2 <h:panelGrid> documentation, but not in JSF 2.1 <h:panelGrid> documentation.
Your question history confirms that you're using JSF 2.2 on GlassFish 4.0, so this compiler warning is actually wrong. This is not exactly a JSF problem, but an IDE problem. The IDE is somehow thinking that you're not using JSF 2.2, but JSF 2.1 or older. I.e. your toolset is working against you. You didn't mention which IDE you're using, so it's not possible to post the right answer.
If the project runs fine and the JSF page produces the right HTML output (i.e. the role attribute actually ends up in generated HTML <table> element as you can see by rightclick, View Source in webbrowser), then everything is well and it's just the IDE who's pretending to be smarter than it actually is.
I'd start peeking around in IDE project's properties to check if the JSF versions are all right. The JSF facet in project's properties must be set to version 2.2, not lower. The faces-config.xml must be declared conform JSF 2.2, not lower.

Migrating JSF 1.1 with Ajax4jsf 1.x to JSF 2

We are migrating JSF 1.1 (MyFaces) project to JSF 2. The idea is to migrate periodically by keeping both JSP and XHTML together for some time. We use many ajax4jsf-1.1.1 tags in JSP pages. We don't use RichFaces. After configuring the system to JSF 2 (with all config changes mentioned in tutorial by Balusc) When tried to access the JSP page with ajax4jsf.jar in classpath, we get an exception:
Caused by: java.lang.IllegalStateException: setViewHandler may not be executed after a lifecycle request has been completed
at org.apache.myfaces.application.ApplicationImpl.setViewHandler(ApplicationImpl.java:853)
at org.ajax4jsf.framework.ajax.InitPhaseListener.beforePhase(InitPhaseListener.java:92)
at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersBefore(PhaseListenerManager.java:76)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:131)
It looks ajax4jsf.jar is not compatible with JSF 2. Looks some issue with LifeCycle configuration.
Is there any way we can make a4j work with JSF 2 JSPs? I know when we use XHTML we don't need all this.
Get rid of Ajax4jsf 1.x altogether. It's indeed not compatible with JSF2. Instead, JSF2 offers a new main ajax tag <f:ajax> which covers all the core functionality as previously offered by Ajax4jsf 1.x.
If upgrading to RichFaces 4 is not an option (because, as you said yourself, you aren't using RichFaces components anywhere), then just remove Ajax4jsf 1.x and replace all <a4j:xxx> tags by standard JSF2 equivalents.
<a4j:ajaxListener>: use <f:ajax listener>.
<a4j:keepAlive>: just put managed bean in the view scope by #ViewScoped.
<a4j:log>: use jsf.ajax.addOnEvent() or jsf.ajax.addOnError() in JS context.
<a4j:commandLink>: just nest <f:ajax> inside <h:commandLink>.
<a4j:outputPanel>: use <h:panelGroup> and remember to include its ID in <f:ajax render> or PrimeFaces <p:outputPanel>.
<a4j:repeat>: just use standard <ui:repeat>.
<a4j:form>: just use <h:form>, it will autorecognize <f:ajax>.
<a4j:htmlCommandLink>: just nest <f:ajax> inside <h:commandLink>.
<a4j:jsFunction>: just use standard <h:commandScript>. It was however introduced late in JSF 2.3. If you can't upgrade to JSF 2.3 then consider OmniFaces <o:commandScript> or PrimeFaces <p:remoteCommand>.
<a4j:region>: just use <f:ajax execute>, you can even wrap <f:ajax> around a group of components.
<a4j:loadBundle>: just use standard <f:loadBundle>.
<a4j:status>: use jsf.ajax.addOnEvent() or jsf.ajax.addOnError() in JS context.
<a4j:actionparam>: just use standard <f:param>.
<a4j:loadScript>: just use standard <h:outputScript>.
<a4j:mediaOutput>: no replacement. Consider PrimeFaces <p:media>.
<a4j:poll>: no replacement. Consider OmniFaces <o:commandScript> or PrimeFaces <p:poll>.
<a4j:commandButton>: just nest <f:ajax> inside <h:commandButton>.
<a4j:include>: just use standard <ui:include>.
<a4j:loadStyle>: just use standard <h:outputStylesheet>.
<a4j:support>: just use standard <f:ajax>.
You also need to rename/rewrite JSP files to Facelets files. In simple cases, this is usually just a matter of changing root declarations and file extensions. Facelets makes it easier to replace all duplicated code by a single template. The following answer applies:
Migrating from JSF 1.2 to JSF 2.0

Iterating over a list in JSF 1.2

Is there any way of iterating over a list in JSF 1.2 without using any third party components? I know I can do it using Tomahawk. I also know that it can be done using JSTL, but I am keeping that as my last resort. Also I cannot use <ui:repeat> since we are using JSF 1.2. Is there any elegant way like <ui:repeat> to do it in jsf 1.2?
The only JSF 1.2 component which can iterate over a List is the <h:dataTable>.
In JSP, the only other "standard" (i.e. not "3rd party") tag which can iterate over a List is the JSTL <c:forEach>. Using JSTL shouldn't harm that much if the List which you'd like to iterate over is already available during view build time. You'll only run into trouble when it's only available during view render time, for example because it's been nested in a <h:dataTable> and should be iterating over a property of table's var. This just won't work due to reasons also mentioned in JSTL in JSF2 Facelets... makes sense?
There are no other ways without using a 3rd party library such as Tomahawk's <t:dataList>, unless you're open to reinventing the wheel by creating a custom UIComponent yourself. This is however not a trivial job.
It's however possible to integrate Facelets 1.x in JSF 1.2. A guide is described in the Facelets 1.x docbook. This is only going to be quite some of work if you already have an existing JSF application using JSP as view technology; you'd need to convert JSP to Facelets. But it'll in end make the upgrade path to JSF 2.x so much easier. See also a.o. Migrating from JSF 1.2 to JSF 2.0 and Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?

<button> with JSF 1.2

Simple question:
Is there any way of rendering a html <button>-element using JSF or any other framework (RichFaces, Tomahawk etc.)? Or would I have resort to writing a custom component for this?
No, there isn't. Since a <button> is usually only used in GET requests, you can also just put it plain vanilla in the JSF template. You don't need to bind any action to a JSF managed bean anyway.
In JSF 2.0, there's by the way the <h:button> which renders a GET button and offers the option to include view parameters and/or to perform implicit navigation. Both features aren't available in JSF 1.2, so there's not really a point of having similar component in JSF 1.2 anyway.
On the other hand, if you actually intend to use a <button> to invoke a POST managed bean action method, then you should really be using a <h:commandButton> instead. If you're having a specific problem with it for which you thought that using <button> was the solution, then you'd need to reframe your question to elaborate in more detail about that specific problem so that we can answer how to achieve the same with <h:commandButton>.

RichFaces and Facelets, a good combo?

I've been programming using RichFaces and JSF for quite some time now, and like the features that Facelets offer (especially as part of JSF 2), but haven't used it yet. Are there any gotchas to watch out for or incompatibilities between RichFaces and Facelets? I use A4J ajax functionality a lot as part of RichFaces, so I am concerned about that also.
Thanks in advance.
Keep in mind that Facelets is just a view layer for JSF 2. JSF2 and RichFaces have some incompatibilities. Check this to see a list of them:
http://community.jboss.org/wiki/RichFaces333andJSF20
RichFaces used with standalone Facelets (I currently use 1.1.14) works great.
The RichFaces demo application uses Facelets, so I'd even say it's recommended.
I'd go a bit further and say that not using Facelets with JSF is crazy.
It's such a handy framework. No wonder they used many concepts from it for JSF2.
We use them together without any problem. I have not encountered any gotchas.
No worry about this combination, it's definitely the way to go with JSF !
I'm used configuration: richfaces4 + facelets + netbeans 6.9, and the only problem detected betwen facelets and rich is a two(2) click for ajax or include page dynamic

Resources