thanks for any suggestions.
I i'm trying to use primefaces component when i select element and proceed getting null pointer exception due to contractid, which can not be null because i have already initiated this.
Here is the code.
<h:selectOneMenu id="ContractName" value="#{managedContract.c.contract_id}" style="width: 300px;" required="true" requiredMessage="Please Select Contract.">
<f:selectItem itemValue="" itemLabel="-Select Contract-" noSelectionOption="true" itemDisabled="true"></f:selectItem>
<f:selectItems value="#{managedContract.contracts}" var="contractvar" itemLabel="#{contractvar.contract_name}" itemValue="#{contractvar.contract_id}" />
</h:selectOneMenu>
public class ManagedContract{
private int contractid;
public managedContract(){
this.contractid=0;
//getter
//setter
//....
}
romove var="contractvar" itemLabel="#{contractvar.contract_name}" itemValue="#{contractvar.contract_id}" from <f:selectItems>
<h:selectOneMenu id="ContractName" value="#{managedContract.c.contract_id}" style="width: 300px;" required="true" requiredMessage="Please Select Contract.">
<f:selectItem itemValue="" itemLabel="-Select Contract-" noSelectionOption="true" itemDisabled="true"></f:selectItem>
<f:selectItems value="#{managedContract.contracts}" />
</h:selectOneMenu>
and in your bean you must have getContracts() method
#ManagedBean
#ViewScoped
public class ManagedContract imlements java.io.Serializable{
private int contractid;
public managedContract(){
this.contractid=0;
}
//getter
//setter
//....
}
Related
I have a h:selectOneMenu and a p:calendar which may be disabled depending on the select item chosen.
The problem is : if I first select the item value b, click on the calendar to choose a date different than today, select the item value c, then the date is reset to initial value. How can I keep the selected date ?
<h:selectOneMenu value="#{mb.selectedTypeMessage}" style="min-width: 250px;">
<f:selectItem id="type1" itemLabel="a" itemValue="a" />
<f:selectItem id="type2" itemLabel="b" itemValue="b" />
<f:selectItem id="type3" itemLabel="c" itemValue="c" />
<f:ajax render="button" />
</h:selectOneMenu>
<p:calendar id="button" value="#{mb.selectedIncidentDate}" disabled="#{mb.selectedTypeMessage eq a}" showOn="button"/>
#ManagedBean(name = "mb")
#ViewScoped
public class MessageBean implements Serializable {
private String selectedTypeMessage;
private String selectedSector;
private Date selectedIncidentDate;
#PostConstruct
public void init() {
this.selectedIncidentDate = new Date();
this.selectedTypeMessage = "a";
}
...
}
I believe you need a p:ajax on the calendar. That should update the value on the bean.
<p:calendar ...>
<p:ajax event="dateSelect" />
</p:calendar>
I m using JSF-2 and I want to show an Outputtext when changes h:selectOneMenu value to 'A' , but it doesnt work :
Here is the view:
<p:column>
<p:selectOneMenu id="type"
value="#{Controller.typeR}" style="width:100px;">
<f:selectItem itemLabel="--Selectionner--" itemValue="-1" />
<f:selectItem itemLabel="A" itemValue="1" />
<f:selectItem itemLabel="B" itemValue="2" />
<f:selectItem itemLabel="C" itemValue="3" />
<p:ajax update="test"
listener="#{Controller.handleTypeChange}" />
</p:selectOneMenu>
</p:column>
<p:column>
<h:outputText id ="test" value="A OK :" rendered="#{Controller.typeAOk}" />
</p:column>
the managed bean
#SuppressWarnings("serial")
#ManagedBean(name = "Controller")
#ViewScoped
public class NoIe{
public void handleTypeChange(){
if (typeR.equals("1")) {
setTypeAOk(true);
System.out.print(typeAOk);
}}
//Getter and Setter
Any help will be greatly appreciated!
As #{Controller.typeAOk} seems to be false your outputText won't be part of the resulting html page and thus, it won't be available for updating.
In cases like this, you will need to wrap your outputText inside another component and then update that component which is always rendered. Here is an example:
<p:column>
<p:outputPanel id="test">
<h:outputText value="A OK :" rendered="#{Controller.typeAOk}" />
</p:outputPanel>
</p:column>
Once again: Only rendered components can be updated.
Here is the full code I used to test the solution (note that you can use a panelGroup too):
<h:form>
<p:dataTable value="#{viewMBean.list}" var="l">
<p:column>
<p:selectOneMenu id="type" value="#{viewMBean.id}" style="width:100px;">
<f:selectItem itemLabel="--Selectionner--" itemValue="-1" />
<f:selectItem itemLabel="A" itemValue="1" />
<f:selectItem itemLabel="B" itemValue="2" />
<f:selectItem itemLabel="C" itemValue="3" />
<p:ajax update="test" />
</p:selectOneMenu>
</p:column>
<p:column>
<h:panelGroup id ="test">
<h:outputText value="A OK :" rendered="#{viewMBean.id eq 1}" />
</h:panelGroup>
</p:column>
</p:dataTable>
</h:form>
The ManagedBean
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class ViewMBean implements Serializable {
private Integer id;
private List<SimpleBean> list;
#PostConstruct
public void setup() {
list = new ArrayList<SimpleBean>();
list.add(new SimpleBean(11, "A"));
list.add(new SimpleBean(22, "B"));
list.add(new SimpleBean(33, "C"));
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<SimpleBean> getList() {
return list;
}
}
I am facing problem in resetting the p:selectOneMenu. In my facelet, I have two p:selectOneMenu items. My requirement is that, if user chooses something from first p:selectOneMenu, then second p:selectOneMenu should reset itself and vice versa.
Below is the code I am using:
<p:outputLabel for="country" value="Country:" />
<p:selectOneMenu id="country" effect="none"
value="#{infoBean.infoDataHolder.selectedCountry}">
<f:selectItem itemLabel="Select One" itemValue=""
noSelectionOption="true" />
<f:selectItems
value="#{infoBean.infoDataHolder.availableCountries}"
var="aCountry" itemLabel="#{aCountry.description}"
itemValue="#{aCountry.description}" />
<p:ajax update="state"
listener="#{infoBean.resetState()}" />
</p:selectOneMenu>
<p:outputLabel for="state" value="State:" />
<p:selectOneMenu id="state" effect="none"
value="#{infoBean.infoDataHolder.selectedState}">
<f:selectItem itemLabel="Select One" itemValue=""
noSelectionOption="true" />
<f:selectItems
value="#{infoBean.infoDataHolder.availableStates}"
var="aState" itemLabel="#{aState}"
itemValue="#{aState}" />
<p:ajax update="country"
listener="#{infoBean.resetCountry()}" />
</p:selectOneMenu>
My backing bean InfoBean is in RequestScope and infoDataHolder is in View Scope.
In infoBean.resetCountry() / infoBean.resetState(), I am making the infoBean.infoDataHolder.selectedCountry / infoBean.infoDataHolder.selectedState as null.
Now what is happening is that when I am choosing State, Country p:selectOneMenu is getting resetted. but choosing country, State p:selectOneMenu is not getting resetted.
Could you please help me here. Thanks.
You may wanted to update parent component of select one menu like
<p:panel id="panel_">
<p:selectOneMenu id="country" ...
<p:ajax update="panel_" listener="#{infoBean.resetState()}" />
</p:selectOneMenu>
<p:selectOneMenu id="state" ...
<p:ajax update="panel_" listener="#{infoBean.resetCountry()}" />
</p:selectOneMenu>
</p:panel>
I can't see the point of your requirement of reseting the country list when selecting an state. The proper behaviour here is, in my opinion, allowing the end user to choose an State inside each country. That's done by loading the related states for each country and rendering the dependent h/p:selectOneMenu.
I don't encourage you to use two different beans for that, just go with #ViewScoped. Also accessing transient JSF managed beans from the view the way you do (#{infoBean.infoDataHolder}) does not make sense in JSF, just access to a bean directly.
Here you've got my workaround:
#ManagedBean
#ViewScoped
public class InfoDataHolder {
private List<String> availableCountries = Arrays.asList("USA",
"Switzerland");
private List<String> availableStates = new ArrayList<String>();
private String selectedCountry;
private String selectedState;
public void countrySelected() {
if ("USA".equals(selectedCountry)) {
availableStates = Arrays.asList("Arizona", "California");
} else if ("Switzerland".equals(selectedCountry)) {
availableStates = Arrays.asList("Zurich", "Bern");
} else {
availableStates = new ArrayList<String>();
}
}
public List<String> getAvailableCountries() {
return availableCountries;
}
public List<String> getAvailableStates() {
return availableStates;
}
public String getSelectedCountry() {
return selectedCountry;
}
public String getSelectedState() {
return selectedState;
}
public void setSelectedCountry(String selectedCountry) {
this.selectedCountry = selectedCountry;
}
public void setSelectedState(String selectedState) {
this.selectedState = selectedState;
}
}
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head />
<h:body>
<h:form>
<h:selectOneMenu value="#{infoDataHolder.selectedCountry}">
<f:selectItem noSelectionOption="true" itemLabel="Choose a Country" />
<f:selectItems var="country"
value="#{infoDataHolder.availableCountries}" itemValue="#{country}" />
<f:ajax listener="#{infoDataHolder.countrySelected}"
render="state_selection" />
</h:selectOneMenu>
<h:selectOneMenu value="#{infoDataHolder.selectedState}"
id="state_selection">
<f:selectItem noSelectionOption="true" itemLabel="Choose an State" />
<f:selectItems value="#{infoDataHolder.availableStates}" var="state"
itemValue="#{state}" />
</h:selectOneMenu>
</h:form>
</h:body>
</html>
See also:
h:selectOneMenu wiki
I am trying to use a <p:dataTable> to display the contents of an object selected from a <p:selectOneMenu> list.
The <p:selectOneMenu> is correctly populated with options. However:
The <p:dataTable> is not updated when I select an item from the <p:selectOneMenu>
The server log indicates that the value of #{MB.selectedDatabase} is null.
#{MB.setSelectedDatabase()} is never invoked.
XHTML:
<h:form id="form">
<h:messages id="messages" />
<p:panelGrid columns="2">
<p:outputLabel value="Data:" />
<p:selectOneMenu immediate="true" id="customDatas" value="#{MB.selectedDatabase}" >
<p:ajax execute="customDatas" render="infos" listener="#{MB.whatIsSelected()}" />
<f:selectItem itemLabel="Select Data" itemValue="" />
<f:selectItems value="#{MB.datas}" var="data" itemLabel="#{data.name}" itemValue="#{data.id}"/>
</p:selectOneMenu>
</p:panelGrid>
<p:dataTable value="#{MB.selectedDatabase.infos}" var="item" id="infos" >
<p:column >
<f:facet name="header">
<h:outputText value="Info"/>
</f:facet>
<h:outputText value="#{item.info}"/>
</p:column>
</p:dataTable>
</h:form>
Partial code from my sessionScoped Bean:
public Data selectedDatabase;
public void whatIsSelected(){
logger.log(Level.SEVERE, "Selected data is : {0}", selectedDatabase.getName());
infoBusinessLocal.getinfosbydata(selectedDatabase);
}
public Data getSelectedDatabase() {
return selectedDatabase;
}
public void setSelectedDatabase(Data selectedDatabase) {
logger.log(Level.SEVERE, "Set selectedDB is invoked");
this.selectedDatabase = selectedDatabase;
}
SelectOneMenu with Pojo classes is needed a JSf converter between page and managed bean. Converter converts string from page to objects in managed beans.
#FacesConverter("dataConverter")
public class DataConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
//return proper object, value is the string on the page
}
#Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
//return the string value of object which will be presented on the page
}
}
Add converter attribute to SelectOneMenu
<p:selectOneMenu immediate="true" id="customDatas" value="#{MB.selectedDatabase}" converter="dataConverter">
See also
BalusC SelectOneMenu tutorial
In my .xhtml file, I have the following SelectOneMenu component:
<ui:define name="formContent">
<h:selectOneMenu value="#{mrBean.itemCategoryID}">
<f:ajax render="abc def" execute="#this" listener="#{mrBean.getListOfItems}"></f:ajax>
<f:selectItem itemLabel="Choose one .." itemValue="0" noSelectionOption="true" />
<f:selectItems value="#{mrBean.itemCategories}" var="ic"
itemLabel="#{ic.name}" itemValue="#{ic.id}" />
</h:selectOneMenu>
<h:panelGrid id="abc" columns="3" border="1">
<h:outputText style="font-weight: bold" value="Name"/>
<h:outputText style="font-weight: bold" value="Producer" />
<h:outputText />
</h:panelGrid>
<h:panelGroup id="def" >
<ui:repeat value="#{mrBean.items}" var="i">
<h:form id="BuyItemForm">
<h:panelGrid columns="3" border="1">
<h:outputText style="font-weight: normal" value="#{i.name}" />
<h:outputText style="font-weight: normal" value="#{i.producer.name}" />
<h:commandButton value="Buy" actionListener="#{mrBean.buyItem}" >
<f:param name="itemID" value="#{i.id}" />
</h:commandButton>
</h:panelGrid>
</h:form>
</ui:repeat>
</h:panelGroup>
</ui:define>
When I open the page, it can load normally with the menu populated properly. However, when I choose 1 of the option, I ran into the following error:
SEVERE: javax.faces.component.UpdateModelException: javax.el.ELException: /partner/BuyItem.xhtml #53,81 value="#{mrBean.itemCategoryID}": Can't set property 'itemCategoryID' on class 'managedBean.MrBean' to value 'null'.
...
Caused by: javax.el.ELException: /partner/BuyItem.xhtml #53,81 value="#{mrBean.itemCategoryID}": Can't set property 'itemCategoryID' on class 'managedBean.MrBean' to value 'null'.
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
at javax.faces.component.UIInput.updateModel(UIInput.java:818)
... 47 more
Caused by: java.lang.IllegalArgumentException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.setValue(BeanELResolver.java:381)
at com.sun.faces.el.DemuxCompositeELResolver._setValue(DemuxCompositeELResolver.java:255)
at com.sun.faces.el.DemuxCompositeELResolver.setValue(DemuxCompositeELResolver.java:281)
at com.sun.el.parser.AstValue.setValue(AstValue.java:197)
at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:286)
at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
... 48 more
EDIT: this is my bean with getListOfItems function:
#ManagedBean
#ViewScoped
public class MrBean {
#EJB
private PartnerBeanLocal partnerBean;
#ManagedProperty(value="0")
private long itemCategoryID;
private List<ItemState> items;
...
public void getListOfItems() {
try {
System.out.println(itemCategoryID); // I never saw this line printed out
ArrayList data = partnerBean.getInfo(Constants.GET_LIST_OF_ITEMS, itemCategoryID);
int result = ((Integer) data.get(0)).intValue();
if (result == Constants.STATUS_SUCCESSFUL) items = (List<ItemState>) data.get(1);
else if (result == Constants.STATUS_NOT_FOUND) FacesContext.getCurrentInstance().getExternalContext().redirect("HomePage.xhtml");
} catch (IOException ex) {
Logger.getLogger(MrBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
...
// Getters and Setters
...
public long getItemCategoryID() {
return itemCategoryID;
}
public void setItemCategoryID(long itemCategoryID) {
this.itemCategoryID = itemCategoryID;
}
public List<ItemState> getItems() {
return items;
}
public List<ItemState> setItems(List<ItemState> items) {
this.items = items;
}
...
}
I'd be very grateful if someone could give me an advice on how to tackle this problem.
EDIT 2: Thanks everyone for helping me! The problem was that I stupidly forgot to put the <f:ajax> tag inside a <h:form> tag.
Here is what I tried:
#ManagedBean
#ViewScoped
public class MrBean implements Serializable {
#ManagedProperty(value="0")
private long itemCategoryID;
private List<ItemCategory> itemCategories;
#PostConstruct
public void init() {
this.itemCategories = new ArrayList<ItemCategory>();
this.itemCategories.add(new ItemCategory("Item1", 1));
this.itemCategories.add(new ItemCategory("Item2", 2));
this.itemCategories.add(new ItemCategory("Item3", 3));
}
public void getListOfItems() {
System.out.println(Thread.currentThread().getStackTrace()[1]);
System.out.println("itemCategoryID: " + this.itemCategoryID);
}
public List<ItemCategory> getItemCategories() {
return itemCategories;
}
public void setItemCategories(List<ItemCategory> itemCategories) {
this.itemCategories = itemCategories;
}
public long getItemCategoryID() {
return itemCategoryID;
}
public void setItemCategoryID(long itemCategoryID) {
this.itemCategoryID = itemCategoryID;
}
}
with:
<h:form>
<h:selectOneMenu value="#{mrBean.itemCategoryID}">
<f:ajax execute="#this" listener="#{mrBean.getListOfItems}"></f:ajax>
<f:selectItem itemLabel="Choose one .." itemValue="0" noSelectionOption="true" />
<f:selectItems value="#{mrBean.itemCategories}" var="ic"
itemLabel="#{ic.name}" itemValue="#{ic.id}" />
</h:selectOneMenu>
</h:form>
And it works without any problem.
Might be the version of JSF (EL) you have, or you have problem somewhere else.
The <f:ajax> tag need to be wrapped inside a <h:form> tag.
Seems like some of your item values are null. If you want to accept null, use Long instead of long.
I had the same issue and it was resolved by changing my variable type boolean to Boolean