我希望能够预订/删除预订产品页面。 保留没问题,遗憾的是不能......
@ManagedBean(name = "wardrobeDetailsBean")
@SessionScoped
//@RequestScoped
public class WardrobeDetailsBean implements Serializable {
@Inject
WardrobeService wardrobeService;
@Inject
AuthBean authBean;
// other fields + setters, getters
@ManagedProperty("#{param.id}")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsernameReserving() {
this.selectedWardrobe = wardrobeService.getWardrobeById(id);
String usernameById = selectedWardrobe.getReservingUserId().getUsername();
StringBuilder sb = new StringBuilder();
sb.append(usernameById.substring(0, 2));
sb.append("...");
sb.append(usernameById.substring(usernameById.length() - 2, usernameById.length()));
return sb.toString();
}
@PostConstruct
public void init() {
if (FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
dbImage1 = new DefaultStreamedContent();
} else if (id != null) {
this.selectedWardrobe = wardrobeService.getWardrobeById(id);
}
}
}
public String reserve() {
this.selectedWardrobe = wardrobeService.getWardrobeById(id);
if (authBean.getUser() != null && authBean.getUser().getId() != null) {
selectedWardrobe.setReservingUserId(authBean.getUser());
wardrobeService.reserveProduct(selectedWardrobe);
return null;
} else {
return "/login.xhtml?faces-redirect=true";
}
}
public void unreserve() {
this.selectedWardrobe = wardrobeService.getWardrobeById(id);
selectedWardrobe.setReservingUserId(null);
wardrobeService.reserveProduct(selectedWardrobe);
}
}
productView.xhtml
<h:commandButton id="reserve" styleClass="btn-u btn-u-sea-shop btn-u-lg" rendered="#{wardrobeDetailsBean.selectedWardrobe.reservingUserId eq null}"
value="Reserve" action="#{wardrobeDetailsBean.reserve()}">
<f:ajax execute="@form" render="reservePanel"/>
</h:commandButton>
<h:outputText value="This product has been reserved by #{wardrobeDetailsBean.usernameReserving}" rendered="#{wardrobeDetailsBean.selectedWardrobe.reservingUserId ne null}"/>
<br />
<input type="hidden" name="remoteUser" value="#{request.remoteUser}"/>
// added using guidance from the answers
<h:commandButton id="unreserve" styleClass="btn-u btn-u-red btn-u-lg" rendered="#{request.remoteUser eq wardrobeDetailsBean.selectedWardrobe.reservingUserId.username}"
value="Unreserve" action="#{wardrobeDetailsBean.unreserve()}">
<f:ajax execute="@form" render="reservePanel"/>
</h:commandButton>
当我删除rendered="#{request.remoteUser eq wardrobeDetailsBean.selectedWardrobe.reservingUserId.username}"
时,它可以正常工作。
单击预约时,应该只显示要删除的按钮。 有什么不对,它不起作用?记录我没有的任何错误。
点击“预约”。该产品已被保留,按钮将被交换。
我单击“取消保留”但仍然没有调用该方法,我丢失了有关产品的信息(刷新页面返回有关产品的信息,但产品仍然保留)
答案 0 :(得分:2)
在请求到达bean方法之前,在提交表单的过程中评估按钮的呈现属性,
当你在渲染方法中检查request.remoteUser
时,对于新请求(操作后请求)它将为空,渲染的评估将为“false”,因此不会调用您的方法。
因此,在表单提交过程中,您必须确保按钮及其父项的呈现属性保持为true。
这是移除渲染属性的原因,它可以正常工作。
作为一种解决方法,您可以向表单添加隐藏的输入:
<input type="hidden" name="remoteUser" value="#{request.remoteUser}"/>
<input type="hidden" name="id" value="#{param.id}"/>
更新:另一种解决方案
另一个解决方案是使用一个按钮,但使用条件值属性和切换方法:
<h:commandButton id="toggle"
styleClass="btn-u btn-u-#{(wardrobeDetailsBean.selectedWardrobe.reservingUserId eq null) ? 'sea-shop' : 'red' } btn-u-lg"
value="#{(wardrobeDetailsBean.selectedWardrobe.reservingUserId eq null) ? 'Reserve' : 'Unreserve'}"
action="#{wardrobeDetailsBean.toggle()}">
<f:ajax execute="@form" render="reservePanel"/>
</h:commandButton>
在你的bean中,使用切换方法:
public String toggle() {
this.selectedWardrobe = wardrobeService.getWardrobeById(id);
if(selectedWardrobe.getReservingUserId() == null){
if (authBean.getUser() != null && authBean.getUser().getId() != null) {
selectedWardrobe.setReservingUserId(authBean.getUser());
wardrobeService.reserveProduct(selectedWardrobe);
return null;
} else {
return "/login.xhtml?faces-redirect=true";
}
}else{
this.selectedWardrobe = wardrobeService.getWardrobeById(id);
selectedWardrobe.setReservingUserId(null);
wardrobeService.reserveProduct(selectedWardrobe);
}
}