I have a simple OneMenu in JSF:
#ManagedBean
#ViewScoped
public class ProductBean {
...
protected static Map<String, String> priceTypes;
...
getter & setter
}
<p:selectOneMenu id="sizeType" >
<f:selectItems value="#{productBean.priceTypes}" />
</p:selectOneMenu>
In my usecase, i want to preselect[1] an option out of "priceTypes" - how can i do that?
I am using Glassfish 3.1.2.2 with Primefaces 3.4.1
[1] See "selected" at http://www.w3schools.com/tags/tag_option.asp
I am not sure about pfaces, but in plain JSF you just have to set the value attribute in the selectOneMenu tag, and make sure that the value returned by the bean is in the select list.
<p:selectOneMenu id="sizeType" value="#{myBean.sizeType}>
<f:selectItems value="#{productBean.priceTypes}" />
</p:selectOneMenu>
Of course, sizeType must mutch the key of your map.
Related
I don't know if there's a solved question for that. If so, I wasn't able to find it.
I'm creating a composite component with two SelectOneMenu that fires an event.
The thing is that this component works in one jsf but doesn't in others.
The component is inside three different xhtml. In all of them the event is fired when I change the value of the first SelectOneMenu, but only in one of the xhtml the value of the SelectItem is not null. In the rest of them, the retrieved value is "null"
//In p001DatosNotificacion.xhtml, it retrieves the selected value of the selectonemenu (b, for example)
//In the others it retrieves null
UISelectOne oneVoewl = (UISelectOne)event.getSource();
System.out.println("One Voewl > " + oneVoewl.getValue());
Can anyone help me and tell me where I'm making the mistake(s)?
Thanks in advance!
The code
Component backing bean
#FacesComponent(value="letterExample")
public class LetterExample extends UINamingContainer{
public void voewlAjaxListener(AjaxBehaviorEvent event){
UISelectOne oneVoewl = (UISelectOne)event.getSource();
System.out.println("One Voewl > " + oneVoewl.getValue());
}
Composite component (Note: I'm using an existing bean for testing)
<cc:interface componentType="letterExample" >
<cc:attribute name="bean" type="es.ccasa.sgnx.business.bean.Direccion" />
</cc:interface>
<cc:implementation>
<p:selectOneMenu id="oneMenuVoewl" value="#{cc.attrs.bean.codigoPais}"
binding="#{cc.uiSelectOneVoewl}" >
<f:selectItem itemLabel="Aaaa" itemValue="a" />
<f:selectItem itemLabel="Eeee" itemValue="e" />
<f:selectItem itemLabel="Iiii" itemValue="i" />
<f:selectItem itemLabel="Oooo" itemValue="o" />
<f:selectItem itemLabel="Uuuu" itemValue="u" />
<p:ajax event="change" listener="#{cc.voewlAjaxListener}" update="consonantWrapper" />
</p:selectOneMenu>
<h:panelGroup id="consonantWrapper" layout="block">
<p:selectOneMenu id="oneMenuConsonant" value="#{cc.attrs.bean.codigoProvincia}"
binding="#{cc.uiSelectOneConsonant}">
<f:selectItem itemLabel="Bbbb" itemValue="b" />
<f:selectItem itemLabel="Cccc" itemValue="c" />
<f:selectItem itemLabel="Dddd" itemValue="d" />
</p:selectOneMenu>
</h:panelGroup>
</cc:implementation>
Bean
public class Direccion implements Serializable {
private String codigoPais; /* with its getter and setter*/
private String codigoProvincia; /* with its getter and setter*/
}
XHTML
This is the way I use inside the xhtml pages: (The name of the controller changes depends on the current controller)
<sgnx:letter-example bean="#{controller.direccion}" />
The #Named beans
#Named
#ViewScoped
public class P001DatosNotificacionController
private Direccion direccion; /* with its getter and setter*/
#Named
#ViewScoped
public class P001GestionNotificacionesController
private Direccion direccion; /* with its getter and setter*/
#Named
#ViewScoped
public class P002BandejaProvisionalNotificacionesController
private Direccion direccion; /* with its getter and setter*/
After a few weeks I went back to the problem.
I decided to start again from scratch.
When I started to write the controller code and annotated it with #ViewScoped I realize about javax.faces.bean and javax.faces.view packages
And that was the mistake. I was using a wrong combination:
import javax.inject.Named;
import javax.faces.bean.ViewScoped;
So, the solution is as easy as using the proper combination of packages:
import javax.inject.Named;
import javax.faces.view.ViewScoped;
In these posts are the answer and the explanation:
#ViewScoped bean recreated on every postback request when using JSF 2.2
ManagedProperty in CDI #Named bean returns null
Inject CDI bean into JSF #ViewScoped bean
I'm using JSF 2.2.8 and primefaces 6.0, and i have a selectCheckBoxMenu i want to retrieve the selected values in my bean.
The selectCheckboxMenu is filled from the database but when i select the attributes and I save nothing happens it does not call the save function
Here is my selectCheckBoxMenu
<p:outputLabel for="ressource" value="Ressource"/>
<h:panelGroup >
<p:selectCheckboxMenu id="ressource" label="Ressource" value="#{affectationBean.selectedRessource}" multiple="true">
<f:selectItems value="#{affectationBean.ressources}" var="r" itemLabel="#{r.nom}" itemValue="r.idt_ressource" />
</p:selectCheckboxMenu>
</h:panelGroup>
<p:commandButton icon="ui-icon-save" actionListener="#{affectationBean.save}" value="Save" update="#affectation" ajax="false" style="display:inline-block;margin-top:5px"/>
Here is the the declaration of the selectedRessource and the actionListener save
private Long [] selectedRessource;
// Getters setters and Construct
public void save(){
for(int i=0 ;i<selectedRessource.length;i++){
system.out.println("id ===> " + selectedRessource[i]);
}
My suggestion would be:
First make sure everything is inside the h:form tag.
don't need to multiple = true as this tag does not take this attribute
i tested with below modification and got the selected multiple value in my bean. The only difference is i am using same value for itemLabel and itemValue but in your case it is object. i am using primefaces 6 also and dont even need to change actionListner to action. It is working as it is.sample xhtml
sample ResourceBean.java
<p:outputLabel for="ressource" value="Ressource"/>
<h:panelGroup >
<p:selectCheckboxMenu id="ressource" label="Ressource" value="#{resourceBean.selectedRessource}">
<f:selectItems value="#{resourceBean.ressources}" var="r" itemLabel="#{r}" itemValue="#{r}" />
</p:selectCheckboxMenu>
</h:panelGroup>
<p:commandButton icon="ui-icon-save" actionListener="#{resourceBean.save}" value="Save" ajax="false" style="display:inline-block;margin-top:5px"/>
The problem is in your p:commandButton, you have 3 options
change your method:
public void save(ActionEvent actionEvent){...}
change your action listener value:
actionListener="#{affectationBean.save()}"
or change your button to use action
action="#{affectationBean.save}"
DISCLAIMER: This is a workaround. It is not intended to be a permanent solution but will allow you to use selectCheckboxMenu and keep working.
There is an issue with this component that prevents it from passing values to the backing bean upon submit.
For some reason the array that should contain the selected values gets cleared out upon submit. Therefore I made an extra array that I did not declare in the tag, and updated in on every change event. Then on submit the values were still there. See below:
BackingBean.java
private String[] sCodes;
private String[] sCodes2; //extra array, not in form.xhtml
public void updateCodes()
{
sCodes2 = sCodes; //keeps the values in the other array
}
public void doSend() throws IOException
{
log.trace("selected codes: {} selected codes2 length: {}", sCodes.length, sCodes2.length);
}
form.xhtml
<p:selectCheckboxMenu id="codeCtl" value="#{bean.SCodes}" label="Codes" filter="true" filterMatchMode="startsWith" panelStyle="width:250px">
<f:selectItems value="#{bean.menuCodes}" />
<p:ajax event="change" listener="#{bean.updateCodes()}" />
</p:selectCheckboxMenu>
<p:commandButton value="submit" actionListener="#{bean.doSend}" id="ctlSubmit" update="appform"/>
<h:selectOneMenu id="reportType" style="width:175px" value="#{pageDemoSearchBean.status}">
<f:selectItems value="#{pageDemoSearchBean.vehicleStatus}" />
</h:selectOneMenu>
I am using JSF 1.2
This lines of code throws a classCastException
java.lang.String cannot be cast to javax.faces.model.SelectItem
Provided that replacing "value" by "itemValue" in f:selectItems tag is giving another exception which tells that
itemValue is incorrect for selectItems according to TLD
In JSF 1.x, it is not possible to provide a List<T> as <f:selectItems value>. It only supports List<javax.faces.model.SelectItem>.
Here's a kickoff example of proper usage, copypasted from our selectonemenu tag wiki page (which is written with JSF 2.x in mind, so take into account that some examples won't work for the jurassic JSF 1.x):
<h:form>
<h:selectOneMenu value="#{bean.selectedItem}">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>
<h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>
private String selectedItem; // +getter +setter
private List<SelectItem> availableItems; // +getter (no setter necessary)
#PostConstruct
public void init() {
availableItems = new ArrayList<SelectItem>();
availableItems.add(new SelectItem("foo", "Foo label"));
availableItems.add(new SelectItem("bar", "Bar label"));
availableItems.add(new SelectItem("baz", "Baz label"));
}
I am new to primefaces and i have a problem to save my primefaces SelectManyCheckbox value to database. I am using hibernate and mysql. The sample code are give as below
My xhtml pages code is:
<h:outputText value="#{msg['elicense.examinationform.personal.classofcertificates']}"/>
<p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedClass}" layout="grid" columns="1">
<f:selectItems value="#{examinationFormBean.examinationPart}"var="className" itemLabel="#{className.name}" itemValue="#{className}" />
</p:selectManyCheckbox>
My bean is:
private String[] selectedClass;
private List<CertificateClass> examinationPart=new ArrayList<CertificateClass>();
getter()
setter()
The method where I want to save my checkbox is:
private void saveExaminationDetails()
{
examDetails.setElementaryPrinciples(); //bolean field
examDetails.setLightinig()
//no of setter
}
I am not able to find out how I will set the selected and not selected checkbox value on the method
Look at primefaces showcases: http://primefaces-rocks.appspot.com/ui/selectManyCheckbox.jsf
Selected values from examinationFormBean.examinationPart should setting in p:selectManyCheckbox attribute value and then you can used this selected list in bean method.
For your example should be something:
<p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedExaminationParts}" layout="grid" columns="1">
<f:selectItems value="#{examinationFormBean.examinationParts}" var="className" itemLabel="#{className.name}" itemValue="#{className}" />
</p:selectManyCheckbox>
And then you can use selectedExaminationParts in your saveExaminationDetails()
The p:selectManyCheckbox select values are biding a String Collection(List, ArrayList... etc) on managed bean. You just need to save each String existent on the Collection.
I will give you an example showing how you can do that:
Example:
...
#Named(value = "myBean")
#SessionScoped
public class InscricaoBean implements Serializable {
...
private List<String> selectedElemnts = new ArrayList();
//selectedElements get and set
...
On JSF you have something like:
...
<h:outputText value="#{msg['elicense.examinationform.personal.classofcertificates']}"/>
<p:selectManyCheckbox id="grid" value="#{examinationFormBean.selectedElemnts}"...>
<f:selectItems value="#{examinationFormBean.examinationPart}"var="className"
itemLabel="#{className.name}" itemValue="#{className}" />
</p:selectManyCheckbox>
...
On save method:
...
private void saveExaminationDetails()
{
for (String nameAux: selectedElemnts )
{
//you save the data here
}
}
...
i have problem with selectonemenu component
in View i have this code:
<p:selectOneMenu id="city" value="#{logisticsController.selectedTrip}" >
<f:selectItem itemValue="#{null}" itemLabel="Wybierz podroz" />
<f:selectItems value="#{logisticsController.tripList}" />
<p:ajax update="pojoPickList"
listener="#{logisticsController.handleTripChange}" />
</p:selectOneMenu>
logisticsController is SessionScoped bean and have init method:
#PostConstruct
public void init(){
SelectedTrip = null ;
}
but every time when i refresh page selectOneMenu shows old value.
i want set default value equals null, thanks for help
First of all, #PostConstruct annotation says to your Injection Framework (Spring or smth else) to execute this method after dependency injections are finished (http://docs.oracle.com/javaee/7/api/javax/annotation/PostConstruct.html).
Just to simplify, we can assume that this method will execute after bean creating.
So, as it's a session scoped bean - it's created only once for user session. That's why method "public void init()" isn't called on page refresh.
There is 2 simple ways how to solve your problem:
1) If it's possible - change your SessionScoped bean to a RequestScoped, after that bean will be created on every request (on every page refresh)
2) You can just add init method invoking in your xhtml page like this:
...
#{logisticsController.init()}
<p:selectOneMenu id="city" value="#{logisticsController.selectedTrip}" >
<f:selectItem itemValue="#{null}" itemLabel="Wybierz podroz" />
<f:selectItems value="#{logisticsController.tripList}" />
<p:ajax update="pojoPickList"
listener="#{logisticsController.handleTripChange}" />
</p:selectOneMenu>
...