jsf transient view creates #ViewScope error - jsf

I am running into a problem where my stateless views generate the following error
#ViewScoped beans are not supported on stateless views
I set up a little test and found the below very simplified version of my page with no backing bean will generate the error.
<?xml version='1.0' encoding='UTF-8' ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<f:view transient="true">
<h:body>
test
</h:body>
</f:view>
</ui:composition>
If I remove the,
<h:body>
Tags the error will not be generated. I'm guessing that this has something to do with the servlet JSF is automatically generating in the background, but after a ton of Googling, I can't seem to find a solution.
How can I use a stateless view with standard JSF?

Related

Composite component causes warning "no 'head' component has been defined within the view"

I have a working JSF page, but if I add xmlns:leaf="http://java.sun.com/jsf/composite/jsf2leaf" to it, and try to use any ot its tags, like <leaf:map center="42.120000,-72.540000" />, I get the following warning:
One or more resources have the target of 'head', but no 'head' component has been defined within the view.
Everything works fine, the map is there, I can add markers, but I can't get rid of the warning message.
My JSF page looks like this:
<?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:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui" xml:lang="hu" lang="hu"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:leaf="http://java.sun.com/jsf/composite/jsf2leaf"
>
<f:view contentType="text/html">
<f:metadata>
<f:viewAction action="#{dashboardController.readURLParam()}"/>
</f:metadata>
<h:head/>
<h:body>
...
<h:form>
...
<p:panel>
...
<c:forEach>
...
<p:panel>
<leaf:map center="42.120000,-72.540000" />
</p:panel>
</c:forEach>
</p:panel>
</h:form>
</h:body>
</f:view>
found the problem, it, in the jsf2leaf.jar the map.xhtml and advancedmap.xhtml uses <head></head> instead of <h:head></h:head>, changed it, repackaged the jar file, and it works fine

Value expressions still evaluated despite ui:fragment rendered="false"

I have bean:
class Property{
private String type;
private Date value;
//getters and setters
}
also have block of code on page:
<ui:fragment rendered="#{property.type eq 'checkbox'}">
<ui:include src="checkbox.xhtml">
<ui:param name="property" value="#{property}"/>
</ui:include>
</ui:fragment>
checkbox.xhtml:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<body>
<ui:composition>
<h:selectBooleanCheckbox value="#{property.value}"/>
</ui:composition>
</body>
</html>
The condition #{property.type eq 'checkbox'} = false
But I get next exception:
javax.servlet.ServletException: checkBox.xhtml value="#{property.value}": Cannot convert 01.11.02 0:00 of type class java.util.Date to class java.lang.Boolean
I expect if the attribute rendered=false in ui:include, then this block will not be processed.
<ui:fragment rendered> prevents it from rendering the HTML output, but it doesn't prevent it from ending up in JSF component tree and being eligible for state saving.
Use <c:if test> instead. It runs during view build time instead of view render time and thus the whole bunch won't end up in JSF component tree at all.
Or, if you have this all inside an <ui:repeat var="property">, and you are using Mojarra, then upgrade to at least 2.1.29 or 2.2.7 wherein this state saving bug was fixed.
See also:
JSTL in JSF2 Facelets... makes sense?
PropertyNotFoundException on conditionally rendered subclasses in ui:repeat

RichFaces a4j:commandButton oncomplete

Hi i have this demo form with RF 4.5.2, the oncomplete execute 3 times, i need a way to execute a method after the ajax call but only one time, i think that is the behaviour of the oncomplete but if it doesnt you can suggest any other ideas to accomplish this:
<!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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head></h:head>
<body>
<rich:panel>
<h:form>
<a4j:commandButton id="btnAccion" value="enviar" oncomplete="#{archivoAfipController.emptyAction()}"/>
</h:form>
</rich:panel>
</body>
</html>
You're supposed to use the action attribute for the purpose of invoking a managed bean method on submit.
<a4j:commandButton ... action="#{archivoAfipController.emptyAction}" />
The oncomplete must represent a client side script expression (thus, e.g. JavaScript, which is in JSF perspective basically a plain vanilla String!), which should be executed when the ajax action has completed, the response has returned, and all necessary parts of the view have been updated in the client side.
See also:
<a4j:commandButton> tag documentation
https://stackoverflow.com/a/20146879/ (only second half of the answer is relevant)

JSF 1.2 : Can I create reusable component inside JSF view

Is possible to something like this in jsf?
<ui:composition>
<x:reusableCode id="editScreen">InnerHtml ... </x:reusableCode>
code...
<x:use component="editScreen"/>
</ui:composition
I know I can create my own component and register it in jsf tagLib, but I need reusable HTML only in on jsf view file.
In Facelets 1.x you can create a tag file for this purpose.
Here's a basic kickoff example. Create /WEB-INF/tags/some.xhtml:
<ui:composition
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="#{foo}" />
</ui:composition>
Define it in /WEB-INF/my.taglib.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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://example.com/jsf/facelets</namespace>
<tag>
<tag-name>some</tag-name>
<source>/WEB-INF/tags/some.xhtml</source>
</tag>
</facelet-taglib>
Register it in /WEB-INF/web.xml:
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>
(note, when you have multiple, use semicolon ; to separate them)
Finally just declare it in your main page templates.
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:my="http://example.com/jsf/facelets"
>
<my:some foo="value1" />
<my:some foo="value2" />
<my:some foo="value3" />
</ui:composition>
A more advanced example can be found here: How to make a grid of JSF composite component? Note: JSF 2.0 targeted, but with minor changes based on above example it works as good on Facelets 1.x.

Sending UTF-8 string to managed bean does not work

<?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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets" >
<head>
</head>
<h:form>
<h:body>
<h:inputText value="#{editorBean.value2}" />
<h:commandButton action="content.xhtml" value="Submit" />
</h:body>
</h:form>
</html>
value 2 is a String in a sessionscoped ManageBean
using JSF Mojarra 2.1.19
Glassfish 3.1.2
When I enter äüö and submit the form, it appears as äöü in stdout.
But if I use ajax, then this does not happen.
How is this caused and how can I solve it?
JSF/Facelets uses by itself already by default UTF-8 throughout the process.
You only need to tell Glassfish that the request parameters are encoded using UTF-8, so that it will properly decode it using UTF-8. Open the /WEB-INF/glassfish-web.xml file and add the following entry to the <glassfish-web-app>:
<parameter-encoding default-charset="UTF-8" />
By the way, your <h:form> has to go inside the <h:body>, not outside. Also, you should be using <h:head> instead of <head> in order to get JSF to auto-include the proper Ajax scripts.

Resources