I've created a custom control that is basically a checkbox. I want the checkbox read the value of the dataSource I pass in - which would be a managed bean. I can get the checkbox field to read from the bean but I'm not seeing anything happen when I change the checkbox. It doesn't look like the setter in the bean ever gets called.
The key snippets of my bean are:
private boolean categoriesOn;
...
public boolean isCategoriesOn() {
System.out.println("Getting On Value");
return categoriesOn;
}
public void setCategoriesOn(boolean newValue) {
System.out.println("Setting On : " + newValue);
this.categoriesOn = newValue;
}
The control on the XPage looks like this:
<xp:checkBox id="flipSwitch"
styleClass="onoffswitch-checkbox"
value="${compositeData.dataSource}"
checkedValue="#{javascript:true}"
uncheckedValue="#{javascript:false}">
<xp:eventHandler event="onchange" submit="true"
refreshMode="complete">
</xp:eventHandler>
</xp:checkBox>
I pass the bean to the custom control with a custom property:
<xc:crtl_toggleSwitch
dataSource="#{exhibitorInfo.categoriesOn}"
refreshID="computedField6">
</xc:crtl_toggleSwitch>
dataSource is set to use Methodbinding.
I've tried with partial and full refresh. I'm just not sure what I need to do to get the changed value back into the bean.
thanks for any advice.
As indicated in Peter's answer on the question Per linked to, checkboxes cannot be bound directly to booleans (which is admittedly ridiculous). Add these methods:
public String getCategoriesOnAsString(){
return isCategoriesOn() ? "1" : "0";
}
public void setCategoriesOnAsString(String value){
setCategoriesOn("1".equals(value));
}
Then bind your checkbox to #{exhibitorInfo.categoriesOnAsString}, and set checkedValue and uncheckedValue to "1" and "0", respectively.
Related
So, here is the jsf component:
<h:selectBooleanCheckbox id="cb#{index}" value="#{backingBean.value}" />
And here is a part of the backing bean java:
/**
* getValue is a method which checks if a checkbox is selected or not, using the checkbox ID
*/
public boolean getValue() {
//TODO: get the checkbox id
String checkboxID = ??
if (getCheckedIDs().contains(checkboxID)) {
return true;
}
return false;
}
When the page is loading the checkboxes, I want to check this way if the checkbox is selected or not. So the question is, what to write instead of ?? to get the ID of the checkbox who called the method? It's very important that I can use only JSF 1.1, so there are many solutions which won't work with this version.
Another very important thing is, that I cannot use the setter/getter in backing bean like here: https://stackoverflow.com/a/48006066/9158590, because I need to store the value of the checkbox immediately after it's checked or unchecked, not only after submit. I have already resolved the storing in backing bean right after checking, I only need to send back true or false when loading page.
This is because I use a page navigation, and for example, when I check a box in page 1, and go to another page, and then go back, the box isn't selected anymore (only in backing bean).
FacesContext context = FacesContext.getCurrentInstance();
UIComponent comp = context.getViewRoot().findComponent("Parent Element
id of HtmlSelectBooleanCheckbox ");
for(UIComponent c : comp.getChildren())
if(c instanceof HtmlSelectBooleanCheckbox)
{
// do something
}
Coming to your Question :
the value of the variable "#{backingBean.value}" is true then the checkbox will be selected
I need your help in disabling and enabling an item from the selectManyCheckbox component in a jsf page. First of all, the selectManyCheckbox component is showing three chechboxes which are (Loan - Health - Transfer). The list will be populated from a bean which it has the code:
private List<hrCertificate> hrCertificatesList = new ArrayList<hrCertificate>();
//Getter and Setter
Private String loanFlag="";
#PostConstruct
public void init() {
this.hrCertificatesList.add(new hrCertificate(("Loan"), "LC"));
this.hrCertificatesList.add(new hrCertificate(("Health"), "HI"));
this.hrCertificatesList.add(new hrCertificate(("Trasnfer"), "TE"));
}
In the same bean, I will be running a SQL statement that will return either Yes or No and that value I am adding it to the loanFlag variable.So if the flag="Y", I need to enable the loan checkbox so the user can select it else I need to disable it from the selectManyCheckbox. The issue is that I am facing difficulties in applying the logic to disable and to enable the item selectManyCheckboxwhere in the above code I am listing and enabling them all the time.
The code for the selectManyChexkbox:
<p:selectManyCheckbox id="hrCertificates" value="#{user.selectedHRCertificates}" layout="pageDirectio>
<f:selectItems value="#{user.hrCertificatesList}"
var="hrCertificate" itemLabel="#{hrCertificate.hrCertificateName}"
itemValue="#{hrCertificate.hrCertificateCode}"/>
</p:selectManyCheckbox>
So how to apply the logic
Could you edit your hrCertificate class to add a disabled boolean field? If yes, then you can add itemDisabled="#{hrCerticate.disabled}" to your f:selectItems which should be the easiest solution.
Another option would be to use a Map<hrCertificate, Boolean> instead of a List<hrCertificate>.
private Map<hrCertificate, Boolean> hrCertificatesMap = new HashMap<hrCertificate, Boolean>();
#PostConstruct
public void init() {
hrCertificatesMap.put(new hrCertificate(("Loan"), "LC"), null);
hrCertificatesMap.put(new hrCertificate(("Health"), "HI"), null);
hrCertificatesMap.put(new hrCertificate(("Trasnfer"), "TE"), null);
}
// Then when you're done with your SQL query, update your Map to add the corresponding boolean values...
.xhtml
<p:selectManyCheckbox id="hrCertificates" value="#{user.selectedHRCertificates}" layout="pageDirectio>
<f:selectItems value="#{user.hrCertificatesMap.keySet().toArray()}" var="hrCertificate" itemLabel="#{hrCertificate.hrCertificateName}" itemValue="#{hrCertificate.hrCertificateCode}" itemDisabled="#{user.hrCertificatesMap.get(hrCertificate)}" />
</p:selectManyCheckbox>
First, note that a property does not retire an actual attribute backing it, you only need a getter. So you can have:
public class MyBean implements Serializable {
private FilterEnum certFilter = FilterEnum.NO_FILTER;
private List<Certificate> certificates;
... // including certificates initialization.
public FilterEnum getCertFilter() {
return this.certFilter;
}
public void setCertFilter(FilterEnum certFilter) {
this.certFilter = certFilter;
}
public List<Certificate> getCertificates() {
// I am sure there is a cooler way to do the same with streams in Java 8
ArrayList<Certificate> returnValue = new ArrayList<>();
for (Certificate certificate : this.certificates) {
switch (this.certFilter) {
case FilterEnum.NO_FILTER:
returnValue.add(certificate);
break;
case FilterEnum.ONLY_YES:
if (certificate.isLoan) {
returnValue.add(certificate);
}
break;
case FilterEnum.ONLY_NO:
if (!certificate.isLoan) {
returnValue.add(certificate);
}
break;
}
}
return returnValue;
}
}
If you insist that you want to do the filter "in the .xhtml", you can combine c:forEach from JSTL with <f:selectItem> (note item, not items), but it will make your xhtml more complicated and may cause issues if you want to use Ajax with it.
Domino 8.5.3 FP5, Designer 9.0.1.
I have an empty xpage containing only the following checkbox. I am not able to make it editable. It show the right value, but appear as disabled.
<xp:checkBox text="Label" id="checkBox1" uncheckedValue="true"
checkedValue="false"
value="#{javascript:jBeanConfigSupport.validationEnabledTxt}">
</xp:checkBox>
If I remove the binding to Java bean, all run fine. This is the Java bean code (part)
private boolean isValidationEnabled=true;
public String getValidationEnabledTxt() {
return String.valueOf(isValidationEnabled);
}
public void setValidationEnabledTxt(String onOff) {
isValidationEnabled=Boolean.parseBoolean(onOff);
}
public void setValidationEnabledTxt(boolean onOff) {
isValidationEnabled=onOff;
}
Where Am I wrong?
You're binding using SSJS, so instead of binding to the validationEnabledTxt property of your bean, the result of jBeanConfigSupport.validationEnabledTxt is being used to determine what it should be bound to. value="#{jBeanConfigSupport.validationEnabledTxt}" should work to map to the getter and setter.
Francesco,
You need to have a working setter method for this to be editable. In your case there is probably a problem with the setValidationEnabledTxt() method that is causing the field in the UI to be readonly.
Try adding "this" to your code so that it reads this.isValidationEnables = Boolean.parseBoolean(onOff);
I want to disable some menuitem on my web page:
<p:menuitem value="Edit" update=":formedit:viewDisplayEdit" icon="ui-icon-document" oncomplete=... disabled="#{bean.ask(1)}"/>
And in my backup bean:
public boolean ask(int id)
{
Ask the database here for privilege "id" and return true if logged user have privilege "id"
else return false
}
Of course I'd like to check it only once - during the rendering of the page.
In this example, regardless of the result of the method "ask" menuitem is always enabled.
I know that I can set the boolean variable xmls code (#{bean.ask1}), but I would like a universal solution.
Could you help?
Make ask a private variable in the bean with get and set methods.
private boolean ask = true;
public boolean isAsk(){
return ask;
}
public void setAsk(boolean ask){
this.ask = ask;
}
Now for whatever condition you want to enable or disable the menu item, set or reset the ask variable appropriately in a different method in the bean.
if (condition = true){
setAsk(false);
}
I have created jsf form at backing bean. I have created form, it is being shown on screen with values successfully. When I click sumbit button, backing bean method is invoked with ActionEvent but updated input values are not submitted to backend. Backign bean entity object values are not updated.
Is there any wrong? Thanks so much for your helps,
Br.
Ramazan
HtmlForm form = new HtmlForm();
form.setId(appletWrapper.getName()+"_FORM");
PanelGrid panelGrid = (PanelGrid)createComponent(PanelGrid.COMPONENT_TYPE);
form.getChildren().add(panelGrid);
panelGrid.setColumns(4);
panelGrid.setId(appletWrapper.getName()+"_FORM_PANEL");
for (FieldWrapper fieldWrapper : appletWrapper.getFields()) {
panelGrid.getChildren().add(new UIFieldLabel(fieldWrapper).component(entities));
panelGrid.getChildren().add(newFieldComponent(fieldWrapper, entities));
}
HtmlPanelGroup panelGroup = new HtmlPanelGroup();
panelGroup.setId(appletWrapper.getName()+"_PANEL_GROUP");
panelGrid.getFacets().put("footer", panelGroup);
CommandButton commandButton = new CommandButton();
panelGroup.getChildren().add(commandButton);
commandButton.setId(appletWrapper.getName()+"_UPDATE");
commandButton.setAjax(true);
commandButton.setValue("update");
commandButton.setProcess("#this");
commandButton.setType("submit");
MethodExpression updateEntityME = createMethodExpression("#{mainBean.entityUpdateListener}", null, new Class[] {ActionEvent.class });
commandButton.addActionListener(new MethodExpressionActionListener(updateEntityME));
return form;
Edited: I gave all components a fixed id.
One of my coponents genarated like that;
InputText inputText = (InputText)createComponent(InputText.COMPONENT_TYPE);
inputText.setId(fieldAlphanumericWrapper.getName());
inputText.setValueExpression("value", createValueExpression("#{mainBean.selection."+fieldAlphanumericWrapper.getProperty()+"}", Object.class));
return inputText;
My backing bean target method;
public void entityUpdateListener(ActionEvent actionEvent) {
TAccount tAccount = (TAccount)getSelection();
System.out.println("tAccount.gettWebsite():"+tAccount.gettWebsite());
}
The main problem is that actually; When I press commandButton mainBean.setSelection is not invoked, so backing bean object is not updated. I cant take updated object instance through actionEvent instance.
I found solution, cause of problem was this line;
commandButton.setProcess("#this");
I have changed to this and problem solved.
commandButton.setProcess("#form");
Br.
Ramazan