JSF selectOneMenu - setting value in bean - jsf

I have selectOneMenu
<h:selectOneMenu id = "current" value = "#{helloBean.currentLanguage}">
<f:selectItems value="#{helloBean.categoryLanguages}"></f:selectItems>
<f:ajax render = "ccData"></f:ajax>
</h:selectOneMenu>
Here is HelloBean
#ManagedBean(name="helloBean")
#SessionScoped
public class HelloBean implements Serializable {
private String currentLanguage;
public void setcurrentLanguage(String currentLanguage){
this.currentLanguage = currentLanguage;
}
}
I have a problem with setting of currentLanguage. Eclipse shows an error message that #{helloBean.currentLanguage} expression is not settable. And when I run my project, I get an error
javax.servlet.ServletException: /hello.xhtml #19,74 value="#{helloBean.currentLanguage}": Property 'currentLanguage' not readable on type java.lang.String

As BalusC pointed out, Java is case sensitive.
So when you write: value=#{helloBean.currentLanguage} JSF expects that you define a setter and a getter of that attribute (currentLanguage):
public void setCurrentLanguage(String language){
this.currentLanguage = language;//Or whatever
}
(Notice the upper C in current. And add a getter)
Since you are using Eclipse, try to automatically generate access methods of your classes. (Right click -> source -> generate getter and setters)

Related

Is a value put to viewScope map in EL without explicit converting always cast to java.lang.String?

The value in f:selectItem is an Integer (Const.DB_NEW_DATASET) but the output of testlistener is always java.lang.String. That's not what I had expected.
xhtml
<f:metadata>
<f:importConstants type="foo.bar.Const" />
</f:metadata>
<h:selectOneListbox value="#{viewScope.foo}">
<f:selectItem
itemValue="#{Const.DB_NEW_DATASET}"
itemLabel="foo" />
<f:selectItem
itemValue="#{Const.DB_NEW_DATASET}"
itemLabel="bar" />
<f:ajax listener="#{myBean.testlistener}" />
</h:selectOneListbox>
bean
#Named
#ViewScoped
public class MyBean implements Serializable {
#Inject
#ViewMap
private Map<String, Object> viewMap;
public void testlistener() {
System.out.println(viewMap.get('foo').getClass());
}
}
public class Const {
public static final Integer DB_NEW_DATASET = -1;
}
Mojarra 2.3.9.SP01
It's actually not "cast to java.lang.String" at all. It's just the default type of ServletRequest#getParameter() which is then left unconverted by JSF.
This is because the ValueExpression#getType() of #{viewScope.foo} returns java.lang.Object and thus JSF won't perform any automatic conversion against any registered #FacesConverter(forClass).
You need to explicitly specify the built-in javax.faces.Integer converter which is essentially a #FacesConverter(forClass=java.lang.Integer).
<h:selectOneListbox ... converterId="javax.faces.Integer">
This is not necessary if you're using e.g. #{bean.foo} with a private Integer foo, because this way the ValueExpression#getType() will return java.lang.Integer and thus JSF can find the built-in converter.
See also:
Why does JSF put String values in a Map<..., Integer>? And how to work around it?
Conversion Error setting value for 'null Converter' - Why do I need a Converter in JSF?

Get managed bean and type bound to "value" attribute

Let's suppose I have following structure:
1) Managed Bean:
#ViewScoped
#ManagedBean
public class TestBean {
private Test test;
//getters/setters
}
2) Test class:
public class Test {
private String attribute;
//gets/sets
}
3) XHTML
<p:inputText id="test" value="#{testBean.test.atribute}" />
Now, I know there is a way to find and get component instance:
UIComponent c = view.findComponent(s);
From UIComponent, how do I get the type bound to component?
What I need is to get full qualified class name from what is set as "value" attribute in component. Something like: package.Test.attribute.
UIComponent offers getValueExpression("attributeName")
sample :
UIViewRoot viewRoot = Faces.getViewRoot();
UIComponent component= viewRoot.findComponent("x");
ValueExpression value = component.getValueExpression("value");
Class<?> expectedType = value.getType(Faces.getELContext());
NB:Faces here is from Omnifaces, which is a "Collection of utility methods for the JSF API that are mainly shortcuts for obtaining stuff from the thread local FacesContext. "
excepts from getType() javadoc
public abstract Class getType(ELContext context) Evaluates the
expression relative to the provided context, and returns the most
general type that is acceptable for an object to be passed as the
value parameter in a future call to the setValue(javax.el.ELContext. java.lang.Object) method. This is not always the same as
getValue().getClass(). For example, in the case of an expression that
references an array element, the getType method will return the
element type of the array, which might be a superclass of the type of
the actual element that is currently in the specified array element.
For MethodExpression read this.

Conversion Error setting value 'dbaccess.persistence.Employee[id=66666666]' for 'null Converter'

Here's a snippet of the form I'm trying to submit:
<h:outputText value="Employees"></h:outputText>
<h:selectManyListbox id="employees"
value="#{lookupControl.memberEmployees}"
converter="#{employeeConverter}">
<f:selectItems value="#{lookupControl.employees}"
var="emp"
itemLabel="#{emp.EMPLOYEE_NUMBER}"
itemValue="#{emp}"/>
</h:selectManyListbox>
<h:message id="employeesMsg"
for="employees"
errorStyle="color:red; display:block"
styleClass="errorMessage"/>
<h:outputText value="Project Lead"></h:outputText>
<h:selectOneListbox id="projectLead"
value="#{lookupControl.chosenLead}"
converter="#{employeeConverter}">
<f:selectItems value="#{lookupControl.employees}"
var="emp"
itemLabel="#{emp.EMPLOYEE_NUMBER}"
itemValue="#{emp}"/>
</h:selectOneListbox>
<h:message id="projectLeadMsg"
for="projectLead"
errorStyle="color:red; display:block"
styleClass="errorMessage"/>
And here's the converter employeeConverter:
#FacesConverter(value = "employeeConverter")
public class EmployeeConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// hack to get an em
HelperBean helper = FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(context, "#{helperBean}", HelperBean.class);
EntityManager em = helper.getEm();
System.out.println(value);
Employee tmp = DBHelper.findEmployee(em, value);
return tmp;
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Employee tmp = (Employee) value;
return tmp.getEMPLOYEE_NUMBER();
}
}
The idea being that a "Project" (the object being created when this form is submitted) has a list of members (List<Employee>) and a team leader (Employee). When I try submitting the form, I get a message along the lines of:
Conversion Error setting value 'dbaccess.persistence.Employee[id=66666666]' for 'null Converter'.
It's saying it can't convert from a String to an Employee, but it worked for the field right above it. I'm a little confused.
The "null converter" is the exception that the converter instance cannot be found. Since you're referencing the converter as a managed bean by converter="#{employeeConverter}, it would only be found if it's annotated with #javax.faces.bean.ManagedBean
Add #ManagedBean(name = "employeeConverterBean") into class EmployeeConverter
#ManagedBean(name = "employeeConverterBean")
#FacesConverter(value = "employeeConverter")
public class EmployeeConverter implements Converter {
and use converter="#{employeeConverterBean}" instead of converter="#{employeeConverter}"
You are referring to a scoped variable in your converter= attribute, but #FacesConverter does not create a scoped variable. Rather, it registers your converter class with the converter ID you specify.
From the documentation for FacesConverter.value:
The value of this annotation attribute is taken to be the converter-id …
Remove the converter= attribute entirely from your h:selectManyListbox, and instead, add a nested f:converter element:
<h:selectManyListbox id="employees"
value="#{lookupControl.memberEmployees}">
<f:converter converterId="employeeConverter"/>
<f:selectItems value="#{lookupControl.employees}"
var="emp"
itemLabel="#{emp.EMPLOYEE_NUMBER}"
itemValue="#{emp}"/>
</h:selectManyListbox>
Update: I'd always taken the tag documentation literally, which says the attribute's value "must evaluate to javax.faces.convert.Converter". BalusC points out that a nested f:converter is not needed; passing a literal converter ID in the converter= attribute will work:
<h:selectManyListbox id="employees"
value="#{lookupControl.memberEmployees}"
converter="employeeConverter">
<f:selectItems value="#{lookupControl.employees}"
var="emp"
itemLabel="#{emp.EMPLOYEE_NUMBER}"
itemValue="#{emp}"/>
</h:selectManyListbox>
I had never felt comfortable doing that in the past, without a documented guarantee that it works. I just looked through the JSF specification and found this under its "Standard HTML RenderKit Tag Library" section:
The following action must be taken to handle the value of the converter property. If isLiteralText() on the converter property returns true, get the value of the property and treat it as a converterId by passing it as the argument to the createConverter() method of the Application instance for this webapp, then pass the created Converter to the setConverter() method of the component for this tag.
So yes, passing a converter-id in the converter= attribute is completely supported.

OmniFaces:SelectItemsConverter not working with PrimeFaces: PickList

I'm trying to use SelectItemsConverter with PrimeFaces Picklist.
XHTML:
<p:pickList id="plUpdateFirma" value="#{bsvttController.dlmFirma}" var="plFirma"
itemLabel="#{plFirma.schluesselFirma}" itemValue="#{plFirma}"
converter="FirmaConverter">
<f:facet name="sourceCaption">
Vorjahr
</f:facet>
<f:facet name="targetCaption">
#{bsvttController.selSaison}
</f:facet>
<p:column>
#{plFirma.schluesselFirma}
</p:column>
</p:pickList>
Converter:
#FacesConverter(value = "FirmaConverter")
public class FirmaConverter extends SelectItemsConverter
{
#Override
public String getAsString(final FacesContext facesContext, final UIComponent component, final Object object)
{
return ((Firma) object).getSchluesselFirma();
}
}
Bean:
#ManagedBean
#ViewScoped
public class BsvttController implements Serializable
{
private DualListModel<Firma> dlmFirma;
private List<Firma> dlmFirmaSource;
private List<Firma> dlmFirmaTarget;
private Firma firma;
#PostConstruct
public void init()
{
dlmFirmaSource = FirmaPersistenz.leseFirmaAlle();
dlmFirmaTarget = new ArrayList<Firma>();
dlmFirma = new DualListModel<>(dlmFirmaSource, dlmFirmaTarget);
}
public DualListModel<Firma> getDlmFirma()
{
return dlmFirma;
}
public List<Firma> getDlmFirmaSource()
{
return dlmFirmaSource;
}
public List<Firma> getDlmFirmaTarget()
{
return dlmFirmaTarget;
}
public void setDlmFirma(DualListModel<Firma> dlmFirma)
{
this.dlmFirma = dlmFirma;
}
public void setDlmFirmaSource(List<Firma> dlmFirmaSource)
{
this.dlmFirmaSource = dlmFirmaSource;
}
public void setDlmFirmaTarget(List<Firma> dlmFirmaTarget)
{
this.dlmFirmaTarget = dlmFirmaTarget;
}
}
While debugging converter I could see that getAsString method is working fine. But after submitting the form both arraylists (dlmFirmaSource and dlmFirmaTarget) are empty.
OmniFaces showcase says that
"The omnifaces.SelectItemsConverter allows you to populate e.g. a drop-down with complex Java model objects as value of f:selectItems and have JSF convert those automatically back without the need to provide a custom converter which may need to do the job based on possibly expensive service/DAO operations."
But in case of PickList component there doesn't exist any f:selectItems tag.
Does SelectItemsConverter even support PickList component?
Does SelectItemsConverter even support PickList component?
No, it doesn't.
Since OmniFaces 1.5, you can use omnifaces.ListConverter or omnifaces.ListIndexConverter for the desired purpose. See also the ListConverter showcase example which also demonstrates the usage on <p:pickList>.
No, the SelectItemsConverter handles conversion of core JSF SelectItem objects for use with various JSF components.
The class DualListModel is a PrimeFaces specific class meant for use with advanced PrimeFaces data components. The workaround of course is to possible use a #PostConstruct method to initialize your DualListModel in the managed bean so that it does not require a converter, or you can simply implement the converter in the traditional way. From the PrimeFaces guide on the converter attribute of Pick List:
An el expression or a literal text that defines a
converter for the component. When it’s an EL
expression, it’s resolved to a converter instance.
In case it’s a static text, it must refer to a
converter id

Difference between value and binding

What is the difference between using value and binding with JavaServer Faces, and when would you use one as opposed to the other? To make it clearer what my question is, a couple of simple examples are given here.
Normally with JSF in the XHTML code you would use "value" as here:
<h:form>
<h:inputText value="#{hello.inputText}"/>
<h:commandButton value="Click Me!" action="#{hello.action}"/>
<h:outputText value="#{hello.outputText}"/>
</h:form>
Then the bean is:
// Imports
#ManagedBean(name="hello")
#RequestScoped
public class Hello implements Serializable {
private String inputText;
private String outputText;
public void setInputText(String inputText) {
this.inputText = inputText;
}
public String getInputText() {
return inputText;
}
// Other getters and setters etc.
// Other methods etc.
public String action() {
// Do other things
return "success";
}
}
However, when using "binding", the XHTML code is:
<h:form>
<h:inputText binding="#{backing_hello.inputText}"/>
<h:commandButton value="Click Me!" action="#{backing_hello.action}"/>
<h:outputText value="Hello!" binding="#{backing_hello.outputText}"/>
</h:form>
and the correspondibg bean is called a backing bean, and is here:
// Imports
#ManagedBean(name="backing_hello")
#RequestScoped
public class Hello implements Serializable {
private HtmlInputText inputText;
private HtmlOutputText outputText;
public void setInputText(HtmlInputText inputText) {
this.inputText = inputText;
}
public HtmlInputText getInputText() {
return inputText;
}
// Other getters and setters etc.
// Other methods etc.
public String action() {
// Do other things
return "success";
}
}
What practical differences are there between the two systems, and when would you use a backing bean rather than a regular bean? Is it possible to use both?
I have been confused about this for some time, and would most appreciate having this cleared up.
value attribute represents the value of the component. It is the text that you see inside your text box when you open the page in browser.
binding attribute is used to bind your component to a bean property. For an example in your code your inputText component is bound to the bean like this.
#{backing_hello.inputText}`
It means that you can access the whole component and all its properties in your code as a UIComponent object. You can do lot of works with the component because now it is available in your java code.
For an example you can change its style like this.
public HtmlInputText getInputText() {
inputText.setStyle("color:red");
return inputText;
}
Or simply to disable the component according to a bean property
if(someBoolean) {
inputText.setDisabled(true);
}
and so on....
Sometimes we don't really need to apply the value of UIComponent to a bean property. For example you might need to access the UIComponent and work with it without applying its value to the model property. In such cases it's good to use a backing bean rather than a regular bean. On the other hand in some situations we might need to work with the values of the UIComponent without any need of programmatic access to them. In this case you can just go with the regular beans.
So, the rule is that use a backing bean only when you need programmatic access to the components declared in the view. In other cases use the regular beans.

Resources