We are using JSF 2.1.10 & mojarra and I don't see we will upgrade to 2.2 or 2.1.18 any time soon because of some other dependencies.
Now, I want to use ViewScope for my managedBean as i have some ajax functionality. If i use requestScope, it keep on creating new objects everytime I call ajax function. And I know that if I use ViewScoped then I need to make sure all the objects in the bean should implement Serializable, if not it throws NotSerializableException. And I cannot do that because in my bean i need to use some objects where I cannot change them to implement Serializable. Since I am using mojarra I cannot use this as a solution.
Is there anything in mojarra to say false to serialization?
Related
I'm in the process of migrating from jsf1.1 to jsf2.0. When testing one of the jsf page that I've converted, it encountered the java.io.NotSerializableException on one of the class. I'm not getting this error when it was under jsf1.1. To resolve the problem, I added the Serializable interface to the class. After I did that I get the same error on a different class. I know I can simply add Serializable interface to this class to resolve the issue. But is this normal when migrating from jsf1.1 to jsf 2.0?
You're only getting this error now because in JSF 1.x, partial state saving wasn't a requirement. For a primer on JSF state saving, See this question.
From the JSF 2.x Spec:
For Applications versioned at 1.2 and under, the runtime must not use the partial state saving mechanism. For applications versioned at 2.0 and above, the runtime must use the partial state saving mechanism
This stipulation is what forces any and all view components to be serializable
Technically, anything stored in the session should be serializeable. In JSF, the view and flash scopes are stored within the session. So in short anything not in the request scope will end up in the session. However, you don't see the error until things are serialized out of the session. I don't know the details, but there msut be a difference in JSF 1.1 and JSF 2.0 implementations were JSF 2.0 is more aggressive at serializing the session.
Is there any JAva Server Faces framework implementation, that doesn't produce nor uses any JavaScript inside generated output?
No JavaScript nor AJAX, pure HTML with optional static resources like CSS.
If possible, also without using cookies." By framework.
You can just use the standard JSF implementation. The <h:commandLink>, <h:button> and <f:ajax> are the only components which generate and require JavaScript. If you just don't use them, then you're already set. As alternatives you could use <h:commandButton> or <h:link> respectively and then throw in some CSS to make it look like a link or button respectively.
As to cookies, this is only created when JSF makes (in)directly use of the HttpSession. Just toggle to client side state saving by setting javax.faces.STATE_SAVING_METHOD to client, or make JSF stateless by <f:view transient="true">. Also and don't use #SessionScoped managed beans. #ViewScoped is already not possible in stateless JSF — although "useless" is a better term — if you make JSF stateless). Stateless JSF only requires a minimum of Mojarra version 2.1.19, older versions didn't support it. You also need to keep in mind that stateless JSF is more sensitive to forgery attacks, but that's not JSF's fault.
See also:
When should I use h:outputLink instead of h:commandLink?
Difference between h:button and h:commandButton
Why JSF saves the state of UI components on server?
I see everybody using MyFaces CODI or SeamFaces ViewScope Services saying that CDI don't have #ViewScope annotation
If the ViewScope serve just to retrieve the bean after each ajax call back, I think CDI provides extension to add your own implementation and fires AfterBeanDiscovery event which will be benefit to retrieve owr bean from ViewRoot Map.
See this topic http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/ for more explanation.
So can anyone tell me what's the difference between this implementation and MyFaces CODI or SeamFaces ViewScope ?
Thanx in advance.
The Seam Faces view scope is a little different, in that it behaves just like the JSF view scope but allows CDI injection as well. Also, these are prebuilt solutions vs needing to build your own solution.
I have a few questions on the various options and best practices when using JSF with EJB3.1. The mental model I have, given the daunting amount of choices and combinations available, is far from clear so some questions may not make sense.
JSF/Facelets reference backing beans (I am using the term "backing bean" for beans whose properties are written or read from Facelets pages) through EL code that is agnostic as to the actual annotations used in the bean classes (javax.faces.bean.* or javax.enterprise.context.*).
Is it correct to say that one can toggle between JSF and CDI scope annotations just by changing the imports in the bean classes without any changes to the Facelets xhtml code?
Is it an established pattern that JSF/Facelets should be used only for the xhtml markup code with all scope and lifecycle (plus injection) annotations done using CDI?
In a JBoss AS setting, where is the lifecycle management of the JSF backing beans (using either JSF or CDI annotations) taking place? In the web container or in the EJB3 container?
In a typical web application given that the SessionScoped beans can be provided by CDI, is there any need for using EJB3 beans other than those of type #Entity, e.g. for the last typical step in each "flow" when information is to be persisted in the database?
Is it correct to say that one can toggle between JSF and CDI scope annotations just by changing the imports in the bean classes without any changes to the Facelets xhtml code?
Yes.
Is it an established pattern that JSF/Facelets should be used only for the xhtml markup code with all scope and lifecycle (plus injection) annotations done using CDI?
JSF is moving towards CDI. The new #FlowScoped annotation of the upcoming JSF 2.2 is evidence of this as this extends from the CDI API. The only disadvantage is that CDI doesn't offer a standard annotation for the tremendously useful JSF #ViewScoped annotation. You'd need #ConversationScoped wherein you manually start and end the conversation, or take a look at a CDI extension like MyFaces CODI.
In a JBoss AS setting, where is the lifecycle management of the JSF backing beans (using either JSF or CDI annotations) taking place? In the web container or in the EJB3 container?
The web container (in flavor of a WAR). JSF is built on top of the Servlet API, so it's definitely the web container.
In a typical web application given that the SessionScoped beans can be provided by CDI, is there any need for using EJB3 beans other than those of type #Entity, e.g. for the last typical step in each "flow" when information is to be persisted in the database?
The #Entity is part of JPA, not of EJB. The #Entity is to be used on a model class which is mapped to a database table and usually solely meant to transfer data across the layers. What you're last describing sounds like candidate for a #Stateful EJB. To understand #Stateless vs #Stateful EJBs better, head to this detailed answer: JSF request scoped bean keeps recreating new Stateful session beans on every request?
I want to use the #ViewScoped - scope in my application for the backing beans of some web pages. Also I use CDI to inject the dependecies into the backing beans.
However, when I use a backing bean annotated like this
#ManagedBean
#ViewScoped
#Inject
someDependency (...)
the #Inject annotation will not inject anything and i get a NullPointerException as soon as i am accessing the dependency.
However, when I decorate the backing bean with
#Named
#ViewScoped
#Inject
someDependency (...)
the injection works fine, but now the #ViewScoped is ignored as it is not part of CDI / Weld.
How can I use #ViewScoped together with CDI Weld?
The problem is that you are mixing simple managed beans with CDI managed beans and they don't work together. Managed Beans is a simple framework for defining beans and their injected beans. CDI is a separate beast with all sorts of extra goodness.
However, Managed beans can't use CDI Injection points but can use the ViewScope while CDI beans use CDI injection points and all that good stuff but the ViewScope isn't available.
To resolve the issue you have to either go with CDI and use the Seam-Faces library to use view scope, or drop CDI and stick with simple managed beans which is a simple implementation.
Cheers,
Andy
You can get #javax.faces.bean.ViewScoped to work by including the Seam Faces 3.1.0 jar in your project.
Failing that (i.e. you're using GlassFish 3.1.1 or earlier), you can simply copy ViewContextExtension.java, ViewScopedContext.java and javax.enterprise.inject.spi.Extension from Seam Faces 3.1.0 into your own project, ensuring you use the same path to the files as Seam Faces does. The java files can be copied verbatim. All lines except the one ending with ViewContextExtension should be removed from javax.enterprise.spi.Extension.
I am using the latter method successfully in GlassFish 3.1.1 and will try the former method one GlassFish 3.1.2 is released.
No, it's not directly supported. Seam3 is supposed to provide such extras that CDI does not. Check it out.
You can implement the #NormalScope to create your own CDI Scope witout using any other framework or waiting for new JEE7
CDI fires an event AfterBeanDiscovery after each bean call
You can use CDI extension to #Observes this event and add you context implementation
In your scope implementation you can :
Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
Use CreationalContext if the bean name from first step is not found to create it in the FacesContext ViewRoot Map
for a more in-depth explanation i recommand this link : http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/
I don't use Seam, just normal JSF + PrimeFaces. I just found this and I am going to give it a try... you might want to as well.
Weld in combination with Seam-Faces would provide it but it's broken. An interesting thread about it and an alternative for it is e.g. at http://forum.primefaces.org/viewtopic.php?f=3&t=7585
I think Apache CODI or Seam 3 solves this. There is a new project called DeltaSpike that might be doing this, think it continues Seam 3.
In Java EE 7, this problem will be solved as I understand that all beans are CDI beans, so there is no JSF beans.