我有以下评级代码:
<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>
我的支持bean是这样的:
package backupController;
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;
}
我收到的信息是我评价的,但在控制台中,该值被评为空。显示我的xhtml页面上的信息以及评级,但bean没有读取值。
答案 0 :(得分:4)
RequestScoped的宽度不足以达到您想要达到的效果。对于您发出新请求的每个速率,都会将其发送到Web服务器,因此会创建新的请求范围bean。尝试查看scoped bean