JSF - h:selectOneMenu Setter is never called - java

Cant understand <h:selectOneMenu> component. Tried many ways to pass parameter from dropdown to java, and 4 different ways to call java method. First two ways (onchange and commandbutton doesnt call java) other two does, but works badly as it cant set selectedItem to java variable:
<h:form id="tasks">
<h:selectOneMenu id = "selectonemenu" value="#{services.selectedItem}"
immediate="true" onchange="javascript: return this.form.submit();">
<f:selectItems value="#{services.selectItems}" />
</h:selectOneMenu>
<h:commandButton value = "StringHello" action="#{services.StringHelo}" />
<s:link action="#{services.StringHello}" reRender="tasks"></s:link>
<a:commandLink id="aclink"
action="#{services.StringHello}"
ajaxSingle="true" eventsQueue="globalQueue"
ignoreDupResponses="true" requestDelay="0"
onComplete="return false;"
status="globalStatus">
aclink
</a:commandLink>
<h:outputText id = "valueofselected" value = "#{services.selectedItem}"/>
</h:form>
And Services.Java:
private String selectedItem;
public String getSelectedItem() {
if (selectedItem == null) {
selectedItem = "All"; // This will be the default selected item.
}
System.out.println("getSelectedItem "+selectedItem);
return selectedItem;
}
public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
System.out.println("setSelectedItem");
}
public List getSelectItems() {
List selectItems = new ArrayList();
selectItems.add(new SelectItem("All", "All"));
selectItems.add(new SelectItem("A", "A"));
selectItems.add(new SelectItem("B", "B"));
selectItems.add(new SelectItem("C", "C"));
selectItems.add(new SelectItem("D", "D"));
System.out.println("getSelectItems: "+selectItems.size());
return selectItems;
}
public void StringHello(){
System.out.println(" SelectedItem - "+selectedItem);
}
returns null in console:
14:41:51,897 INFO [STDOUT] SelectedItem - null
but i can see that outputText's value is always "All" and in console i see every time when getSelectedItem method is called:
14:41:49,087 INFO [STDOUT] getSelectedItem All
Also, i think it's very important: after i choose any value, page refreshes and it becomes default 'All' value again.

Do you really want to send the whole form (do a form submit)?
Try this, there should be no page refresh:
<h:form id="tasks">
<h:selectOneMenu value="#{services.selectedItem}">
<f:selectItems value="#{services.selectItems}" />
<f:ajax execute="#this" render="result"/>
</h:selectOneMenu>
<h:outputText id="result" value="#{services.selectedItem}"/>
</h:form>

#ujulu was right, i created separate java class with #Scope(ScopeType.SESSION) and my form was located at wrong place, i moved it higher in hierarchy, to get rid of any parent components. And it works now.

Related

how to set default item like "Please select one" in p:selectOneMenu with object from database?

i have a p:selectOneMenu filled with objects from database.When page is looaded first,
the default item of selectOneMenu must be "please select one" in string type( other items of selectOneMenu are in object type.) When page is loaded first, one data in object type from database is visible as default.I don't want this.how to set default item like "Please select one" in p:selectOneMenu with object from database?
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
template="layout.xhtml">
<ui:define name="pageContent">
<h:form id="silYapilandirmaForm" prependId="true">
<p:panel id="silYapilandirmaPanel" header="#{etiketler.silYapilandirma}" collapsed="true" toggleable="true" >
<p:tabView id="tabView" dynamic="false" activeIndex="0">
<p:tab title="#{etiketler.silIslemleri}" id="silIslemleriTab">
<p:outputPanel id="silIslemleriPanel">
<h:panelGrid id="eshsSecPanelGrid" columns="3" >
<h:outputText value="#{etiketler.eshsSertifika}:*" />
<p:selectOneMenu id="silYapilandirmaSelect" value="#{silYapilandirmaView.seciliEshsSertifika}" effect="fade" effectDuration="100" style="width: 205px" converter="#{entityConverter}">
<f:selectItem itemLabel="Please select one" itemValue="#{null}"/>
<f:selectItems value="#{silYapilandirmaView.eshsSertifikaList}" var="eshsSertifika"
itemLabel="#{eshsSertifika.sertifikaKodAdi}" itemValue="#{eshsSertifika}" />
<p:ajax process="#this" event="change" update="eshsSecPanelGrid,silIslemleriPanel" listener="#{silYapilandirmaView.addListener()}" />
</p:selectOneMenu>
<h:outputText value=""/>
<h:outputText value="#{etiketler.eshsSertifika}:" />
<h:inputText value="#{silYapilandirmaView.seciliEshsSertifika.sertifikaKodAdi}" disabled="true" size="30"/>
<h:outputText value=""/>
<h:outputText value="#{etiketler.silUretimBaslangicSaati}:* " />
<p:inputMask id="silUretimBaslangicSaatiInputMask" value="#{silYapilandirmaView.seciliEshsSertifika.silUretimBaslangicSaati}" mask="99:99:99" size="30" />
<p:message for="silUretimBaslangicSaatiInputMask" display="text" />
<h:outputText value="#{etiketler.silUretimBaslangicPeriyodu}:* " />
<p:inputMask maxlength="4" mask="99?99" id="periyotInputMask" size="30" value="#{silYapilandirmaView.seciliEshsSertifika.silUretimPeriyodu}" >
</p:inputMask>
<p:message for="periyotInputMask" display="text" />
</h:panelGrid>
</p:outputPanel>
</p:tab>
</p:tabView>
</p:panel>
</h:form>
</ui:define>
</ui:composition>
i have addListener method in p:ajax in p:selectOneMenu.This method is working in this way;
you are selecting one object from selectOneMenu and this object is put in the last index of the list filling selectOneMenu ,the object in the last index of the list is put in the index of selected object.I mean you are selecting an object and the index of this object in the list is 2.In total there are five objects in the list ,let we say.selected object whose index is 2 is put in index 4 of the list.The object in index 4 of the list is put in index 2 of the list.I am sorry for my english.The aim of this addListener method,selectOneMenu displays the last item of the list as selected always.Whichever object you select,IT DISPLAYS THE LAST ITEM OF THE LIST.Therefore due to this method , the object i selected is put in the last of the list and it displays as selected to user.The method works fine.
But when page is loaded first , the last item of the list is shown as selected in p:selectOneMenu ,i want "please select" to be shown as default."
My bean class is SilYapilandirmaView.java
#ManagedBean
#ViewScoped
public class SilYapilandirmaView extends BaseView implements Serializable {
#ManagedProperty("#{commonService}")
private CommonService commonService;
private EshsSertifika seciliEshsSertifika;
private List<EshsSertifika> eshsSertifikaList;
#PostConstruct
public void init() {
seciliEshsSertifika=new EshsSertifika();
eshsSertifikaList = (List) commonService.hepsiniGetir(EshsSertifika.class);
seciliEshsSertifika = eshsSertifikaList.get(eshsSertifikaList.size() - 1);
}
public CommonService getCommonService() {
return commonService;
}
public void setCommonService(CommonService commonService) {
this.commonService = commonService;
}
public void addListener() {
if(seciliEshsSertifika ==null)
{
return;
}
int i = 0;
for (EshsSertifika eshsSert : eshsSertifikaList) {
if (seciliEshsSertifika.getId() == eshsSert.getId()) {
eshsSert = eshsSertifikaList.get(eshsSertifikaList.size() - 1);
eshsSertifikaList.set(i, eshsSert);
eshsSertifikaList.set(eshsSertifikaList.size() - 1, seciliEshsSertifika);
break;
}
i++;
}
}
public EshsSertifika getSeciliEshsSertifika() {
return seciliEshsSertifika;
}
public void setSeciliEshsSertifika(EshsSertifika seciliEshsSertifika) {
this.seciliEshsSertifika = seciliEshsSertifika;
}
public List<EshsSertifika> getEshsSertifikaList() {
return eshsSertifikaList;
}
public void setEshsSertifikaList(List<EshsSertifika> EshsSertifikaList) {
this.eshsSertifikaList = EshsSertifikaList;
}
}
When i added following code in init() method ,my code is working perfectly!
seciliEshsSertifika=null;
public void init() {
seciliEshsSertifika=new EshsSertifika();
eshsSertifikaList = (List) commonService.hepsiniGetir(EshsSertifika.class);
seciliEshsSertifika = eshsSertifikaList.get(eshsSertifikaList.size() - 1);
seciliEshsSertifika=null;
}
when page is loaded, "please select one" is visible in p:selectOneMenu,but i don't want to make "seciliEshsSertifika" object equal to null.,is there anybody else coming up with another solution?
Try this. get the object you want to set the first item. (this example i get the first object in the list)
bean.java
EshsSertifika firstItem = eshsSertifikaList.get(0);
xhtml
<f:selectItem itemLabel="#{eshsSertifika.firstItem.sertifikaKodAdi}" itemValue="#{eshsSertifika.firstItem}" />

JSF: data not showing when editing twice

I have a dataTable with a list of items and a checkbox for selecting an item to edit. Ticking an item and clicking the edit button pops up a component which has fields and update and cancel buttons. Here's what happens.
Dialog appears
I empty all fields and click UPDATE, required messages appear since all fields are empty, data is not saved
Click CANCEL, dialog disappears
Click and edit the same item again
Some fields are not showing. I checked the datatable and database, data for the item still exists. Just not showing on the edit dialog the 2nd time around.
I noticed that the fields not showing are only those that have NULL attributes. NOT NULL fields are fine. I wonder if this has something to do with sessions. (Using Primefaces for all components)
Code for the edit dialog
<p:dialog header="#{bundle.Edit}" modal="true" widgetVar="editDialog" resizable="false">
<h:form id="edit-form">
<p:messages id="edit-error" autoUpdate="true" closable="true"/>
<h:outputLabel value="#{bundle.Name}" for="name" /><span class="required">*</span>
<p:inputText id="name" value="#{controller.selected.name}" required="true" requiredMessage="#{bundle.Name} #{bundle.FieldIsRequired}" maxlength="45"/>
<h:outputLabel value="#{bundle.Input}" for="input" /><span class="required">*</span>
<h:selectOneRadio id="input" value="#{controller.selected.input}" required="true" requiredMessage="#{bundle.Input} #{bundle.FieldIsRequired}">
<f:selectItem itemLabel="◯ " itemValue="0" />
<f:selectItem itemLabel="☓ " itemValue="1" />
</h:selectOneRadio>
<h:outputLabel value="#{bundle.IsOption}" for="isOption" /><span class="required">*</span>
<h:selectOneRadio id="isOption" value="#{controller.selected.isOption}" required="true" requiredMessage="#{bundle.IsOption} #{bundle.FieldIsRequired}">
<f:selectItem itemLabel="◯ " itemValue="0" />
<f:selectItem itemLabel="☓ " itemValue="1" />
</h:selectOneRadio>
<h:outputLabel value="#{bundle.Remark}" for="remark" />
<p:inputTextarea id="remark" value="#{controller.selected.remark}"/>
<p:commandButton action="#{controller.update()}"
value="#{bundle.Save}"
actionListener="#{controller.prepareList()}"
oncomplete="handleEditDialog(xhr, status, args)"
update=":form:datatable :edit-form:edit-error"
/>
<p:commandButton value="#{bundle.Cancel}"
onclick="editDialog.hide(); reset();"
type="button"/>
</h:form>
</p:dialog>
Code for the update function
public String update() {
RequestContext context = RequestContext.getCurrentInstance();
try {
current.setUpdateDate(new Date());
Map<String, Object> param = JsfUtil.getExternal().getSessionMap();
int createUser = (Integer) param.get("LOGIN_ID");
Account account = accountFacade.find(createUser);
current.setUpdateUser(account);
getFacade().edit(current);
search();
prepareList();
JsfUtil.addSuccessMessage(ResourceBundle.getBundle(JsfUtil.getSessionBundle()).getString("Updated"));
updateOk = true;
current = null;
context.addCallbackParam("updated", true);
return "";
} catch (Exception e) {
if (e.getCause().getCause().getMessage().contains("uk_")) {
JsfUtil.addErrorMessage("edit-form:edit-error",JsfUtil.getResourceString("Duplicate"));
context.addCallbackParam("updated", false);
} else {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle(JsfUtil.getSessionBundle()).getString("PersistenceErrorOccured"));
context.addCallbackParam("updated", false);
}
return null;
}
}
Based on what you are describing I think the following scenario is happening. When you see the form and you delete all fields and you hit save, the components that have no validators are actually overwriting some of the previous property values of your bean. That's why when you reload it for a second edit, you'll notice that all the components that have a validator attached (e.g required = true) are appearing but those with no validators are blank. In reality, you are actually doing a partial update. What you actually need to do is mark all fields as required to avoid that problem.
My teammate just found out a fix for this one. There is a function on the controller generated by NetBeans called prepareList().
public String prepareList() {
recreateModel();
return "";
}
private void recreateModel() {
items = null;
}
Basically we added this on catch block of the update() function. It is called after every unsuccessful update and recreates the model. The bug existed because the field retained its state of not having a value after an unsuccessful update.

Primefaces Datatable gives null pointer exception when action triggered

I'm trying to use datatable component but using an array instead of an object inside selection property
Here is the sample from Primefaces showcase
My domain name is EfaDocLabelModel and I updated the CarDataModel(from showcase) to EfaDocLabelModel here is the code .
public EfaDocLabelModel() {
}
public EfaDocLabelModel(List<EfaDocLabelMatch> data) {
super(data);
}
#Override
public EfaDocLabelMatch getRowData(String rowKey) {
List<EfaDocLabelMatch> docLabelList = (List<EfaDocLabelMatch>) getWrappedData();
for (EfaDocLabelMatch docLabel : docLabelList) {
if (docLabel.getDoclabelId().toString().equals(rowKey)) {
return docLabel;
}
}
return null;
}
#Override
public Object getRowKey(EfaDocLabelMatch docLabel) {
return docLabel.getDoclabelId();
}
When user triggered commandbutton inside footer part of datatable ,in my managed bean
I try to get selected rows but it gives
java.lang.NullPointerException
at java.lang.reflect.Array.newArray(Native Method)
at java.lang.reflect.Array.newInstance(Array.java:70)
at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:53)
at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:39)
at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:53)
I can run the multiple selection from showcase which used CarDataModel but using with the same structure when I modify it to according my need I gat this error.
Here is my bean
public class DocLabelBean implements Serializable {
private List<EfaDocLabelMatch> docLabelMatchList;
private EfaDocLabelMatch[] docLabelArray;
public String updateLabel() {
EfaDocLabelMatch[] selectedDocArray = getDocLabelArray();//I m sure I m
//getting error because selectedDocArray is null.
DTO result = new DTO();
for (EfaDocLabelMatch docLabel : selectedDocArray) {
}
Here is my page necessary code:
<p:dataTable emptyMessage="No Labeled Doc"
var="docLabel" value="#{docLabelBean.docLabelModel}"
selection="#{docLabelBean.docLabelArray}">
<p:column selectionMode="multiple" />
<p:column headerText="Id" >
#{docLabel.doclabelId}
</p:column>
<p:column headerText="Doc No" >
#{docLabel.documentId}
</p:column>
<p:column headerText="Label Key" >
#{docLabel.docLabelKey}
</p:column>
And commanbutton code from page:
<p:commandButton value="Sil" icon="ui-icon-search"
update="#form" process="#form"
action="#{docLabelBean.updateLabel()}" />
I tried ajax=false and other possible error reason mentioned by balusc but still I couldn't resolve the problem
Here is
Bug report .I need to assign selected rows to an list or array if its a real bug is there any alternative ways ?

Primefaces component does not update

I have a p:dataGrid that used to update itself in 3.0.1. Now I upgraded to PF 3.1 and the ajax update event of the "availableIcons" component does not fire anymore. I don't get an error that the component is not found in the view.
The XHMTL
<h:form id="Application">
......
<p:confirmDialog id="iconDialog" message="Select one icon"
showEffect="bounce" hideEffect="explode" header="Icon Selection"
severity="alert" widgetVar="iconSelect" modal="false">
<p:dataGrid id="availableIcons" var="icon"
value="#{appEditController.availableIcons}" columns="4">
<p:column>
<p:panel id="pnl" header="" style="text-align:center">
<h:panelGrid columns="1" style="width:100%" id="iconPanelGrid">
<p:graphicImage value="/resources/icons/#{icon.icon}"
id="iconImage" />
<p:selectBooleanCheckbox id="iconSelector"
value="#{icon.selected}"
disabled="#{appEditController.isIconSelected(icon)}">
<p:ajax update="availableIcons" event="change"
process="availableIcons"
listener="#{appEditController.iconSelectedChanged(icon)}" />
</p:selectBooleanCheckbox>
</h:panelGrid>
</p:panel>
</p:column>
</p:dataGrid>
<p:commandButton value="Done" update="currentIcon"
action="#{appEditController.updateCurrentIcon}" ajax="false"
oncomplete="iconSelect.hide()" />
</p:confirmDialog>
.......
</h:form>
I don't see what's missing or what's incorrect.
This is the backing bean code
public void updateCurrentIcon() {
for (IconVO iconVO : availableIcons) {
if (iconVO.isSelected()) {
log.debug("CURRENT ICON IS NOW " + iconVO.getIcon());
currentIcon = iconVO;
break;
}
}
}
public void iconSelectedChanged(IconVO iconVO) {
if (iconVO == currentIcon) {
log.debug("NULLING ICON");
currentIcon = null;
} else {
log.debug("SETTING NEW ICON");
currentIcon = iconVO;
}
}
public boolean isIconSelected(IconVO iconVO) {
log.debug("IS ICON SELECTED " + iconVO.getIcon());
if (currentIcon == null
|| iconVO.getIcon().equals(currentIcon.getIcon())) {
return false;
}
return currentIcon != null;
}
I tried to do update="#form", then the update fires but it closes the modal panel completely.
Thanks,
Coen
Indeed, the way how PrimeFaces locates components by relative client ID has been changed in PrimeFaces 3.1 to adhere the UIComponent#findComponent() javadoc.
In your particular case, you need to specify the absolute client ID of the <p:dataGrid> instead. Easiest way to figure it is to check the ID of the <p:dataGrid> in the generated HTML source. With the code given so far, that would be Application:availableIcons. You need to prefix it with : to make it absolute and then reference it in update as follows:
<p:ajax update=":Application:availableIcons" ... />
Update as per the comments it turns out to not work at all. You could try wrapping the table in some invisible containing component like <h:panelGroup> and update it instead. Alternatively, you could consider moving the <h:form> into the dialog and use update="#form" instead. Having the <h:form> outside the dialog is kind of odd anyway. You surely won't submit all the other inputs which are outside the dialog inside the same form.

Mutual exclusive jsf selectOneMenu items

Suppose I have two selectOneMenu tags bound to two different lists (let's say Products and Departments).
I want to prevent choosing some Departments depending on the Product selected and vice versa. Where could I put that logic?
Not sure if this is the way you want to go about for this. But you can load the Department list base on the selection of Product. To do this you need to send Ajax request to update the Department list when you select a Product. To illustrate this, I will use PrimeFaces to send out ajax request
<h:selectOneMenu id="product" value="#{bean.selectedProduct}">
<f:selectItem itemLabel="Select Product" itemValue="" />
<f:selectItems value="#{bean.productList}"/>
<!-- here is where I send out ajax request -->
<p:ajax listener="#{bean.loadDepartmentById}" event="change" update="department" />
</h:selectOneMenu>
<h:selectOneMenu id="department" value="#{bean.selectedDepartment}">
<f:selectItem itemLabel="" itemValue="" />
<f:selectItems value="#{bean.departmentList}"/>
</h:selectOneMenu>
Here is my bean
#ManagedBean
#ViewScoped
public class bean{
private List<SelectItem> productList = null;
private List<SelectItem> departmentList = null;
private Long selectedProduct = null;
private Long selectedDepartment = null;
#PostConstruct
public void init(){
//Pre load product list
productList = new ArrayList<SelectItem>();
productList.add(new SelectItem("value1", "label1"));
productList.add(new SelectItem("value2", "label2"));
productList.add(new SelectItem("value3", "label3"));
//The values are the keys passed to the selectItem property.
//The labels are those what you see on the menu.
}
public void loadDepartmentById(){
if(selectedProduct != null){
//Load the appropriate list of department.
}
}
}

Categories