[英]p:inputText inside ui:repeat works only for last element
I am creating an image gallery with JSF and PrimeFaces. 我正在使用JSF和PrimeFaces创建图像库。 I am using ui:repeat with p:graphicImage inside to display a list of images retrieved from db.
我正在将ui:repeat与p:graphicImage一起使用,以显示从数据库检索的图像列表。 Every image has an on click p:dialog (with its respective id) defined also inside ui:repeat.
每个图像在ui:repeat内都定义了一个单击p:dialog(具有其各自的ID)。 In the p:dialog I am showing again the clicked image and there is also ah:form that has inside ap:inputText and ap:commandbutton to save the text of p:inputText to a String property of a bean.
在p:dialog中,我再次显示单击的图像,还有ah:form在ap:inputText和ap:commandbutton内部,用于将p:inputText的文本保存到bean的String属性。 The problem is that only last image of the list showed by ui:repeat "sees" the bean and set the property.
问题在于,只有ui:repeat显示的列表的最后一个图像“看到”该bean并设置属性。 If in the dialog of last image showed by ui:repeat I write a comment and click the commandbutton it sets the String text of the bean, if I do the same for the other images the String text is null.
如果在ui:repeat显示的最后一个图像的对话框中,我写了一条注释并单击命令按钮,它设置了bean的String文本,如果我对其他图像也这样做,则String文本为null。 Maybe it's a problem of bean visibility.
也许这是bean可见性的问题。 I tried to use different scopes for the bean but it doesn't work anyway.
我试图为bean使用不同的作用域,但无论如何它都行不通。
This is the JSF code: 这是JSF代码:
<ui:repeat value="#{imageShowController.images}" var="img">
<h:outputLink value="javascript:void(0)"
onclick="PF('picDialog-#{img.id}').show();">
<p:graphicImage value="#{imageShowController.streamedContent}"
width="250" height="250" cache="false">
<f:param name="id" value="#{img.id}" />
</p:graphicImage>
</h:outputLink>
<p:dialog id="picDialog-#{img.id}" widgetVar="picDialog-#{img.id}"
width="500" height="500">
<p:graphicImage value="#{imageShowController.streamedContent}">
<f:param name="id" value="#{img.id}" />
</p:graphicImage>
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<p:inputText value="#{imageShowController.txt}" />
<p:commandButton value="Submit comment"
action="#{imageShowController.saveComment()}">
<f:param name="id" value="#{img.id}" />
</p:commandButton>
</h:panelGrid>
</h:form>
</p:dialog>
</ui:repeat>
This is the bean (Java): 这是bean(Java):
@ManagedBean
@RequestScoped
public class ImageShowController {
@Inject
private UserSessionBean userSession;
@Inject
private ImageDaoService imagedao;
@Inject
private CommentDaoService commentdao;
private List<Image> images;
private String text;
private String id;
@PostConstruct
public void init() throws SQLException {
images = new ArrayList<>();
images = imagedao.findImagesByUserId( userSession.getUserId() );
}
public void saveComment(){
FacesContext context = FacesContext.getCurrentInstance();
String id =
context.getExternalContext().getRequestParameterMap().get("id");
Comment comment = new Comment();
comment.setText(text);
comment.setDate(new Date());
comment.setImage(imagedao.findById(Long.valueOf(id)).get(0));
commentdao.addComment(comment);
}
public StreamedContent getStreamedContent() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
}
else {
id =
context.getExternalContext().getRequestParameterMap().get("id");
System.out.println("INDEX: "+id);
byte [] b = null;
for (int i = 0; i < images.size(); i++) {
if(images.get(i).getId() == Long.valueOf(id)){
b = images.get(i).getPicture();
break;
}
}
return new DefaultStreamedContent(new ByteArrayInputStream(b));
}
}
}
I solved using tag c:forEach of JSTL instead of ui:repeat. 我使用JSTL的标签c:forEach而不是ui:repeat解决了。 It works!!
有用!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.