【问题标题】:action p:command button doesn't set property into managed bean [duplicate]操作 p:命令按钮未将属性设置为托管 bean [重复]
【发布时间】:2015-09-02 08:50:54
【问题描述】:

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

}

谢谢!

【问题讨论】:

    标签: jsf action managed-bean commandbutton


    【解决方案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;
        }

    【讨论】:

    • 我认为这将帮助您解决问题
    【解决方案2】:

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

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

    通常,操作方法期望该方法应返回字符串返回值。你能像下面的代码那样改变方法定义然后它就可以工作了

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

    【讨论】:

    • 我刚试了,还是不行!
    • 你能在setSong方法里面放断点来检查执行是否落地吗?
    • 方法没有被调用!
    • 我已将其放入表单中,现在看来可行!
    • 但是我在另一个地方也遇到了同样的问题,而且表格是存在的!
    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2019-05-28
    • 2014-07-17
    • 1970-01-01
    相关资源
    最近更新 更多