diff --git a/src/main/java/net/bootsfaces/demo/selectOneMenu/Beer.java b/src/main/java/net/bootsfaces/demo/selectOneMenu/Beer.java new file mode 100644 index 00000000..016d64fe --- /dev/null +++ b/src/main/java/net/bootsfaces/demo/selectOneMenu/Beer.java @@ -0,0 +1,32 @@ +package net.bootsfaces.demo.selectOneMenu; + +/** + * Example taken from : https://memorynotfound.com/using-custom-converter-for-hselectonemenu/ + */ + +public class Beer { + + private Integer id; + private String brand; + + public Beer(Integer id, String brand) { + this.id = id; + this.brand = brand; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } +} \ No newline at end of file diff --git a/src/main/java/net/bootsfaces/demo/selectOneMenu/BeerConverter.java b/src/main/java/net/bootsfaces/demo/selectOneMenu/BeerConverter.java new file mode 100644 index 00000000..d8c856a1 --- /dev/null +++ b/src/main/java/net/bootsfaces/demo/selectOneMenu/BeerConverter.java @@ -0,0 +1,32 @@ +package net.bootsfaces.demo.selectOneMenu; + +import javax.el.ValueExpression; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.convert.Converter; +import javax.faces.convert.FacesConverter; + +/** + * Example taken from : https://memorynotfound.com/using-custom-converter-for-hselectonemenu/ + */ + +@FacesConverter(value = "beerConverter") +public class BeerConverter implements Converter { + + @Override + public Object getAsObject(FacesContext ctx, UIComponent uiComponent, String beerId) { + System.out.println("getAsObject:" + beerId); + + ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(), + "#{beersBean}", BeersBean.class); + + BeersBean beers = (BeersBean) vex.getValue(ctx.getELContext()); + return beers.getBeer(Integer.valueOf(beerId)); + } + + @Override + public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object beer) { + System.out.println("getAsString:" + beer); + return ((Beer) beer).getId().toString(); + } +} diff --git a/src/main/java/net/bootsfaces/demo/selectOneMenu/BeersBean.java b/src/main/java/net/bootsfaces/demo/selectOneMenu/BeersBean.java new file mode 100644 index 00000000..6a93229c --- /dev/null +++ b/src/main/java/net/bootsfaces/demo/selectOneMenu/BeersBean.java @@ -0,0 +1,72 @@ +package net.bootsfaces.demo.selectOneMenu; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.faces.bean.ManagedBean; + +/** + * Example taken from : https://memorynotfound.com/using-custom-converter-for-hselectonemenu/ + */ +@ManagedBean +@javax.faces.bean.ViewScoped +public class BeersBean implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -3192521384162408966L; + private Beer selectedBeer; + private Beer selectedBeer2; + + public Beer getSelectedBeer2() { + return selectedBeer2; + } + + public void setSelectedBeer2(Beer selectedBeer2) { + this.selectedBeer2 = selectedBeer2; + } + + private List beers; + + public BeersBean() { + _init(); + } + + public void _init() { + beers = new ArrayList(); + beers.add(new Beer(10, "La Chouffe")); + beers.add(new Beer(20, "Stella Artois")); + beers.add(new Beer(30, "Westmalle Trippel")); + } + + public Beer getSelectedBeer() { + return selectedBeer; + } + + public void setSelectedBeer(Beer selectedBeer) { + System.out.println("set:" + selectedBeer); + this.selectedBeer = selectedBeer; + } + + public List getBeers() { + return beers; + } + + public void setBeers(List beers) { + this.beers = beers; + } + + public Beer getBeer(Integer id) { + if (id == null) { + throw new IllegalArgumentException("no id provided"); + } + for (Beer beer : beers) { + if (id.equals(beer.getId())) { + return beer; + } + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/net/bootsfaces/demo/SelectOneMenuBean.java b/src/main/java/net/bootsfaces/demo/selectOneMenu/SelectOneMenuBean.java similarity index 98% rename from src/main/java/net/bootsfaces/demo/SelectOneMenuBean.java rename to src/main/java/net/bootsfaces/demo/selectOneMenu/SelectOneMenuBean.java index cdf70d96..46b4fe7b 100644 --- a/src/main/java/net/bootsfaces/demo/SelectOneMenuBean.java +++ b/src/main/java/net/bootsfaces/demo/selectOneMenu/SelectOneMenuBean.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package net.bootsfaces.demo; +package net.bootsfaces.demo.selectOneMenu; import java.io.Serializable; diff --git a/src/main/webapp/forms/converters.xhtml b/src/main/webapp/forms/converters.xhtml index 12394b5d..bc74bc5a 100644 --- a/src/main/webapp/forms/converters.xhtml +++ b/src/main/webapp/forms/converters.xhtml @@ -16,6 +16,7 @@ + @@ -161,6 +162,141 @@ +

Using converters with <b:selectOneMenu />

+

This example has been inspired by https://memorynotfound.com/using-custom-converter-for-hselectonemenu/. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/forms/selectOneMenu.xhtml b/src/main/webapp/forms/selectOneMenu.xhtml index 4f368f93..18386e55 100644 --- a/src/main/webapp/forms/selectOneMenu.xhtml +++ b/src/main/webapp/forms/selectOneMenu.xhtml @@ -37,7 +37,8 @@ - + @@ -164,14 +165,166 @@ this can be surprising because JavaEE allows you to define converters via annotations. In this case, the converter isn't mentioned by the <b:selectOneMenu /> declaration directly.

+

This example has been inspired by https://memorynotfound.com/using-custom-converter-for-hselectonemenu/. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Arrays and HashMaps

The list of items can be defined as an <f:selectItems /> tag. BootsFaces uses a modified version of the algorithm PrimeFaces 5.1 uses to implement <f:selectItems />. As a result, you can use <b:selectOneMenu> almost the same way as <b:selectOneMenu>.

Due to a bug, arrays and hashmaps can be used to display the combobox, but the user input is not processed correctly by the server. This bug will be fixed in the next version of BootsFaces. - - @@ -269,7 +422,7 @@ ]]> - +