action p:command button doesn't set property into managed bean

960 views Asked by At

I have an issue, I have a carousel, and I load dynamically some data into it, when I press into a carousel object it have to set a value into a manangedbean (CurrentSong) and show a dialog, now the dialog appear but the set property doesn't work. why?

xhtml page:

<h:body style="background: url(../resources/images/knapsack_background_light.jpg); background-attachment:fixed;">
<div id="contentContainer" class="trans3d"> 
    <section id="carouselContainer" class="trans3d">
        <ui:repeat value="#{retrieve.mostPopularSongs}" var="carouselSelectedSong">
                            <figure id="item" class="carouselItem">
                <div class="itemInfo">
                    <h:commandButton id="selectedButton"
                                     action="#{currentSong.setSong(carouselSelectedSong)}"
                                     styleClass="btn"
                                     onclick="parent.showSongDialog();"
                                     style="
                                     background-image: url('#{carouselSelectedSong.coverPath}');
                                     background-size:100%;
                                     width:300px; 
                                     height:300px; 
                                     border: black;">
                        <f:ajax render="songDialogContent"/>
                    </h:commandButton>
                </div>
            </figure> 
        </ui:repeat>
    </section>
</div>

managed bean @ManagedBean @SessionScoped:

public class CurrentSong implements Serializable {
    @EJB
    private CustomerManagementLocal customerManagement;
    @EJB
    private SocialManagementLocal socialManagement;

    private Customer customer;
    private Song song;
    private String textComment;


    public CurrentSong() {
    }

    public Customer getCustomer() {
        return customer;
    }

    public Song getSong() {
        return song;
    }

    public void setSong(Song song) {
        System.out.println("----------------------------------------- current song: " + song.getTitle());
        this.song = song;
    }

    public void putLike () {
        putValutation(true);
    }

    public void putDislike () {
        putValutation(false);
    }

    public String getTextComment() {
        return textComment;
    }

    public void setTextComment(String textComment) {
        this.textComment = textComment;
    }

    public void putComment () {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            Comment newComment = new Comment(customer, new Date(), textComment, song);
            song.getCommentList().add(newComment);
            socialManagement.putComment(newComment);
            Notifier.notifyInfoMessage(context, Constants.INSERTION_COMMENT_SUCCESSFULLY);
            RequestContext requestContext = RequestContext.getCurrentInstance();
            requestContext.execute("clearTextComment();");
        } catch (CustomerNotFoundException ex) {
            Notifier.notifyErrorMessage(context, Constants.INTERNAL_ERROR);
        } catch (SongNotFoundException ex) {
            Notifier.notifyErrorMessage(context, Constants.INTERNAL_ERROR);
        }
    }

    private void putValutation (boolean valutation) {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            socialManagement.putValutation(new LikeValutation(customer, song, valutation, new Date()));
            Notifier.notifyInfoMessage(context, Constants.INSERTION_VALUTATION_SUCCESSFULLY);
        } catch (CustomerNotFoundException | SongNotFoundException ex) {
            Notifier.notifyErrorMessage(context, Constants.INTERNAL_ERROR);
        }
    } 

    @PostConstruct
    public void init() {
        customer = customerManagement.getCurrentCustomer();
    }

}

thanks!

2

There are 2 answers

1
Natarajan Murugesan On

Define one selectedSong variable in your managed bean with getters and setters.
Use JSF setPropertyActionListener similar to below code.
Remove the argument from your action method.

 <h:commandButton id="selectedButton"
                                     action="#{currentSong.setSong()}"
                                     styleClass="btn"
                                     onclick="parent.showSongDialog();"
                                     style="
                                     background-image: url('#{carouselSelectedSong.coverPath}');
                                     background-size:100%;
                                     width:300px; 
                                     height:300px; 
                                     border: black;">
                        <f:ajax render="songDialogContent"/>
 <f:setPropertyActionListener target="#{currrentSong.selectedSong}" value="#{carouselSelectedSong}" />
                    </h:commandButton>

Assign the selectedSong to carouselSelectedSong in your managed bean action class

 private Song selectedSong;

//getters and setters for selectedSong


 public String setSong() {
        System.out.println("----------------------------------------- current song: " + selectedSong.getTitle());
        this.song = selectedSong;
   return null;
    }

6
Natarajan Murugesan On

I think the method setSong is not executed, that is the problem.

 public void setSong(Song song) {
        System.out.println("----------------------------------------- current song: " + song.getTitle());
        this.song = song;
    }

Normally the action method expects the method should return the String return value. Can you change the method definition like below code then it will work

 public String setSong(Song song) {
        System.out.println("----------------------------------------- current song: " + song.getTitle());
        this.song = song;
   return null;
    }