I'm trying to a populate a selectItems from a query and It simply won't WORK. I've tried with List list, List list, Map<Long,String> list... None of then works... There's no error, the prime faces won't show the list. Simply as that.
<p:selectOneMenu id="pessoa" value="#{matriculaController.pessoa}">
<f:selectItem itemLabel="Selecione uma Pessoa" itemValue="" />
<f:selectItems
value="#{selectOneMenuView.pessoaLista}"
var="pessoa"
itemLabel="#{pessoa.nome}"
itemValue="#"
/>
</p:selectOneMenu>
Controller:
#Getter
#Setter
#Autowired
private List<Person> pessoaLista;
public void findAllPersons() {
pessoaLista = matriculaService.carregarPessoas();
}
My query function is working, the pessoaLista is populated, the HTML doesn't break. It simply comes blank.
And I've copied from the PrimeFaces docs... (docs)
Use List<Person> and add itemValue="#{pessoa.id}" to your <f:selectItems>
<p:selectOneMenu id="pessoa"
value="#{matriculaController.pessoa}">
<f:selectItem itemLabel="Selecione uma Pessoa"
itemValue="" />
<f:selectItems value="#{selectOneMenuView.pessoaLista}"
var="pessoa"
itemLabel="#{pessoa.nome}"
itemValue="#{pessoa.id}"/>
</p:selectOneMenu>
Related
I've been strugling with selectOneMenu dinamically populated by a HashMap without success for a day now, and can´t find what´s going on.
Followed the steps on
How to populate options of h:selectOneMenu from database?
but still no luck
Here´s my bean:
private Paciente selectedPaciente;
private Map<String, String> itensPacientes;
#PostConstruct
public void init() {
itensPacientes = new LinkedHashMap<String, String>();
itensPacientes.put("1","teste1");
itensPacientes.put("2","teste1");
itensPacientes.put("3","teste1");
}
public Map<String, String> getItensPacientes() {
return itensPacientes;
}
public Paciente getSelectedPaciente(){
return selectedPaciente;
}
public void setSelectedPaciente(Paciente selectedPaciente){
this.selectedPaciente = selectedPaciente;
}
and here's the jsf portion
<h:selectOneMenu value="#{beanAgenda.selectedPaciente}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="--select--" />
<f:selectItems value="#{beanAgenda.itensPacientes}"
itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
</h:selectOneMenu>
But when I run the code, I can only see the "--select--" option on the combobx.
Is there something I'm overlooking?
Thanks in advance
Try this:
<f:selectItems value="#{beanAgenda.itensPacientes.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
Answer and explanation from this post
I have Primefaces <p:datatable> with cellEdit mode enabled, means when cell is clicked, it changes to editing mode and when you click somewhere else (onblur) cell returns to output mode and calls cellEdit ajax event if its changed.
In datatable editable cells i use <p:selectOneMenu> and <p:autoComplete> with dropdowns. Primefaces generates HTML code of dropdowns outside the cell container, so every time I select something from dropdown, the cell saves value and exits the edit mode, and I need it to stay in edit mode.
I know this works properly with <h:selectOneMenu>, but using other elements is not an option for me.
Is there a way to make cell edit to ignore clicks on drop down?
Or is there a way to prevent that onblur event from firing while drop down is open?
Columns of datatable are dynamic in my case.
I use :
Primefaces 5.3
PrimeFaces Extensions 4.0.0
Mojarra 2.2.9
Wildfly 8
A basic example of this issue:
xhtml:
<h:form id="form">
<p:dataTable id="cars" var="car" value="#{dtEditView.cars}" editable="true" editMode="cell" widgetVar="cellCars">
<p:ajax event="cellEdit" />
<p:column headerText="Car">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.title}" /></f:facet>
<f:facet name="input"><p:inputText value="#{car.title}" style="width:100%" label="Car"/></f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Color">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car.color}" /></f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{car.color}" style="width:100%">
<f:selectItem itemLabel="Red" itemValue="Red" />
<f:selectItem itemLabel="Blue" itemValue="Blue" />
<f:selectItem itemLabel="Green" itemValue="Green" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
Backing bean:
#ViewScoped
#Named("dtEditView")
public class TestController implements Serializable {
List<Car> cars=new ArrayList<Car>();
#PostConstruct
public void init(){
cars.add(new Car("BMW","Red"));
cars.add(new Car("Alfa Romeo","Green"));
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
}
Car object:
public class Car {
String title;
String color;
Car(String title, String color){
this.title=title;
this.color=color;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
I'm not sure if this is directly related, but in Primereact for the same DataTable component, if cell edit and a Dropdown is used together you get the same behaviour when you click the dropdown and select a value in the list, it's interpretted as a click outside of the cell and so edit mode ends without saving the value.
This is rectified by adding appendTo='self' to the Dropdown, making the list appear attached to the dropdown and so to the cell, meaning the click isn't classed as being outside of the cell. Setting this value on the Dropdown results in the expected behaviour of the cell remaining in edit mode.
I'm fairly new to Java EE and I'm building a simple webshop using maven web application . I have a problem with my Stateful Session Bean. I've been searching the net and tried different sollutions(most of them for using servlets) but it doesn't seem to work.
Anyway, my problem is that I'm trying to use my session bean to keep track of what's in the shopping cart. I am using an arrayList to store the items. However, when I add a new item it just replaces the old item instead of adding it to the list. I'm guessing the session bean somehow updates or a new instance of it is created but I just can't seem to find any sollution or information about this.
The stateful session bean
#Stateful
#LocalBean
public class CartSessionBean{
private List contents;
public CartSessionBean(){
contents= new ArrayList();
}
public List getContents() {
return contents;
}
public void addProduct(String title) {
contents.add(title);
}
}
The Managed Bean
#ManagedBean
#RequestScoped
public class ProductController {
private List cartList = new ArrayList();
private int nrOfCartItems=0;
#EJB private CartSessionBean cart;
public String doAddCart(String title)
{
cart.addProduct(title);
setCartList(cart.getContents());
setNrOfCartItems(cart.getContents().size());
return "products.xhtml";
}
}
The Facelet
<h:form>
<p>
your cart contains <h:outputLabel class="" value="#{productController.nrOfCartItems}" /> items.
<ui:repeat value="#{productController.cartList}" var="cart">
<h:outputLabel value="#{cart}" />
</ui:repeat>
<h:commandButton value="go to checkout"/>
</p>
</h:form>
<h:form>
<h:dataTable value="#{productController.productList}" var="pr" border="0">
<h:column>
<h:graphicImage value="images/#{pr.picture}" />
</h:column>
<h:column>
<h2><h:outputText value="#{pr.product_name}"/></h2>
<p> in stock: <h:outputText value="#{pr.stock}"/><br/>
price: <h:outputText value="#{pr.price}"/> SEK<br/><br/>
<h:outputText value="#{pr.description}"/><br/></p>
<h:commandButton value="add to cart" action="#{productController.doAddCart(pr.product_name)}"/>
</h:column>
</h:dataTable>
</h:form>
Your managed bean should be SessionScope to live during the session.
In your case you always creating new ProductController bean for each request and because of that you always inject different CartSessionBean (there is no way how could container know that it should inject the same SessionBean into your RequestScope Managed Bean).
I've got a JSF creation form using a selectManyListbox with selectItems inside (it should contain all tags available for projects). The selectItems list though is always empty - when the page renders, there's nothing in the Listbox. Still, the list in the backing bean contains 3 entries (checked that). What am I doing wrong?
The backing bean:
#ManagedBean(name = "createProjectBean")
#RequestScoped
public class CreateProjectBean {
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
private Project project;
private IProjectService projectService;
private FacesContext facesContext;
private MessageFactory mf;
private List<Tag> tags;
public CreateProjectBean() {
project = new Project();
projectService = (IProjectService)ServiceFinder.getInstance()
.findBean("projectService");
mf = new MessageFactory("properties.projects.messages");
tags = projectService.getTags();
}
/* should be the source of tags */
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public List<Tag> getTags() {
return tags;
}
}
And the page:
<f:view>
<h:outputText id="error" rendered="false" />
<h:message styleClass="errorMessage" for="error" />
<h:form id="creationForm" >
<h:panelGrid columns="2" width="420">
/* blah, blah, set name and stuff */
<h:selectManyListbox id="box" value = "#{createProjectBean.project.tags}">
<f:converter converterId="tag" />
<f:selectItems value="#{createProjectBean.tags}"
var="tag"
itemValue="#{tag}"
itemLabel="${tag.name}" />
</h:selectManyListbox>
<f:verbatim><br/></f:verbatim>
<h:commandButton value="Create" styleClass="formButton" action="#{createProjectBean.create}"/>
</h:panelGrid>
</h:form>
</f:view>
I tried to do it per analogia to this page:
http://digitaljoel.wordpress.com/2010/01/11/jsf-2-custom-converter/
The converter I've written is yet to be tested.
Your EL is bogus. You should use the #{} notation everywhere. Replace
<f:selectItems value="#{createProjectBean.tags}"
var="tag"
itemValue="#{tag}"
itemLabel="${tag.name}" />
by
<f:selectItems value="#{createProjectBean.tags}"
var="tag"
itemValue="#{tag}"
itemLabel="#{tag.name}" />
I have a form where i have to preselect some checkboxes. How is that possible with jsf/seam? In plain html you would just add the "checked" (or checked="checked") as attribute to the checkboxes. But with f:selectItems I have no clue...also the Object "SelectItem" does not provide any setter for this...
You need to preset them in the property behind the component's value attribute as you usually do for every UIInput component. You can do that in the bean's constructor or initialization block.
Here's a basic example:
<h:selectManyCheckbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>
Bean:
private List<String> selectedItems; // +getter +setter.
private List<SelectItem> selectItems; // +getter.
public Bean() {
// Preset the selected items.
this.selectedItems = new ArrayList<String>();
this.selectedItems.add("valueToBePreselected1");
this.selectedItems.add("valueToBePreselected2");
// Those values should be exactly the same as one of the SelectItem values.
// I.e. the Object#equals() must return true for any of them.
}
Populate the property you use in "value" before rendereing the page (for example using a phase listener)
<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
<f:selectItem itemLabel="India" itemValue="India" />
<f:selectItem itemLabel="China" itemValue="China" />
<f:selectItem itemLabel="Germany" itemValue="Germany" />
<f:selectItem itemLabel="USA" itemValue="USA" />
</h:selectManyCheckbox>