Results 1 to 2 of 2
  1. #1
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Wicket: Filling TextArea using AJAX somewhere on the page

    While getting to know Apache Wicket I'm trying to port a simple page from running now with JavaScript and JSP to Wicket. The page itself contains an unlimited number of links whick fill (onClick) als the same TextArea on this page. With Wicket I only get it working when using the Class "Link", but I would prefer to use "AjaxLink" instead.

    The simplified HTML (only one link) is:
    XML Code:
    <html>
    <body>
    	<a href="#" wicket:id="link">Fill Textarea</a> 
    	<div id="logarea">
    		<textarea style="color: red; background-color: lightgray"
    			wicket:id="textarea" name="text" cols="120" rows="25"></textarea>
    	</div>
    </body>
    </html>
    For use with "Link" I running this code:
    Java Code:
    public class LINKpage extends WebPage {
    	private TextArea textarea;
    	private LINKpage homePage;
    	private String log="Hallo";
    	private int count=0;
    	public LINKpage() {
    		homePage=this;
    		
    		textarea = new TextArea("textarea", Model.of(String.format("%s\n%s",count, log)));
    		add(textarea);
    		
    		add(new Link("link") {
    
    			public void onClick() {
    				File logFile = new File("filename");
    				StringBuffer lineBuffer = new StringBuffer();
    				count++;
    				try {
    					BufferedReader br = new BufferedReader(new FileReader(logFile));
    					String line;
    					while ((line=br.readLine()) !=null ) {
    						lineBuffer.append(line+"\n");
    					}
    					log=lineBuffer.toString();
    				} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				textarea = new TextArea("textarea", Model.of(String.format("%s\n%s",count, log)));
    				homePage.replace(textarea);
    			}
    		});
    	}
    }
    For AJAX I'm trying this:
    Java Code:
    public class SAXpage extends WebPage {
    	private static final Logger LOGGER = LoggerFactory.getLogger(Tools.getClassName());
    	private TextArea textarea;
    	private SAXpage homePage;
    	private String log="Hallo";
    	private int count=0;
    	public SAXpage() {
    		homePage=this;
    		
    		add(new AjaxFallbackLink("link") {
    
    			public void onClick(AjaxRequestTarget target) {
    				File logFile = new File("<filename>");
    				StringBuffer lineBuffer = new StringBuffer();
    				count++;
    				try {
    					BufferedReader br = new BufferedReader(new FileReader(logFile));
    					String line;
    					while ((line=br.readLine()) !=null ) {
    						lineBuffer.append(line+"\n");
    					}
    					log=lineBuffer.toString();
    					LOGGER.info("LOG={}", log);
    				} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				if (target != null) {
    					target.addComponent(textarea);
    				}
    			}
    
    		});
    		textarea = new TextArea("textarea", new Model(String.format("%s\n%s",count, log)));
    		textarea.setOutputMarkupId(true);
    		add(textarea);
    
    	}
    }
    The AJAX-approach is based on a sample in "Wicket in Action" (page 20). (Caution: The method "AjaxRequestTarget.addComponent()" is no longer available in current Wicket-Releases to get it tested I have run the sampel with Wicket 1.3.7. But neither this nor "AjaxRequestTarget.add()" in 6.19.0 is working).

    I'm lacking a fundamental understanding here and will appreciate some helpful comments.

  2. #2
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Wicket: Filling TextArea using AJAX somewhere on the page

    Got a solution:
    Java Code:
    public class SAXpage extends WebPage {
    	private TextArea textarea;
    	private SAXpage homePage;
    	private String log="Hello";
    	private int count=0;
    	public SAXpage() {
    		homePage=this;
    		
    		add(new AjaxFallbackLink("link") {
    
    			public void onClick(AjaxRequestTarget target) {
    				File logFile = new File("<filename>");
    				StringBuffer lineBuffer = new StringBuffer();
    				count++;
    				try {
    					BufferedReader br = new BufferedReader(new FileReader(logFile));
    					String line;
    					while ((line=br.readLine()) !=null ) {
    						lineBuffer.append(line+"\n");
    					}
    					log=lineBuffer.toString();
    					LOGGER.info("LOG=\n{}", log);
    				} catch (FileNotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				if (target != null) {
    					target.addComponent(textarea);
    				}
    			}
    
    		});
    		textarea = new TextArea("textarea", new Model() {
    			  @Override
    		      public Object getObject() {
    		        return String.format("%s\n%s",count, log);
    		      }	
    		});
    		textarea.setOutputMarkupId(true);
    		add(textarea);
    	}
    I'm not really how this works; either the Model supervises its source and when changed rendering is done again or renedering is done again with every client request and if source becomes changed the change is displayed by chance.

Similar Threads

  1. Wicket and / or Spring
    By qfactors in forum Spring
    Replies: 0
    Last Post: 11-19-2013, 10:46 AM
  2. Replies: 0
    Last Post: 08-08-2013, 10:41 PM
  3. simple wicket example won't run
    By ronr34 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-27-2011, 11:08 AM
  4. Use Ajax to refresh page without postback
    By hungleon88 in forum Java Servlet
    Replies: 0
    Last Post: 11-27-2009, 02:27 PM
  5. refresh the jsp page(ajax)
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-28-2008, 09:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •