You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Arun Chauhan <ar...@gmail.com> on 2012/12/19 15:24:45 UTC

Wicket: redirecting to wicket page using setResonsePage

I have a wicket page which has a link **ADD PRODUCT**. On clicking the link a
modal window open which takes the product information.

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>

I am using a Iframe to upload the image. I have added a iframe to my
ProductPanel.html. Because it is not possible to upload file using ajax
submit.

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;
	    }
    }
}

I am using setresonsePage() to redirect to initial page on which "Add
Product" link is present. So that i get the refreshed page having new
product information. 

My problem is that modal window is not closing on window.close() and inside
that window i am getting the refreshed page.

My requirement is that Modal window should close and page should be
refreshed. I am passing the Parentpage.class in my setresponsePage().

Any help and advices appreciated! Thanks in advance. 



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-redirecting-to-wicket-page-using-setResonsePage-tp4654935.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket: redirecting to wicket page using setResonsePage

Posted by Arun Chauhan <ar...@gmail.com>.
Hi,

finally i got a solution in wicket. In ParentPage.class I am calling
setWindowClosedCallback and in that i have wriiten code for refrehing the
ParentPage when modal window closes.

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

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



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-redirecting-to-wicket-page-using-setResonsePage-tp4654935p4655037.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket: redirecting to wicket page using setResonsePage

Posted by Arun Chauhan <ar...@gmail.com>.
Thanks Ernesto. U are right.. :)



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-redirecting-to-wicket-page-using-setResonsePage-tp4654935p4654939.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket: redirecting to wicket page using setResonsePage

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Hi,

On Wed, Dec 19, 2012 at 3:42 PM, Arun Chauhan <ar...@gmail.com> wrote:

> Thanks Ernesto for nice suggestion. I will give it a try.. :)
>
> Is there a way to do it in wicket code only. Because till now I haven't
> used
> much javascript in my application.
>

No,  I don't know of a solution without using some JavaScript. You can
encapsulate this scrip on a behavior that you add to your page.

By the way, if you are using ModalWindow and AJAX you are already using a
lot of JavaScript on your application;-)


>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Wicket-redirecting-to-wicket-page-using-setResonsePage-tp4654935p4654937.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Regards - Ernesto Reinaldo Barreiro
Antilia Soft
http://antiliasoft.com/ <http://antiliasoft.com/antilia>

Re: Wicket: redirecting to wicket page using setResonsePage

Posted by Arun Chauhan <ar...@gmail.com>.
Thanks Ernesto for nice suggestion. I will give it a try.. :)

Is there a way to do it in wicket code only. Because till now I haven't used
much javascript in my application.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-redirecting-to-wicket-page-using-setResonsePage-tp4654935p4654937.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket: redirecting to wicket page using setResonsePage

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Hi,

Add script

 <script type="text/javascript"><!--
        if (top != self) {
          var wicket = top.Wicket;
          if (wicket && wicket.Window) {
            var modal = wicket.Window.get();
            if (modal) {
              modal.close();
            }
          }
          top.location.href = location.href;
        }
      //--></script>

]

to Parentpage.class

See

http://comments.gmane.org/gmane.comp.java.wicket.user/86671


where solution was proposed


On Wed, Dec 19, 2012 at 3:24 PM, Arun Chauhan <ar...@gmail.com> wrote:

> I have a wicket page which has a link **ADD PRODUCT**. On clicking the
> link a
> modal window open which takes the product information.
>
> 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>
>
> I am using a Iframe to upload the image. I have added a iframe to my
> ProductPanel.html. Because it is not possible to upload file using ajax
> submit.
>
> 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;
>             }
>     }
> }
>
> I am using setresonsePage() to redirect to initial page on which "Add
> Product" link is present. So that i get the refreshed page having new
> product information.
>
> My problem is that modal window is not closing on window.close() and inside
> that window i am getting the refreshed page.
>
> My requirement is that Modal window should close and page should be
> refreshed. I am passing the Parentpage.class in my setresponsePage().
>
> Any help and advices appreciated! Thanks in advance.
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Wicket-redirecting-to-wicket-page-using-setResonsePage-tp4654935.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Regards - Ernesto Reinaldo Barreiro
Antilia Soft
http://antiliasoft.com/ <http://antiliasoft.com/antilia>