1

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;
}

 }
JuHarm89
  • 847
  • 3
  • 12
  • 26
  • The most likely reason why the backing bean action would not be called is if validation and/or conversion fail. Verify that the conversion and validation are happening properly; place a `` component on your page or look in the javascript console in your browser to confirm – kolossus Jan 17 '14 at 21:46
  • Please work through the causes listed in the answer of http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked and update your question accordingly. – BalusC Jan 19 '14 at 14:46
  • I added a message-Tag and the problem seems to be a Validation Error. After submitting the Form it says: "Value is not valid". I read somewhere that this might have something to do with the fact that i use "@PersistenceContext" to inject the EntityManager in my Stateless-Bean, which handels the Games in the DB? – JuHarm89 Jan 19 '14 at 16:10
  • I solved the problem by overriding the equals() method of the class "Game". Can someone explain me, why the original equals() method fails, when (in my case) the selectedGame is compared to the List of Games in the voting? – JuHarm89 Jan 20 '14 at 11:50

2 Answers2

-1

Try:

<p:commandButton value="Submit" actionListener="#{votingListController.doAddVoteAktion}"
 process=":votingFormID"/>

Maybe it can be a error when your converter calls getAsObject, can you post the converter code?

Cesar Zegarra
  • 207
  • 1
  • 13
  • I added the Converter-Code. As you see for the conversion the "GameListManager" is used, which handels all the Games in the DB. Also the System.out gives me the correct Name of the Game, which i selected and submitted previously. – JuHarm89 Jan 17 '14 at 23:48
-1

Change your action Listener definition to

public void doAddVoteAktion (ActionEvent event){
    if (selectedGame!=null){
        addChoiceEventSrc.fire(selectedGame);
    }   
}

And

<p:commandButton value="Submit" actionListener="#votingListController.doAddVoteAktion}" ajax="true" process="@this"/>