How to get the value of <p:selectOneMenu> in Bean - jsf

I'm trying to get value of selectOneMenu from primefaces to Bean. it returns the old value not the new.
ManagedBean
public void doSetPrivilege( AjaxBehaviorEvent event)
{
SelectOneMenu selectOneMenu = (SelectOneMenu) event.getSource();
String value = selectOneMenu.getValue().toString();
System.out.println(value);
}
I get property privilegeApp from database
public Privilege getPrivilegeApp() {
TreeTable treeTable = new TreeTable();
treeTable = (TreeTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(":idform:apps");
data = treeTable.getRowNode();
if (data.getType().equals("type1")) {
Application app = applicationService.loadApplicationByDesignation(data.getData().toString());
privilegeApp = privilegeService.getPrivilegeApplicationUtilisateur(getUtilisateur().getIdUtilisateur(),
app.getIdApplication());
}
return privilegeApp;
}
xhtml
<p:treeTable id="apps" value="#{treeTableTest3.root}" var="app" selectionMode="single" selection="#{treeTableTest3.selectedNode}" >
<p:column headerText="Designation" >
<h:outputText value="#{app}" />
</p:column>
<p:column headerText="Etat" >
<center>
<p:selectOneMenu value="#{treeTableTest3.privilegeApp.etat}" id="etat" style="width:100px; size:30px; float:center;">
<f:selectItem itemLabel="::Etat::" itemValue="#{null}"/>
<f:selectItems value="#{treeTableTest3.etats}"
var="etat"
itemValue="#{etat}" >
</f:selectItems>
<p:ajax event="change" listener="#{treeTableTest3.doSetPrivilege}" process="#all"/>
</p:selectOneMenu>
</center>
</p:column>
</p:treeTable>
Thank's for your help

Related

How to hide/show selectOneMenu in JSF? [duplicate]

This question already has an answer here:
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 7 years ago.
I have an inputText and a selectOneRadio component, when form load selectOneRadio should be hidden. When user select inputText, I want to show the selectOneRadio component. I have made the selectOneRadio hidden in #PostConstruct, which should be display on select inputText.
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true">
<p:ajax event="onselect" update="city" listener="#{userBean.showName}" />
</p:inputText>
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3" rendered="#{userBean.displayName}" >
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
The bean code is like:
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName() {
return true;
}
...
But some how this is not working. I'm using JSF2.0 with primefaces 5.2.
I found the solution.
<h:form id="frm">
<h:panelGrid id="panelgrid">
<p:panel id="panel" >
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{userBean.name}" immediate="true" onkeypress="#{userBean.showName}">
<p:ajax event="change" update="frm:panelgrid" listener="#{userBean.inputcangeeListener}" />
</p:inputText>
</p:panel>
<p:panel id="p2" rendered="#{userBean.displayName}">
<p:selectOneRadio id="city" value="#{userBean.city}" layout="grid" columns="3">
<f:selectItems value="#{userBean.cities}" var="c" itemLabel="#{city}" itemValue="#{city}" />
</p:selectOneRadio>
</p:panel>
</h:panelGrid>
</h:form>
And my managed bean looks like-
#PostConstruct
public void init() {
displayName = false;
}
public boolean isShowName(){
return displayName;
}
public void inputcangeeListener(javax.faces.event.AjaxBehaviorEvent changeEvent){
setDisplayName(true);
cities = new ArrayList<>();
cities.add("pune");
cities.add("KOL");
}
Thanks for your response.

h:selectOneMenu value should change onclick of p:selectBooleanCheckbox

I have a problem in my application.
I have a datatable, inside that one selectBooleancheckbox , selectonemenu with items("Confirmed", "Rejected" ,"selected") and one inputText is there. I need to write code for,
if selectbooleancheckbox is checked then the selectonemenu will get the value "Selected" and the inputText also become editable.
this is my xhtml code:
<p:dataTable id="panel1"
rendered="true"
var="step2table"
value="#{dtstep2_tab1.step2table}"
editMode="cell"
scrollable="true"
selection="#{dtstep2_tab1.selectedrows}"
rowKey="#{step2table.rs2_empname}"
rowSelectMode="checkbox"
rowStatePreserved="true">
<p:column headerText="Select" id="hSelect" style="font-size:12px;width:60px;">
<p:selectBooleanCheckbox id="Booleanchkbox" >
<p:ajax listener="#{dtstep2_tab1.UpdateStatus}" update="panel1" />
</p:selectBooleanCheckbox>
</p:column>
<p:column headerText="Status" id="hStatus" style="font-size:12px;width:100px;">
<h:selectOneMenu id="cbo" value="#{step2table.rs2_status}" >
<f:selectItem itemLabel="Confirmed" itemValue="Confirmed"></f:selectItem>
<f:selectItem itemLabel="Selected" itemValue="Selected"></f:selectItem>
<f:selectItem itemLabel="Rejected" itemValue="Rejected"></f:selectItem>
</h:selectOneMenu>
</p:column>
<p:column style="font-size:12px;width:150px;">
<f:facet name="header">
<h:outputText value="Comments"></h:outputText>
</f:facet>
<h:inputText value="#{step2table.rs2_comments}" rendered="#{dtstep2_tab1.editable}"/>
</p:column>
</p:dataTable>
and in bean I have the following code in addition with getter and setter method.
public void UpdateStatus(AjaxBehaviorEvent event) {
SelectBooleanCheckbox permit = (SelectBooleanCheckbox) event.getComponent();
boolean checked = (Boolean) permit.getValue();
if (checked) {
setRs2_status("Selected");
setEditable(true);
setSelect(true);
}
else
{
setEditable(false);
setSelect(false);
}
}
I don't know what I am doing wrong here. Please guide me on the issue.

Data Table Primefaces

I'm working with JSF Primefaces and I want to make a search and show results in but results won't show, I'm doing this with Result type and had implemented like this in JSF 2.1 whith MyFaces tomahawk, but now with primefaces this doesn't work, here is my code
<h:form id="buscar_sucursal">
<p:panel style="width: 800px">
<h:panelGrid columns="5">
<h:outputText value="Criterio: " />
<p:selectOneMenu id="citerio" value="#{SucursalesBean.criterio}">
<f:selectItem itemValue="1" itemLabel="CVE SUCURSAL"></f:selectItem>
<f:selectItem itemValue="2" itemLabel="NOMBRE"></f:selectItem>
</p:selectOneMenu>
<h:outputText value="Valor: " />
<p:inputText id="valor" value="#{SucursalesBean.valor}"></p:inputText>
<p:commandButton action="#{SucursalesBean.buscar}" value="Buscar"></p:commandButton>
</h:panelGrid>
</p:panel>
<p:dataTable id="sucursalesTable" var="suc" value="#{SucursalesBean.sucursalesTable}">
<p:column headerText="Id">
<h:outputText value=""></h:outputText>
</p:column>
<p:column headerText="Cve">
<h:outputText value="#{suc.cve_sucursal}"></h:outputText>
</p:column>
<p:column headerText="Sucursal">
<h:outputText value="#{suc.sucursal}"></h:outputText>
</p:column>
<p:column headerText="Domicilio">
</p:column>
<p:column headerText="ContraseƱa">
</p:column>
<p:column headerText="Accion">
<h:commandLink value="Editar"></h:commandLink> /
<h:commandLink value="Eliminar"></h:commandLink>
</p:column>
</p:dataTable>
</h:form>
And the code of my ManagedBean
public Result getSucursalesTable()
{
return this.sucursalesTable;
}
public void setSucursalesTable(Result sucursalesTable)
{
this.sucursalesTable = sucursalesTable;
}
public void buscar() throws ClassNotFoundException
{
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
this.valor = request.getParameter("buscar_sucursal:valor");
try
{
LoginBean loginBean = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("LoginBean");
Connection conn = loginBean.getConectar_db();
//int id_empresa = loginBean.getId_empresa();
ArrayList<SucursalesBean> al = new ArrayList<SucursalesBean>();
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
//ResultSet rs = st.executeQuery("SELECT *FROM clientes WHERE id_cliente='"+request.getParameter("id_cliente")+"';"); // AND id_contribuyente='' AND id_empresa=''
//if(request.getParameter("sucursales:id_sucursal") != null)
//rs = st.executeQuery("SELECT *FROM sucursales WHERE id_sucursal='"+request.getParameter("sucursales:id_sucursal")+"' AND id_empresa='"+loginBean.getId_empresa()+"' AND id_contribuyente='"+loginBean.getId_contribuyente()+"';"); // AND id_contribuyente='' AND id_empresa=''
if(getCriterio().toString() != null)
{
if(getCriterio().toString().equals("1"))
rs = st.executeQuery("SELECT cve_sucursal,sucursal FROM sucursales WHERE cve_sucursal='"+this.valor+"' AND id_empresa='"+loginBean.getId_empresa()+"' AND id_contribuyente='"+loginBean.getId_contribuyente()+"';");
if(getCriterio().toString().equals("2"))
rs = st.executeQuery("SELECT *FROM sucursales WHERE sucursal LIKE '%"+this.valor+"%' AND id_empresa='"+loginBean.getId_empresa()+"' AND id_contribuyente='"+loginBean.getId_contribuyente()+"';");
}
this.sucursalesTable = ResultSupport.toResult(rs);
//this.sucursalesTable = rs;
//setSucursalesTable(ResultSupport.toResult(rs));
/*while(rs.next())
{
//out.println("<tr><td>"+rs.getString("sucursal")+"</td><td>"+rs.getString("cve_sucursal")+"</td><td>"+rs.getString("telefono")+"</td><td>"+rs.getString("e_mail")+"</td><td>"+rs.getString("calle")+"</td><td><a href='sucursales.jsp?accion=editar&id_sucursal="+rs.getString("id_sucursal")+"'>Editar</a></td><td></td></tr>");
//setId_sucursal(String.valueOf(rs.getInt("id_sucursal")));
setCve_sucursal(rs.getString("cve_sucursal"));
setSucursal(rs.getString("sucursal"));
al.add(this);
}
this.sucursalesTable = al;*/
}
catch(SQLException ex)
{
}
}
I don't want to use serialized objects in order to use List type because I think it's to much code for doing something that supposed to be simple, even if this it's not the best aproach.
My doubt it's if Primefaces has support for Result type or am I lost with this and maybe that's why this aproach isn't working.
Any help I really appreciate thank in advance
It looks to me as you've converted a h:commandButton to p:commandButton. The first by default uses non-ajax request, but the latter uses an ajax request. If correct, you just need to tell the button which components to process during and update after the request:
<p:commandButton action="#{SucursalesBean.buscar}"
value="Buscar"
process="criterio valor"
update="sucursalesTable" />
Or use a non-ajax request:
<p:commandButton action="#{SucursalesBean.buscar}"
value="Buscar"
ajax="false" />

p:selectOneMenu value not set in bean

In p:selectOneMenu loginbranchName value is not set in bean.In selectItems having list of branchName.I'm using listener to select value from list but value not set in getLoginBranchId method....
<h:outputLabel value="Branch Name:*" style="text-align: left;display: block;" rendered="#{loginBean.userLoggedIn}"/>
<p:selectOneMenu value="#{loginBean.loginbranchName}"
rendered="#{loginBean.userLoggedIn}"
style="width:175px;">
<f:selectItem itemLabel="Select" itemValue="0" />
<f:selectItems value="#{loginBean.branchName}" />
<p:ajax event="change" listener="#{loginBean.getLoginBranchId}"/>
</p:selectOneMenu>
login.java
public String getLoginbranchName() {
return loginbranchName;
}
public void setLoginbranchName(String loginbranchName) {
System.out.println("loginbranchName"+loginbranchName);
this.loginbranchName = loginbranchName;
}
public void getLoginBranchId()
{
System.out.println("enter into getloginbranchid");
System.out.println("loginbranchName"+loginbranchName);
int unitId=loginDAO.getLoginBranchId(loginbranchName);
System.out.println("unitId"+unitId);
}
#BalusC was correct regarding the above discussion where, JSF2 can work with List and SelectItem https://stackoverflow.com/tags/selectonemenu/info I think you might be missing
<h:form>
</h:form>
tag.

Set only entity ID instead of whole entity in p:selectOneMenu

I'm using JSF 2.1 and Tomcat 7. I have the following PrimeFaces select one menu:
<p:selectOneMenu id="idMarcaEdit"
value="#{cfgCentraleController.selMarcaEdit}"
var="p"
height="250"
effect="fade"
converter="marcaConverter"
>
<f:selectItems value="#{cfgCentraleController.marche}" var="c" itemLabel="#{c.marca}-#{c.modello}-#{c.versione}" itemValue="#{c}"/>
<p:column>
<h:outputText value="#{p.marca}" />
</p:column>
<p:column>
<h:outputText value="#{p.modello}" />
</p:column>
<p:column>
<h:outputText value="#{p.versione}" />
</p:column>
<p:column>
<h:outputText value="#{p.provisioning}" />
</p:column>
</p:selectOneMenu>
I have this in #{cfgCentraleController}
private List<Marca> marche;
private Marca selMarcaEdit;
public Marca getSelMarcaEdit() {
return selMarcaEdit;
}
public void setSelMarcaEdit( Marca selMarcaEdit ) {
this.selMarcaEdit = selMarcaEdit;
this.selectedCentrale.setIdRete( this.selMarcaEdit.getIdMarca());
}
public List<Marca> getMarche() {
return marche;
}
It works fine, but I don't realy need the whole entity to be submitted. I just need its ID.
I'm using OmniFaces for the converter:
import org.omnifaces.converter.SelectItemsConverter;
#Override
public String getAsString( FacesContext context, UIComponent component, Object value ) {
Integer id = (value instanceof Marca) ? ((Marca) value).getIdMarca() : null;
return (id != null) ? String.valueOf( id ) : null;
}
How do I obtain only the ID of the entity in the model?
If you need only the ID, then just set only the ID as select item value.
Change
<f:selectItems ... itemValue="#{c}" />
to
<f:selectItems ... itemValue="#{c.idMarca}" />
and change
private Marca selMarcaEdit;
to
private Integer selMarcaEdit;
then you can remove the converter from <p:selectOneMenu>.
See also:
Our selectOneMenu wiki page
Update: this is not possible in this construct with menu with "custom layout". After all, it turns out that the <p:selectOneMenu var> is getting its value from <f:selectItems itemValue> instead of <f:selectItems var>. Sorry, I totally didn't expect that. Given this, you cannot change the model and get rid of the converter. You should keep the code as is. Your best bet is to post an enhancement request to PF guys to improve this situation.

Resources