我在尝试从p:galleria组件内部进行命令链接时遇到问题问题是尽管在运行时链接值value="Show present #{present.name} #{present.presentId}"
包含id的正确值作为示例value="Show present Foo 1"
,当按下命令链接时它发送错误的id每次第二个对象
<h:form>
<p:galleria value="#{presentBean.allPresentList}" var="present" panelWidth="500" panelHeight="313" showCaption="true">
<f:facet name="content">
<h:commandLink value="Show present #{present.name} #{present.presentId}" action="pretty:present" actionListener="#{presentBean.setPresentObj}">
<f:attribute name="present" value="#{present.presentId}"/>
</h:commandLink>
</f:facet>
</p:galleria>
</h:form>
@ManagedBean(name="presentBean")
@SessionScoped
public class PresentBean implements Serializable{
ArrayList<Present> allUserPresentList = new ArrayList<Present>();
@PostConstruct
private void usersPresent(){
PresentDao presentDao = new PresentDaoImpl();
allPresentList = (ArrayList<Present>) presentDao.findAllPresents();
}
public ArrayList<Present> getAllUserPresentList() {
return allUserPresentList;
}
public void setAllUserPresentList(ArrayList<Present> allUserPresentList) {
this.allUserPresentList = allUserPresentList;
}
private String presentId ;
public String getPresentId() {
return presentId;
}
public void setPresentId(String presentId) {
this.presentId = presentId;
}
public void setPresentObj(ActionEvent ev){
Object presentOb = ev.getComponent().getAttributes().get("present");
if(presentOb != null){
this.presentId = (String) presentOb;
}else{
presentId = null ;
}
}
}
您需要使用setPropertyActionListener而不是<f:attribute name="present" value="#{present.presentId}"/>
,因为f:attribute标记仅在创建组件时(仅一次)评估,而不是在组件基于迭代行生成html时评估。
所以你需要改为使用:
<f:setPropertyActionListener target="#{presentBean.presentId}" value="#{present.presentId}" />
这将在托管bean中设置presentId的值,因此在您的操作方法中,您只需访问presentId本身而无需进行操作。
或者,如果您使用的是更高版本的JSF(使用Servlet 3.0或更高版本),那么您可以在托管bean中创建一个方法,该方法将presentId甚至当前对象作为参数
例如在您的托管bean中:
public void myAction(Present p){
//do whatever you want with the Present object
}
在你的.xhtml中:
<h:commandLink value="Show present #{present.name} #{present.presentId}" actionListener="#{presentBean.myAction(present)}">
</h:commandLink>