-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TheCoder4eu/BootsFaces-OSP#919 demo for the converters
- Loading branch information
1 parent
6a1b6de
commit 0122c5b
Showing
6 changed files
with
430 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/net/bootsfaces/demo/selectOneMenu/BeerConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/net/bootsfaces/demo/selectOneMenu/BeersBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Beer> beers; | ||
|
||
public BeersBean() { | ||
_init(); | ||
} | ||
|
||
public void _init() { | ||
beers = new ArrayList<Beer>(); | ||
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<Beer> getBeers() { | ||
return beers; | ||
} | ||
|
||
public void setBeers(List<Beer> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.