操作 p:命令按钮未将属性设置为受管 Bean



我有一个问题,我有一个轮播,我动态地将一些数据加载到其中,当我按下轮播对象时,它必须将值设置为一个manangedbean(CurrentSong)并显示一个对话框,现在对话框出现,但set属性不起作用。 为什么?

XHTML 页面:

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

托管 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();
    }
}

谢谢!

在托管 Bean 中定义一个 selectedSong 变量,其中包含 getter 和 setter。
使用 JSF setPropertyActionListener,类似于下面的代码。
从操作方法中删除参数。

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

将所选歌曲分配给轮播所选歌曲在托管 Bean 操作类中

 private Song selectedSong;
//getters and setters for selectedSong
 public String setSong() {
        System.out.println("----------------------------------------- current song: " + selectedSong.getTitle());
        this.song = selectedSong;
   return null;
    }

我认为方法setSong没有执行,这就是问题所在。

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

通常,操作方法期望该方法应返回 String 返回值。您可以像下面的代码一样更改方法定义吗,那么它将起作用

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

相关内容

  • 没有找到相关文章

最新更新