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
Related
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.
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'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.
I implemented code that allows me to display a dropdown which depends on another. Everything works fine except when I try to retrieve and display the value of the two fields, it raises the following error:
form:parcours : erreur : de validation. la valeur est incrorrecte
in english :
form:parcours : validation error. value is not valid
I am using JSF 2.0, EJB 3.0, JPA 2.0 and PrimeFaces 3.2.
View:
<h:form id="form" >
<!-- <p:growl id="msgs" showDetail="true"/> -->
<h:messages globalOnly="true"/>
<p:growl id="msgs" showDetail="true" />
<p:panel header="Double Combo" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:selectOneMenu id="countries" value="#{plansEtude.selectedDep}">
<f:selectItem itemLabel="Select Country" itemValue="" />
<f:selectItems value="#{plansEtude.depList}" var="c" itemLabel="#{c.nomDepFr}" itemValue="#{c.id}"/>
<p:ajax update="parcours,parcoursTab"
listener="#{plansEtude.handleDepChange}" />
</p:selectOneMenu>
<p:selectOneMenu id="parcours" value="#{plansEtude.selectedParcours}" >
<f:convertNumber maxFractionDigits="0"/>
<f:selectItem itemLabel="Select City" itemValue="" />
<f:selectItems value="#{plansEtude.parcoursList}" var="ct" itemLabel="#{ct.designParcours}" itemValue="#{ct.id}" />
</p:selectOneMenu>
</h:panelGrid>
<p:separator />
<p:commandButton value="Submit" update="msgs" actionListener="#{plansEtude.displayLocation}" id="btnSubmit"/>
</p:panel>
Controller :
#EJB
private DepartementFacade departementFacade;
#EJB
private ParcoursFacade parcoursFacade;
private List<Departement> depList;
private List<Parcours> parcoursList;
private Integer selectedDep;
private Integer selectedParcours;
public PlansEtude() {
}
public DepartementFacade getDepartementFacade() {
return departementFacade;
}
public void setDepartementFacade(DepartementFacade departementFacade) {
this.departementFacade = departementFacade;
}
public ParcoursFacade getParcoursFacade() {
return parcoursFacade;
}
public void setParcoursFacade(ParcoursFacade parcoursFacade) {
this.parcoursFacade = parcoursFacade;
}
public List<Departement> getDepList() {
depList = getDepartementFacade().findAll();
return depList;
}
public void setDepList(List<Departement> depList) {
this.depList = depList;
}
public List<Parcours> getParcoursList() {
return parcoursList;
}
public void setParcoursList(List<Parcours> parcoursList) {
this.parcoursList = parcoursList;
}
public Integer getSelectedDep() {
return selectedDep;
}
public void setSelectedDep(Integer selectedDep) {
this.selectedDep = selectedDep;
}
public Integer getSelectedParcours() {
return selectedParcours;
}
public void setSelectedParcours(Integer selectedParcours) {
this.selectedParcours = selectedParcours;
}
public void handleDepChange(){
if(selectedDep !=null && !selectedDep.equals(""))
parcoursList = parcoursFacade.findParcoursInDep(selectedDep);
else
parcoursList = new ArrayList<Parcours>();
}
public void handleParcoursChange(){
}
public void displayLocation() {
String monMessage="Departement :" + selectedDep + ", Parcours : " + selectedParcours;
FacesMessage msg = new FacesMessage("Selected", monMessage);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
parcoursFacade :
public List<Parcours> findParcoursInDep(Integer dep){
Query query = em.createNamedQuery("Parcours.findParcoursInDep");
query.setParameter("dep", dep);
return (List<Parcours>)query.getResultList();
}
Named query :
#NamedQuery(name = "Parcours.findParcoursInDep", query = "SELECT p FROM Parcours p WHERE p.departementid.id = :dep"),
Remove the <f:convertNumber maxFractionDigits="0"/> from your parcours dropdown. It makes no sense. It would only convert the number to BigDecimal while you need an Integer.
Another possible cause is that the #{plansEtude.parcoursList} has incompatibly changed during the form submit because the managed bean is request scoped. You need to make sure that the managed bean is placed in at least the view scope, so that the parcoursList is preserved for the submit.
I face with situation, when item selection from does not change bean property. Method setCurrentOrg() does not invoked.
Managed bean code is:
#ManagedBean(name = "requestAccessBean")
#RequestScoped
public class RequestAccessSection {
private List<AccessRight> accessList;
private List<OrgUnit> orgList;
private String currentOrg;
public String getCurrentOrg() {
return this.currentOrg;
}
public void setCurrentOrg(String currentOrg) {
this.currentOrg = currentOrg;
}
public List<AccessRight> getAccessList() {
if (this.accessList == null) {
this.accessList = returnAccessList();
}
return this.accessList;
}
public void setAccessList(List<AccessRight> accessList) {
this.accessList = accessList;
}
public List<OrgUnit> getOrgList() {
if (this.orgList == null) {
this.orgList = returnOrgList();
}
return this.orgList;
}
public void setOrgList(List<OrgUnit> orgList) {
this.orgList = orgList;
}
public List<OrgUnit> returnOrgList() {
List<OrgUnit> orgList = new ArrayList<OrgUnit>();
orgList = getOfficeBranches();
return orgList;
}
public List<AccessRight> returnAccessList() {
List<AccessRight> accessList = new ArrayList<AccessRight>();
accessList = getAccessList();
return accessList;
}
}
Page is:
<h:form>
<h:selectOneMenu id="orgList" value="# {requestAccessBean.currentOrg}">
<f:selectItem itemLabel="--select--" itemValue="null"></f:selectItem>
<f:selectItems value="#{requestAccessBean.orgList}"
var="org" itemLabel="#{org.ou}" itemValue="#{org.globalid}"/>
<f:ajax event="change" execute="#this" render="accessTable"/>
</h:selectOneMenu>
<h:dataTable var="access" value="#{requestAccessBean.accessList}"
binding="#{requestAccessBean.htmlDataTable}" id="accessTable">
<h:column>
<h:selectOneRadio onclick="radioButton(this);" id="selectAccess"
valueChangeListener="#{requestAccessBean.setSelected}">
<f:selectItem itemValue="null" />
</h:selectOneRadio>
</h:column>
<h:column>
<h:outputText value="#{access.title}" />
</h:column>
</h:dataTable>
<h:form>
Could you please help me?
<h:selectOneMenu id="orgList" value="# {requestAccessBean.currentOrg}">
This is not valid EL. You have a space between # and {. Remove it.
<h:selectOneMenu id="orgList" value="#{requestAccessBean.currentOrg}">