簡體   English   中英

Wicket:使用setResponsePage重定向到wicket頁面

[英]Wicket: redirecting to wicket page using setResponsePage

我有一個wicket頁面,其中有一個鏈接ADD PRODUCT 點擊鏈接后,會打開一個模態窗口,其中包含產品信息。

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(),在那個窗口里我得到了刷新的頁面。

我的要求是Modal窗口應該關閉,頁面應該刷新。 我在setResponsePage()中傳遞Parentpage.class。

任何幫助和建議表示贊賞! 提前致謝。

在打開模態窗口的ParentPage.class中,我調用了setWindowClosedCallback()方法,其中我將getPage()添加到目標,以便在關閉模式窗口時刷新頁面。 這是相同的代碼

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

           @Override 
           public void onClose(AjaxRequestTarget target) 
           { 
               target.addComponent(getPage()); 
           } 
   });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM