I am currently working on a small Voting-Tool with JSF, Primefaces, CDI etc... The main purpose of that is to improve my programming skills and learning a bit about Java EE.
In the tool, one can collect PC-games and create Game-Votings, where people (Lan-Party) can (or should be able to) vote for a game. For the Voting-Form I want to use Primefaces "SelectOneListBox". It gets the List of Games from a @ApplicationScoped Bean that handels the Voting ("votingListProducer") and looks as follows:
<h:form id="votingFormID">
<p:selectOneListbox value="#{votingListController.selectedGame}" converter="#{gameConverter}"
var="g" showCheckbox="true" style="width:500px;" >
<f:selectItems value="#{votingListProducer.voting.gameList}" var="game" itemLabel="#{game.name}" itemValue="#{game}" />
<p:column>
<p:graphicImage value="resources/images/#{g.imgSrc}" width="40"/>
</p:column>
<p:column>
#{g.name}
</p:column>
<p:column>
#{g.version}
</p:column>
</p:selectOneListbox>
<p:commandButton value="Submit" action="#{votingListController.doAddVoteAktion}" />
<p:commandButton value="Zeige selektierte List" action="#{votingListController.doPrintSelectedGame}"/>
</h:form>
When I call this view, the correct List of Games is displayed. But when I select one and click on "Submit" the converter converts it right, because it returns the correct Game, but in the Controller the attribute "selectedGame" will not be set to this Game. And also the Methods are not called.
Here is the Controller:
@SessionScoped
@Named
public class VotingListController implements Serializable{
private static final long serialVersionUID = -7222543653854660316L;
private Game selectedGame;
@Inject @AddedChoice
private Event<Game> addChoiceEventSrc;
public void doAddVoteAktion (){
if (selectedGame!=null){
addChoiceEventSrc.fire(selectedGame);
}
}
public void doPrintSelectedGame(){
if (selectedGame!=null){
System.out.println(selectedGame.getName());
}
}
public Game getSelectedGame() {
return selectedGame;
}
public void setSelectedGame(Game selectedGame) {
this.selectedGame = selectedGame;
}
}
Do you have any Idea why this does not work?
Thank You!
Edit: Converter-Code
@RequestScoped
@Named
public class GameConverter implements Converter{
@Inject
private GameListManager gameListManager;
@Override
public Object getAsObject(FacesContext fc, UIComponent ui, String value) {
for (Game g : gameListManager.getGameList()){
if (g.getGameId().toString().equalsIgnoreCase(value)){
System.out.println ("I receive: "+g.getName());
return g;
}
}
return null;
}
@Override
public String getAsString(FacesContext fc, UIComponent ui, Object value) {
if (value!=null){
Game game = (Game) value;
String res = game.getGameId().toString();
System.out.println(res);
return res;
}
return null;
}
}