JSF AJAX - Class doesn't have property - java

I have been trying to run AJAX example in JSF. But I am getting "class does not have the property login". But in all the examples in various websites, code is same the same.
My index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF AJAX Calls</title>
</h:head>
<h:body>
<h2>AJAX Example</h2>
<h:form>
<h:inputText id="inputName" value="#{userData.name}"></h:inputText>
<h:commandButton value="Login">
<f:ajax execute="inputName" render="outputMsg" />
</h:commandButton>
<br />
<hr />
<h2><h:outputText id="outputMsg" value="#{userData.login}" /></h2>
</h:form>
</h:body>
</html>
UserData.java
package com.cyb3rh4wk.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "userData", eager = true)
#SessionScoped
public class UserData implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String login() {
if ("".equals(name) || name == null)
return "";
else
return "Logged in as " + name;
}
}
This is my error,
/index.xhtml value="#{userData.login}": The class 'com.cyb3rh4wk.test.UserData' does not have the property 'login'.
How do I resolve this error ?

Value and Method Expressions
The EL defines two kinds of expressions: value expressions and method expressions. Value expressions can either yield a value or set a value. Method expressions reference methods that can be invoked and can return a value.
Example of a value expression according to the above definition in your code is:
userData.name
In the following tag definition:
<h:outputText id="outputMsg" value="#{userData.login}" />
you are not using a value expression but rather a method expression because login is not a simple JavaBean getter returning the value of a bean property.
So you have to change the above line as:
<h:outputText id="outputMsg" value="Logged in as : #{userData.name}" />
And remove your login or use it for navigation purpose (that is why it is there).
Here is the reason (taken from JSF 2.0 specification) why you have to pass a value expression to an output component instead of a method expression:
4.1.10 UIOutput
UIOutput (extends UIComponentBase; implements ValueHolder) is a component that has a value, optionally retrieved from a model tier bean via a value expression (see Section 5.1 “Value Expressions”), that is displayed to the
user. The user cannot directly modify the rendered value; it is for display purposes only:

Related

FacesComponent with no set called

Here's my FacesComponent class:
#FacesComponent("ExibicaoChecklistComponent")
public class ExibicaoChecklistComponent extends UINamingContainer {
private ListaChecklistWrapper checklist;
private String altura;
public ListaChecklistWrapper getChecklist() {
return checklist;
}
public void setChecklist(ListaChecklistWrapper checklist) {
this.checklist = checklist;
}
public String getAltura() {
return altura;
}
public void setAltura(String altura) {
this.altura = altura;
}
}
and the xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:pretty="http://ocpsoft.com/prettyfaces"
xmlns:ui="http://java.sun.com/jsf/facelets">
<cc:interface componentType="ExibicaoChecklistComponent">
<cc:attribute name="altura" default="300px" type="java.lang.String" />
<cc:attribute name="checklist" required="true" />
</cc:interface>
<cc:implementation>
Altura: #{cc.altura}
Checklist: #{cc.checklist.nome}
</cc:implementation>
and finally the usage:
<checando:exibicaoChecklist altura="200px" checklist="#{CheckBean.checklists[0]}" />
The setAltura method is called with the 200px value, but the setChecklist(ListaChecklistWrapper checklist) is not called and the checklist attribute is always null inside the component.
If I do #{CheckBean.checklists[0].nome} outside the <checando:exibi... tag it works. So, the object is not null... it's only a missing call to the set method.
Is there anything I'm missing?
Mojarra 2.1.13 (20120907-1514) and java version "1.7.0_25".
Thanks.
I guess, giving the nome directly as a parameter into the component does work again? (Something like setChecklistNome(String nome).
If so, there might be a challenge with giving direct parameters different from java.lang.String. Have you tried to give the parameters as cc.attrs.checklist instead from writing it directly into the UINamingContainer?

a4j:ajax listener exception MethodNotFoundException

I started study RichFaces 4.2.2 and have a problem in simple example, I have an xml:
<ui:define name="content">
<h:form>
<rich:panel style="width: 50%">
<h:panelGrid columns="2">
<h:outputText value="Name:"/>
<h:inputText id="inp" value="#{echoBean.name}">
<a4j:ajax event="keyup" render="echo count" listener="#{echoBean.countListener}"/>
</h:inputText>
<h:outputText value="Echo:"/>
<h:outputText id="echo" value="#{echoBean.name}"/>
<h:outputText value="Count:"/>
<h:outputText id="count" value="#{echoBean.count}"/>
</h:panelGrid>
<a4j:commandButton value="Submit" actionListener="#{echoBean.countListener}" render="echo, count"/>
</rich:panel>
</h:form>
</ui:define>
and a simple bean:
#Component("echoBean")
#Scope(value = "session")
public class EchoBean {
private String name;
private Integer count = 0;
//getter setter methods here
public void countListener(ActionEvent event) {
count++;
}
}
And when i try to print in inputText i have exception:
Caused by: javax.el.MethodNotFoundException: /home.xhtml #35,112 listener="#{echoBean.countListener}": Method not found: com.example.training.bean.EchoBean#d523fa.countListener()
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at org.ajax4jsf.component.behavior.MethodExpressionAjaxBehaviorListener.processAjaxBehavior(MethodExpressionAjaxBehaviorListener.java:71)
at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:113)
at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:98)
at org.ajax4jsf.component.behavior.AjaxBehavior.broadcast(AjaxBehavior.java:348)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:763)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
... 19 more
But why? With button this same listener works just fine and in docs for "listener" parameter in a4j:ajax it says that:
The expression must evaluate to a public method that takes an ActionEvent parameter, with a return type of void, or to a public method that takes no arguments with a return type of void
Why it uses countListener() without ActionEvent parameter? I don't get it.
For you to be able to use the listener attribute with RF4, your listener method should take an argument of the AjaxBehaviorEvent type, not an ActionEvent type. The other alternative approach as you can see from the error message is to define a standard java method that doesn't take arguments and has a void return type as in
public void countListener();
Why it uses countListener() without ActionEvent parameter? I don't get it.
That's the contract for the API, you're required to conform to be able to use it.
Use the bean function with the following signature
void as return type
ActionEvent object as parameter
Example for the bean function is as below
public void countListener(ActionEvent event) {}

Jsf2 view parameters and viewscoped beans

How can I access the view parameters from a viewscoped bean?
I have a page almost with the same content as this:
<f:metadata>
<f:viewParam name="name" value="#{goToUserpageRequest.name}" />
</f:metadata>
<ui:define name="content">
<h:outputText value="#{user.name}" styleClass="text"></h:outputText>
<h:outputText value="#{user.description}" styleClass="text"></h:outputText>
</ui:define>
GoToUserpageRequest is a bean which I use to redirect to this page, so I can send the value for name.
User is my viewscoped bean. I want to pass the value of viewParam name to user.name. How can I do that?
Thanks in advance!
There is an easier way for your case which I have just figured out while looking for a solution for the same situation.
just use this in your xhtml together :
<f:metadata>
<f:viewParam name="name" value="#{goToUserpageRequest.name}" />
</f:metadata>
<f:event type="preRenderView" listener="#{MY_BEAN.setName(goToUserpageRequest.name)}"/>
so you can send the goToUserpageRequest.name value back to your redirected view's bean (I called MY_BEAN)
You can get this information using the external context from your context. See the request parameters.
However, I would try to use a request scope bean and inject the view and parameter scope values into that. You can then manipulate your view scoped object from there. This approach is easier to unit test.
EDIT:
Here is a sample implementation:
#ManagedBean #RequestScoped
public class NameUpdater {
#ManagedProperty("#{param.name}") private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
#ManagedProperty("#{user}") private User user;
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
#PostConstruct public void init() {
if(name != null) user.setName(name);
}
}
In order to create the request scoped bean, the binding expression would change to something like:
<h:outputText value="#{nameUpdater.user.name}" />

Jsf 2.0 Populate h:selectOneMenu to access page

I am newbie in JSF 2.0, I worked in JSF 1.1 and 1.2 and I populate selectOneMenu in constructor of Managed bean's page. For when users to acces to page the List is populate. example below. I put the same in JSF 2.0 but is not work, the selectOneMenu appears empty.
<h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}">
<f:selectItems value="#{PersonBean.status}"/>
</h:selectOneMenu>
In constructor's managed bean I put:
public class PersonBean {
private SelectItem[] status=new SelectItem[0];
public PersonBean () {
detallePersonas= new ArrayList();
status= new SelectItem[3];
status[0]=new SelectItem("S","Single");
status[1]=new SelectItem("M","Married");
status[2]=new SelectItem("D","Divorced");
}
}
Editor Netbeans 6.8 (JSF 2.0 default configuration wizard)
No Exception Error
Never run the constructor PersonBean (I put a breakpoint and never stops)
There are other ways to populate selects to load the page
This is the complete code:
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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Person</title>
</h:head>
<h:body>
<h:form id="frmPerson">
<h:outputLabel id="lblStatus" value="Status:"/>
<h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}">
<f:selectItems value="#{PersonBean.status}"/>
</h:selectOneMenu>
</h:form>
</h:body>
</html>
PersonBean.java
package com.prueba.backingbean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
/**
*
* #author Administrador
*/
#ManagedBean(name = "Person")
#ViewScoped
public class PersonBean {
private String idStatus;
private SelectItem[] status = new SelectItem[0];
public PersonBean() {
status = new SelectItem[3];
status[0] = new SelectItem("S", "Single");
status[1] = new SelectItem("M", "Married");
status[2] = new SelectItem("D", "Divorced");
}
/**
* #return the idStatus
*/
public String getIdStatus() {
return idStatus;
}
/**
* #param idStatus the idStatus to set
*/
public void setIdStatus(String idStatus) {
this.idStatus = idStatus;
}
/**
* #return the status
*/
public SelectItem[] getStatus() {
return status;
}
/**
* #param status the status to set
*/
public void setStatus(SelectItem[] status) {
this.status = status;
}
}
private Map<String,String>status = new HashMap<String,String>();
.......
status.put("S", "Single");
status.put("M", "Married");
status.put("D", "Divorced");
in JSF PAGE:
<h:selectOneMenu value="#{personBean.idStatus}" >
<f:selectItems value="#{personBean.status}"/>
</h:selectOneMenu>
It should be "personBean" instead of "PersonBean" (first letter should be lowercase). You also need getter for status (getStatus()) and setter/getter for idStatus (setIdStatus()/getIdStatus()). Are they there?
Look here:
#ManagedBean(name = "Person")
#ViewScoped
public class PersonBean {
You've declared the managed bean name as Person. So it's in JSF EL available as #{Person}. Yet you're attempting to access it as #{PersonBean}. Because such a bean does not exist, the menu remains empty.
You've 3 options:
Rename #{PersonBean} by #{Person} in your JSF page.
Rename #ManagedBean(name = "Person") to #ManagedBean(name = "PersonBean") in your managed bean.
Get rid of bean name and use the default JSF naming conventions. I.e. just use #ManagedBean without a name and use #{personBean} in your JSF page (in essence, the bean's class name with first character lowercased).
Option 3 is preferred.
Have you tried using List instead of Array of SelectItems.
Below code might help you.
private List<SelectItem> status = new ArrayList<SelectItem>();
status.add(new SelectItem("S","Single"));
status.add(new SelectItem("M","Married"));
status.add(new SelectItem("D","Divorced"));
/*
SETTER / GETTER
*/

JBoss Seam - Can't #In annotations be used at the same time with accessor methods for different properties?

I generated a new form using sean-gen (seam new-form) and added another field to it using an #In annotation:
#Stateful
#Name("dummy")
public class DummyBean implements Dummy
{
#Logger private Log log;
#In StatusMessages statusMessages;
#In private String bar;
private String foo;
public void doStuff()
{
String msg = "dummy.doStuff() action called with foo: #{dummy.foo} and bar: #{bar}. instance variable for bar:" + bar;
log.info(msg);
statusMessages.add(msg);
}
#Length(max = 10)
public String getFoo()
{
return foo;
}
public void setFoo(String value)
{
this.foo = value;
}
#Remove
public void destroy() {}
}
The interface is this one:
#Local
public interface Dummy
{
public void doStuff();
public String getFoo();
public void setFoo(String value);
public void destroy();
}
The problem is, when I try to access the properties I get:
javax.faces.FacesException: javax.el.ELException: /dummy.xhtml #22,52 value="#{dummy.foo}": Error reading 'foo' on type org.javassist.tmp.java.lang.Object_$$_javassist_seam_2
at javax.faces.component.UIOutput.getValue(UIOutput.java:187)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:201)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:284)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:154)
.
.
.
Caused by: javax.ejb.EJBTransactionRolledbackException: #In attribute requires non-null value: dummy.bar
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:115)
at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
My view is this:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
xmlns:a="http://richfaces.org/a4j"
template="layout/template.xhtml">
<ui:define name="body">
<h:form id="dummyForm">
<rich:panel>
<f:facet name="header">dummy header</f:facet>
<s:decorate id="fooField" template="layout/edit.xhtml">
<ui:define name="label">Foo</ui:define>
<h:inputText id="foo" required="true"
value="#{dummy.foo}"/>
</s:decorate>
<s:decorate id="barField" template="layout/edit.xhtml">
<ui:define name="label">Bar</ui:define>
<h:inputText id="bar" required="true"
value="#{bar}"/>
</s:decorate>
<div style="clear:both"/>
</rich:panel>
<div class="actionButtons">
<h:commandButton id="doStuff" value="doStuff"
action="#{dummy.doStuff}"/>
</div>
</h:form>
</ui:define>
</ui:composition>
If I remove either 'fooField' or 'barField' the view renders and works correctly, but if I try to use both at the same time I get the above exception.
I also noticed that if I use only #In annotations (remove the accessor methods from the bean) the page works.
Is this something expected and I should be doing my homework before asking?
I'm using JBoss 5.1.0.GA, Seam 2.2.0.GA and Java 6.
Turns out this is expected behavior, quoting an answer from the seam forum:
Use #In(required=false) if the Seam component you're injecting does not have the #AutoCreate annotation at the class level.
So, changing:
#In private String bar;
to:
#In (required=false) private String bar;
fixes the issue.

Categories

Resources