fill second primefaces selectOneMenu - jsf-2

I have two selectOneMenu. I choose item from selectOneMenu1 and I add item value to where condition for query. Query result is successful. But I didn't put query result to selectOneMenu2. selectOneMenu2 is empty everytime. I add managedBean and xhtml page code about this issue.
// BirimManagedBean about above issue
#Override
public void processAjaxBehavior(AjaxBehaviorEvent event) throws AbortProcessingException {
String birimRequested = deger;
byte birimId = Byte.parseByte(
bolumManagedBean.bolumBilgileriniGetir(birimId);
}
// BolumManagedBean about above issue
public void bolumBilgileriniGetir(byte id) {
bolumler = new ArrayList<Bolum>();
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("from Bolum b where b.birim.birim_id = :id");
query.setParameter("id", id);
bolumler = query.list();
}
// yeni_kayit.xhtml about above issue
<p:selectOneMenu id="birimi" value="#{birimMBean.deger}" style="float: left;" >
<f:selectItems value="#{birimMBean.birimler}" var="birim" itemLabel="#{birim.birim_adi}" itemValue="#{birim.birim_id}" />
<p:ajax event="change" listener="#{birimMBean.processAjaxBehavior}" />
</p:selectOneMenu>
<br/><br/>
<p:selectOneMenu id="bolumu" value="#{bolumMBean.secilenBolum}" style="float: left;">
<f:selectItems value="#{bolumMBean.bolumler}" var="bolum" itemLabel="#{bolum.bolum_adi}" itemValue="#{bolum.bolum_id}" />
</p:selectOneMenu>

add update="bolumu" to the p:ajax tag.

Related

get value of f:selectItems from view in bean

i have a selectedOneMenu i wann like to get the value selected into my java code to do some work with this value so this my xhtml:
<p:selectOneMenu id="tbName" >
<f:selectItem itemLabel="Select Table" itemValue=""/>
<f:selectItems value="#{infoTable.nameTa}" />
</p:selectOneMenu>
and for the java code i have this:
public List<SelectItem> getNameTa() {
List<SelectItem> subcat = new ArrayList<SelectItem>();
try {
ConnectionBase con = new ConnectionBase();
TableInfo tt = new TableInfo();
List<String> rs = tt.getTable(con, "%");
Iterator i = rs.iterator();
while (i.hasNext()) {
subcat.add(new SelectItem(i.next()));
}
} catch (Exception e) {
e.getStackTrace();
}
return subcat;
}
this methode get the List of the name of my table in data base so when i select item i wanna get the value to pu it here :
public List<SelectItem> getFkName2() {
List<SelectItem> subcat = new ArrayList<SelectItem>();
nameT =generatedName(); //from the selecteditem
System.out.println("name of table choice"+nameT);
try {
TableInfo tt = new TableInfo();
List<String> rs = tt.getNameCtable(con, nameT);
Iterator i = rs.iterator();
while (i.hasNext()) {
subcat.add(new SelectItem(i.next()));
}
} catch (Exception ex) {
}
return subcat;
}
to used it to make other selectOneMenu that get the column of the name of table selected. So what should i make it and thx
try to add a getter/setter of String value like this "slectedName" and for the xhtml put this:
<p:selectOneMenu id="cat">
<f:selectItem itemLabel="Select Column" itemValue="" />
<f:selectItems value="#{infoTable.getFkName2()}" />
</p:selectOneMenu>
<p:outputLabel value="Table :" />
<p:selectOneMenu id="tbName" value="#{infoTable.slectedName}" >
<f:selectItem itemLabel="Select Table" itemValue="" />
<f:selectItems value="#{infoTable.nameTa}" />
<p:ajax update="cat"></p:ajax>
</p:selectOneMenu>
I hoppe it will work for you

operations on validated text fields in jsf

I'm Using Primefaces 3.5. I have around 10 input text fields in my .xhtml page.Few text fields are made mandatory with attribute required="true".
I have a search button that displays Data from Database in a Data Table.The functionality of my page is to insert the values into these fields by on row select() the data in the Data Table of Search Button.
The Problem here is the data is inserting into the Fields which are highlighted with the red border ie fields with validations applied.
Example:
Transport Field has no validation but it had value that has to be inserted. These type of things are happening to many of my Input Fields.
Please give me some suggestions.
.xhtml file is:
<p:inputText id="email" value="#{addcust.c.email}" required="true"
validatorMessage="Enter Valid Email">
<f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"/></p:inputText>
<h:outputLabel value="Transport"></h:outputLabel>
<p:inputText value="#{addcust.c.transport}" </p:inputText>
<p:commandButton value="add" type="submit" update=":form,:msg" actionListener="#{addcust.onAddSelect}"</p:commandButton>
<p:commandButton value="Search" type="submit" onclick="ser.show() "></p:commandButton>
<p:dialog id="dialog11" header=" Search" widgetVar="ser" resizable="false" showEffect="fade"
hideEffect="explode" >
<p:dataTable id="dt" var="sd" value="#{addcust.al}" selection="#{addcust.c}">
<p:ajax event="rowSelect" update=":form" listener="#{addcust.onRowSelect}"
oncomplete="ser.hide()"/>
<p:column>
<f:facet name="header">
<h:outputText value="Email"/>
</f:facet>
<h:outputText value="#{sd.email}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Transport"/>
</f:facet>
<h:outputText value="#{sd.transport}"/>
</p:column>
</p:dataTable>
customerbean.java
public class customerbean {
private String email;
private String transport;
public String getTransport() {
return transport;
}
public void setTransport(String transport) {
this.transport = transport;
}
return email;
}
public void setEmail(String email) {
this.email = email;
}
addcust.java
public customerbean c = new customerbean();
public ArrayList<customerbean> al;
public void onAddSelect(){
// Inserted my values into customer table.
}
public void onSearchSelect() {
try {
st = con.createStatement();
ResultSet rs = st.executeQuery("select * from customer where cmpid=" + getCurrcompanyid() + "");
al = new ArrayList<customerbean>();
while (rs.next()) {
customerbean s = new customerbean();
s.setEmail(rs.getString(1));
s.setTransport(rs.getString(2));
}
} catch (Exception e) {
System.out.println(e);
}
}
public void onRowSelect(SelectEvent event) throws SQLException {
customerbean r = (customerbean)event.getObject();
c = r;
}
If I'm not clear enough please leave me a comment .Thanks for Reading.

p:ajax event="sort" of p:dataTable does not process

For some strange reason I can't manage to process my data when I'm using <p:ajax event="sort" inside <p:dataTable , while it works just fine for <p:ajax event="page" and <p:ajax event="filter"
I'm using myfaces 2.0.11 with primefaces 3.4 (tried with 4.0 snapshot)
If I edit my cell input and do PAGINATE I do see the updated value of the cell in the listener method
If i edit my cell input and do SORT I do NOT see the updated value of the cell in the listener method, Any Idea why? I mean both p:ajax (sort/page) got the process="#form" so why its does not process? I inspected the network of the chrome dev tools and in both cases (sort/page) the relevant updated values are being sent to the server, but for some reason in the sort listener the values are not updated ,
b.t.w the only suspicious difference between page and sort ajax request is the following parameter that is present only in the sort request is:
my_table_skipChildren:true , what does it mean? skip from processing ?
I can grab the relevant ids+value from the request from the ExternalContext, but it will be last resort only
Here is a really simple example
Session Scoped Bean code:
#ManagedBean
#SessionScoped
public class myBean {
private List<MyObject> myList = new ArrayList<MyObject>();
#PostConstruct
public void init() {
myList.add(new MyObject("1", "2"));
myList.add(new MyObject("11", "22"));
myList.add(new MyObject("111", "222"));
myList.add(new MyObject("1a", "2a"));
myList.add(new MyObject("11a", "22a"));
myList.add(new MyObject("111a", "222a"));
}
public List<MyObject> getMyList() {
return myList;
}
public void setMyList(List<MyObject> myList) {
this.myList = myList;
}
}
MyObject code
public class MyObject {
private String one;
private String two;
public MyObject(String one, String two) {
super();
this.one = one;
this.two = two;
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
}
XHTML :
<p:dataTable id="my_table" value="#{myBean.myList}"
var="deviceRow" widgetVar="myTable"
paginator="true" rows="4" paginatorPosition="bottom"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}">
<p:ajax process="#form" event="sort" listener="#{myBean.detectSortEvent}"></p:ajax>
<p:ajax process="#form" event="page" listener="#{myBean.detectPageEvent}"></p:ajax>
<p:column id="table_selection_column_one" sortBy="#{deviceRow.one}" filterBy="#{deviceRow.one}">
<f:facet name="header">
<h:outputText value="Column One" />
</f:facet>
<h:inputText id="myRow_One" value="#{deviceRow.one}" />
</p:column>
<p:column id="table_selection_column_two" sortBy="#{deviceRow.two}" filterBy="#{deviceRow.two}">
<f:facet name="header">
<h:outputText value="Column Two" />
</f:facet>
<h:inputText id="myRow_Two" value="#{deviceRow.two}" />
</p:column>
</p:dataTable>
(I also asked it on primefaces forum but got no response)
Tried Andy workaround (adding editable="true" to table, but its no good
The scenario is as follows: change some value of an input text , than click on table column header to do sort
Expectations: Inside detectSortEvent listener I see the updated value inside the list
Reality: Same old value in the list
Regards,
Daniel.

Getting selected value of a SelectOneMenu

I'm testing the component "SelectOneMenu" on a jsf page. I'm populating this component dinamically though my ManageBean (that will get all Animals from database).
I would like to know if is possible to see the user selected item of that "SelectOneMenu" (combobox), I'm trying with value="#{animalsManage.animalSelect}" but it is only called on the beginning of the page. Also, I'm using an inputText to see the value of the selected intem of the "SelectOneMenu".
What I'm doing wrong?
JSF:
<body>
<ui:component>
<h:form>
<h:outputText value="Select one Mets File" />
<h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
<f:selectItem itemLabel="Select..." noSelectionOption="true"/>
<f:selectItems value="#{animalsManage.allAnimals}" />
</h:selectOneMenu>
<h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
</h:form>
</ui:component>
</body>
ManageBean:
#ManagedBean
#ViewScoped
public class AnimalsManage implements Serializable {
#EJB
private AnimalsFacadeREST animalsFacadeREST;
private String animalSelected;
private List< SelectItem> selectAnimals;
public List<SelectItem> getAllAnimals() {
List<Animals> al = animalsFacadeREST.findAll();
selectAnimals = new ArrayList< SelectItem>();
int i = 0;
for (Animals animal: al) {
selectAnimals.add(new SelectItem(i, animal.getName()));
i++;
}
return selectAnimals;
}
public String getAnimalSelected() {
return animalSelected;
}
public void setAnimalSelected(String animalSelected) {
this.animalSelected = animalSelected;
}
}
There are many solutions to the presented problem. I present here two basic ideas.
Server-side solution. Simply attach <f:ajax> tag inside your <h:selectOneMenu> to update selected values and rerender user's choice, like in
<h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
<f:selectItem itemLabel="Select..." noSelectionOption="true"/>
<f:selectItems value="#{animalsManage.allAnimals}" />
<f:ajax execute="combo" render="textbox" />
</h:selectOneMenu>
<h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
If you like, you may also do some custom logic with selected element in ajax listener by specifying listener="#{animalsManage.performCustomAjaxLogic}" of <f:ajax> tag.
Client-side solution. Simply update element with id="textbox" on basic change event. So, if you use jQuery the solution will be
$('#combo').change(function() {
$('#textbox').val($('#combo').val());
});
Thought the client-side solution will bind only text value of your input component.

javax.el.ELException: Can't set property 'propertyName' on class 'com.example.MrBean' to value 'null'

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

Resources