2017-09-30 56 views
0

我正在用JSF和PrimeFaces創建一個圖片庫。我正在使用ui:在p:graphicImage裏面重複顯示從db中檢索的圖像列表。每個圖像都有一個點擊p:對話框(及其各自的id),也在ui:repeat內定義。在p:對話框中,我再次顯示點擊的圖像,並且還有一個h:表單,它內部有一個p:inputText和一個p:commandbutton,用於將p:inputText的文本保存到bean的String屬性中。問題是隻有ui顯示的列表的最後一個圖像:重複「看到」bean並設置屬性。如果在ui顯示的最後一個圖像的對話框中:重複我寫一個註釋,然後單擊commandbutton它設置bean的String文本,如果我對其他圖像執行相同的操作,則String文本爲null。也許這是一個bean可見性的問題。我試圖爲bean使用不同的作用域,但它無法正常工作。p:inputText裏面的UI:重複只適用於最後一個元素

這是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> 

這是豆(JAVA):我用代碼C]解決

@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)); 
     } 
    } 
} 
+1

你不應該爲每個迭代創建一個對話框。把它放在外面,讓變量控制它。我在這裏看到的問題雖然沒有在你的代碼中顯示,但你可能有一個圍繞ui的表單:repeat,並且你的對話框裏面還有一個表單,你不能在另一個表單裏面有一個表單。 –

+0

感謝您的回答。 ui:重複不在表單內,只有對話框中的表單。我已經試圖把對話框放在UI之外:重複,但問題是我需要將相應圖像的ID傳遞給對話框,並且如果對話框在外面則不可能。我該怎麼做? – Euleo

+0

嘗試從PostConstruct註釋的方法init中刪除'throws SQLException'。走着瞧吧。 –

回答

0

:JSTL,而不是用戶界面的forEach:重複。有用!!

相關問題