action p:命令按钮不会将属性设置为托管bean

时间:2015-06-17 08:27:39

标签: jsf action managed-bean commandbutton

我有一个问题,我有一个旋转木马,我动态加载一些数据,当我按下旋转木马对象时,它必须将值设置为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();
    }

}

谢谢!

2 个答案:

答案 0 :(得分:-1)

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

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

通常,action方法期望该方法应返回String返回值。你可以改变方法定义,如下面的代码然后它将工作

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

答案 1 :(得分:-1)

使用getter和setter在托管bean中定义一个selectedSong变量 使用类似于下面代码的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>

将selectedSong分配给托管bean操作类

中的carouselSelectedSong

 private Song selectedSong;

//getters and setters for selectedSong


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