2012-12-19 5 views
5

wicket 페이지에 추가 제품이 있습니다. 링크를 클릭하면 제품 정보를 가져 오는 모달 창이 열립니다.Wicket : setResponsePage를 사용하여 wicket 페이지로 리디렉션

ProductAddPanel.java

public class ProductAddPanel extends Panel { 

private InlineFrame uploadIFrame = null; 
private ModalWindow window; 
private Merchant merchant; 
private Page redirectPage; 
private List<Component> refreshables; 

public ProductAddPanel(String id,final Merchant mct,ModalWindow window,List<Component> refreshables,Page p) { 
    super(id); 
    this.window = window; 
    merchant = mct; 
    redirectPage = p; 
    this.refreshables = refreshables; 
    setOutputMarkupId(true); 
} 

@Override 
protected void onBeforeRender() { 
    super.onBeforeRender(); 
    if (uploadIFrame == null) { 
     // the iframe should be attached to a page to be able to get its pagemap, 
     // that's why i'm adding it in onBeforRender 
     addUploadIFrame(); 
    } 
} 


// Create the iframe containing the upload widget 
private void addUploadIFrame() { 
    IPageLink iFrameLink = new IPageLink() { 
     @Override 
     public Page getPage() { 
      return new UploadIFrame(window,merchant,redirectPage,refreshables) { 
       @Override 
       protected String getOnUploadedCallback() { 
        return "onUpload_" + ProductAddPanel.this.getMarkupId(); 
       } 


      }; 
     } 
     @Override 
     public Class<UploadIFrame> getPageIdentity() { 
      return UploadIFrame.class; 
     } 
    }; 
    uploadIFrame = new InlineFrame("upload", iFrameLink); 
    add(uploadIFrame); 
} 

}

ProductAddPanel.html

<wicket:panel> 
<iframe wicket:id="upload" frameborder="0"style="height: 600px; width: 475px;overflow: hidden"></iframe> 
</wicket:panel> 

나는 이미지를 업로드 할 Iframe을 사용하고 있습니다. 내 ProductPanel.html에 iframe을 추가했습니다. Ajax를 사용하여 파일을 업로드 할 수 없으므로 제출하십시오.

UploadIframe.java

protected void onSubmit(AjaxRequestTarget target, Form<?> form) { 
       DynamicImage imageEntry = new DynamicImage(); 

       if(uploadField.getFileUpload() != null && uploadField.getFileUpload().getClientFileName() != null){ 
        FileUpload upload = uploadField.getFileUpload(); 
        String ct = upload.getContentType(); 

        if (!imgctypes.containsKey(ct)) { 
         hasError = true; 
        } 

        if(upload.getSize() > maximagesize){ 
         hasError = true; 
        } 

        if(hasError == false){ 
         System.out.println("######################## Image can be uploaded ################"); 
         imageEntry.setContentType(upload.getContentType()); 
         imageEntry.setImageName(upload.getClientFileName()); 
         imageEntry.setImageSize(upload.getSize()); 
         if(imageEntry != null){ 
          try { 
           save(imageEntry,upload.getInputStream()); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }else{ 
         target.appendJavaScript("$().toastmessage('showNoticeToast','Please select a valid image!!')"); 
         System.out.println("#################### Error in image uploading ###################"); 
        } 
       }else{ 
        System.out.println("########################### Image not Selected #####################"); 
       } 

       MerchantProduct mp =new MerchantProduct(); 
       Product p = new Product(); 
       Date d=new Date(); 
       try { 

        p.setProductImage(imageEntry.getImageName()); 
        mp.setProduct(p); 

        Ebean.save(mp); 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       for(Component r: refreshables){ 
        target.add(r); 
       } 

       window.close(target); 
       setResponsePage(MerchantProductPage.class); 
      } 

public void save(DynamicImage imageEntry, InputStream imageStream) throws IOException{ 
    //Read the image data 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    copy(imageStream,baos); 
    baos.close(); 
    byte [] imageData = baos.toByteArray(); 
    baos = null; 

    //Get the image suffix 
    String suffix = null; 
    if("image/gif".equalsIgnoreCase(imageEntry.getContentType())){ 
     suffix = ".gif"; 
    }else if ("image/jpeg".equalsIgnoreCase(imageEntry.getContentType())) { 
     suffix = ".jpeg"; 
    } else if ("image/png".equalsIgnoreCase(imageEntry.getContentType())) { 
     suffix = ".png"; 
    } 

    // Create a unique name for the file in the image directory and 
    // write the image data into it. 
    File newFile = createImageFile(suffix); 
    OutputStream outStream = new FileOutputStream(newFile); 
    outStream.write(imageData); 
    outStream.close(); 
    imageEntry.setImageName(newFile.getAbsolutePath()); 

    } 

    //copy data from src to dst 
    private void copy(InputStream source, OutputStream destination) throws IOException{ 
     try { 
       // Transfer bytes from source to destination 
       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = source.read(buf)) > 0) { 
        destination.write(buf, 0, len); 
       } 
       source.close(); 
       destination.close(); 
       if (logger.isDebugEnabled()) { 
        logger.debug("Copying image..."); 
       } 
      } catch (IOException ioe) { 
       logger.error(ioe); 
       throw ioe; 
      } 
     } 

    private File createImageFile(String suffix){ 
     UUID uuid = UUID.randomUUID(); 
     File file = new File(imageDir,uuid.toString() + suffix); 
     if(logger.isDebugEnabled()){ 
      logger.debug("File "+ file.getAbsolutePath() + "created."); 
     } 
     return file; 
    } 
} 

}

내가 링크가있는 "제품을 추가"하는 초기 페이지로 리디렉션 setResonsePage()를 사용하고 있습니다. 그래서 나는 새 제품 정보가있는 새로 고침 된 페이지를 얻습니다.

제 문제는 모달 창이 window.close()에서 닫히지 않고 그 창 내부에서 새로 고친 페이지를 가져 오는 것입니다.

내 요구 사항은 모달 창이 닫아야하고 페이지를 새로 고쳐야한다는 것입니다. 내 setResponsePage()에 Parentpage.class를 전달하고 있습니다.

도움과 조언을 보내 주시면 감사하겠습니다. 미리 감사드립니다.

답변

5

모달 창이 열려있는 ParentPage.class에서 모달 창이 닫힐 때 페이지가 새로 고침되도록 대상에 getPage()를 추가하는 setWindowClosedCallback() 메서드를 호출했습니다. 다음은 같은 코드입니다

modalDialog.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() 
    { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void onClose(AjaxRequestTarget target) 
      { 
       target.addComponent(getPage()); 
      } 
    }); 
관련 문제