EL in JSF-maven project not working - jsf

i'm trying to get my JSF web application to run on JBoss. Til now everthing worked fine, but i can't call methods of my backing bean.
Backing bean:
#Named
#ConversationScoped
public class WelcomePM implements Serializable {
public String getHelloStatement() {
return "Backing bean works!";
}
}
When i tried to invoke the method in a xhtml-page, nothing happened.
Page:
<?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://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:outputText value="JSF works!"></h:outputText>
<h:outputText value="#{welcomePM.getHelloStatement()}" />
</h:body>
</html>
The browser only shows the string 'JSF works!' but not the string 'Backing bean works!' as i expected. So i tried to exchange #{welcomePM.getHelloStatement()} with something the server can't resolve like #{fooPM.getBar()} expecting to get an exception. But there was no exception at all (neither in the browser nor in the server logs). So i believe, that the server doesn't even try to resolve the EL-expression. What am i doing wrong?

Have you tried #{welcomePM.helloStatement} ?

Related

jsf transient view creates #ViewScope error

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?

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

Primefaces dialog from bean only shows once

I'm trying to show a dialog from a bean as described in this PrimeFaces ShowCase. The thing is everything works as expected and dialog shows up, BUT if I close the dialog and then press the button again, the dialog won't show up unless the page is refreshed.
This is not the behavior shown in the example where every time the button is pressed the dialog shows up.
The only difference I have in my code is I have used CDI alternatives instead of managed beans package, because javax.faces.bean package will be deprecated. I mean:
javax.inject.Named instead of javax.faces.bean.ManagedBean
javax.faces.view.ViewScoped instead of javax.faces.bean.ViewScoped
In any case I've also tried with managed bean package but still the same wrong behavior.
This is what I have so far:
index.xhtml
<?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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:commandButton value="Open" actionListener="#{viewDialogMB.viewDialog()}"/>
</h:form>
</h:body>
</html>
ViewDialogMB.java
import java.util.HashMap;
import java.util.Map;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
#Named(value = "viewDialogMB")
#ViewScoped
public class ViewDialogMB {
public void viewDialog() {
Map<String,Object> options = new HashMap<>();
options.put("modal", true);
options.put("resizable", true);
RequestContext.getCurrentInstance().openDialog("dialog", options, null);
}
}
dialog.xhtml
<?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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Dialog Title</title>
</h:head>
<h:body>
<p:outputLabel value="Hello from Dialog!" />
</h:body>
</html>
faces-config.xml (as per Dialog Framework documentation)
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<action-listener>
org.primefaces.application.DialogActionListener
</action-listener>
<navigation-handler>
org.primefaces.application.DialogNavigationHandler
</navigation-handler>
<view-handler>
org.primefaces.application.DialogViewHandler
</view-handler>
</application>
</faces-config>
By the way my platform (if makes any difference) is:
Glassfish 4
JSF 2.2
JDK 1.7 - 64 bits
Java EE 7
PrimeFaces 5.0 (community version)
I have tried with Mozilla Firefox, Google Chrome and MS IE11.
This bug was reported here Dialog Framework regression bug in PF 4.0.10 and higher and Issue 6915: PF 5.0 error when closing DF window opened from DataTable
As a workaround solution, use taconic's solution which is to add a panel inside the body.
Your dialog.xhtml would look 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:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Dialog Title</title>
</h:head>
<h:body>
<p:panel>
<p:outputLabel value="Hello from Dialog!" />
</p:panel>
</h:body>
</html>
Try using < p:dialog> they are really handy. Or you can do as "dic19" said and disable ajax as follows
<p:commandButton ajax="false" value="Open" actionListener="#{viewDialogMB.viewDialog()}"/>

How to invoke Java code on press of the submit button

I have a working JSF application that allows a user to enter their name, press the Submit button, and they are presented with a page that welcomes them.
The structure is very simple, there is a Person bean that has a setter and getter for a name, and the textfield sets the name, and the welcome page gets the name from the bean to present it on the welcome page.
My question is can I invoke java methods from XHTML when the user presses the Submit button, because I need to open a connection to the database. The code for the first page of the application looks like this in the xhtml file:
<?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://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
<h:inputText value="#{helloBean.name}"></h:inputText>
<h:commandButton value="Welcome Me" action="welcome"></h:commandButton>
</h:form>
</h:body>
</html>
So it looks like when they hit Welcome Me, the welcome.xhtml file is called up and the bean is passed to it. But I also need to execute some code to open the database when the Welcome Me button is pressed, how can I do this?
For that, the action attribute of the command button should be bound to the action method of the bean.
<h:commandButton value="Welcome Me" action="#{helloBean.submit}" />
In the method you've all the freedom to write down Java code which should be executed when the button is pressed.
public String submit() {
// ...
return "welcome";
}
The returned string will be used as navigation outcome. If you return null or void, it will return to the same view.
See also:
Our JSF wiki page

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