1

我有以下评分代码:

<h:form>
    <p:growl id="messages" showDetail="true"></p:growl>
       <h:outputLabel value="Rating:" />
        <p:rating value = "#{ratingBean.rating}" stars="5"  rendered="true">
           <p:ajax event="rate" listener="#{ratingBean.onrate}" update="messages" />  
    </p:rating>
</h:form>

我的支持豆是这样的:

包备份控制器;

import java.io.Serializable;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.RateEvent;


@Named
@RequestScoped
public class RatingBean implements Serializable {


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Integer getRating() {
        System.out.println("You Rated: "+ rating);
        return rating;

    }

    public void setRating(Integer rating) {
        this.rating = rating;
        System.out.println("You Rated re:");

    }


  public void onrate(RateEvent rateEvent) {  
      FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Rate Event", "You rated:" + ((Integer) rateEvent.getRating()).intValue());  

      FacesContext.getCurrentInstance().addMessage(null, message);  
  } 

//    public void oncancel() {  
//        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cancel Event", "Rate Reset");  
//  
//        FacesContext.getCurrentInstance().addMessage(null, message);  
//    }  

    private Integer rating;

}

我收到了我已评级的信息消息,但在控制台中,该值被评为 null。在我的 xhtml 页面上显示信息以及评级,但 bean 没有读取值。

4

1 回答 1

4

RequestScoped 对于您想要实现的目标不够广泛。对于您发出的每个速率,新请求都会发送到 Web 服务器,因此会创建新的请求范围 bean。尝试查看作用域bean

于 2013-09-20T07:18:31.093 回答