Why do I always get NULL in JSF to get the postgresql boolean field?

I have a Postgresql 8.4 table

CREATE TABLE users ( username character varying(50) NOT NULL, "password" character varying(50) NOT NULL, enabled boolean NOT NULL, type_of_signature boolean NOT NULL, companyusers2_id integer NOT NULL, numberorganizac character(8) NOT NULL, ); 

In this table, I have only one row: "admin"; "admin"; TRUE TRUE 1; "12345678"

I have jpa

 @Entity @Table(name="users") public class Users implements Serializable { ... private boolean typeOfSignature; ... @Column(name="type_of_signature") public boolean getTypeOfSignature() { return this.typeOfSignature; } public void setTypeOfSignature(boolean typeOfSignature) { this.typeOfSignature = typeOfSignature; } ... } 

I have jsf

 <h:outputText value="Type of signature is NULL" rendered="#{curUser.typeOfSignature == null}"/> <h:outputText value="Type of Signature is TRUE" rendered="#{curUser.typeOfSignature}"/> <h:outputText value="Type of Signature is FALSE" rendered="#{!curUser.typeOfSignature}"/> 

I always get "Signature Type NULLType Signature FALSE" on the results page.

But I also have a method in my bean

 if(getCurUser().getTypeOfSignature()) { jpaBean.pushSignature(dataItem, 1); } else { jpaBean.pushSignature(dataItem, 2); } 

And it works correctly depending on the type of user signature.

Why do I always get NULL in JSF? Or am I a newbie and did something wrong?

+6
source share
2 answers

I ran into the same problem before, trying to use the Boolean type with JPA and JSF, you can try to create a custom BooleanConverter and map your JPA object to a boolean rather than a raw buffer.

Even though you may run into portability issues when starting up using various databases, JSF and JPA implementations. I highly recommend you change your approach on the Booleans map as strings in JPA:

 CREATE TABLE users ( username character varying(50) NOT NULL, "password" character varying(50) NOT NULL, enabled haracter(1) NULL, type_of_signature char(1) NOT NULL, companyusers2_id integer NOT NULL, numberorganizac character(8) NOT NULL, ); 

and your display will look like this:

 @Entity @Table(name="users") public class Users implements Serializable { ... private String typeOfSignature; ... @Column(name="type_of_signature") public String getTypeOfSignature() { return this.typeOfSignature; } public void setTypeOfSignature(String typeOfSignature) { this.typeOfSignature = typeOfSignature; } ... } 

Fill your entry with one line: "admin"; "admin"; 'T'; 'T'; 1; "12345678" then in JSF you get a different result:

 <h:outputText value="Type of signature is NULL" rendered="#{empty curUser.typeOfSignature}"/> <h:outputText value="Type of Signature is TRUE" rendered="#{curUser.typeOfSignature eq 'T'}"/> <h:outputText value="Type of Signature is FALSE" rendered="#{!curUser.typeOfSignature eq 'T'}"/> 
+1
source

Have you tried:

 <h:outputText value="Type of signature is True" rendered="#{true}"/> <h:outputText value="Type of signature is False" rendered="#{false}"/> 

?

If this fails, it means that something is wrong with the binding value. This should not be for basic html elements.

 import javax.faces.component.ActionSource; import javax.faces.component.UIComponent; import javax.faces.component.ValueHolder; import javax.faces.component.html.HtmlOutputText; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.el.ValueBinding; public class MyHtmlOutputText extends HtmlOutputText { public boolean isRendered() { if (rendered != null) { return rendered; } ValueBinding vb = getValueBinding("rendered"); Boolean v = vb != null ? (Boolean) vb.getValue(getFacesContext()) : null; return v != null ? v : DEFAULT_DISABLED; } public boolean getRendered() { return isRendered(); } } 

For some reason, I am not saving the result of value binding. This is because the value stored in the state will no longer be deleted from the original expression at run time. This is what we ultimately do not want to have.

Here you will learn how to register your MyHtmlOutputText component in the context of facelets: http://hanzz.lbs-logics.at/index.php?option=com_content&task=view&id=107&Itemid=31

0
source

Source: https://habr.com/ru/post/890903/


All Articles