KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > tutorials > comp > HelloWorld1


1 package org.enhydra.barracuda.tutorials.comp;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7
8 import org.w3c.dom.*;
9 import org.enhydra.xml.xmlc.*;
10
11 import org.enhydra.barracuda.core.comp.*;
12 import org.enhydra.barracuda.core.comp.helper.*;
13 import org.enhydra.barracuda.core.util.dom.*;
14 import org.enhydra.barracuda.tutorials.xmlc.*;
15
16 /**
17  * Here's an example of HelloWorld using the BTemplate
18  * component
19  */

20 public class HelloWorld1 extends ComponentGateway {
21
22     //xmlc factory (ok as static because its threadsafe)
23
// private static XMLCFactory xmlcFactory = null;
24

25     //-------------------- ComponentGateway ----------------------
26
/**
27      * Handle the default HttpRequest.
28      */

29     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
30         //load the DOM object and find the target node
31
// if (xmlcFactory==null) xmlcFactory = new XMLCStdFactory(this.getClass().getClassLoader(), new StreamXMLCLogger());
32
// XMLObject page = xmlcFactory.create(HelloWorld1HTML.class);
33

34         //load the localized DOM template
35
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(HelloWorld1HTML.class, vc.getViewCapabilities().getClientLocale());
36         Node node = page.getElementById("HelloWorld");
37
38         //create the component and bind it to the view and the model
39
TemplateView tv = new DefaultTemplateView(node);
40         TemplateModel tm = new HelloWorldModel();
41         BTemplate templateComp = new BTemplate(tm);
42         templateComp.setView(tv);
43
44         //add the component to the root
45
root.addChild(templateComp);
46     
47         //return the page
48
return page;
49     }
50
51     //--------------- HelloWorldModel ------------------------
52
/**
53      * HelloWorldModel
54      */

55     class HelloWorldModel extends AbstractTemplateModel {
56         //register the model by name
57
public String JavaDoc getName() {
58             return "HelloWorld";
59         }
60         
61         //provide items by key
62
public Object JavaDoc getItem(String JavaDoc key) {
63             ViewContext vc = getViewContext();
64             if (key.equals("Title")) return "Hello World 1 example";
65             else if (key.equals("Descr")) return "This example demonstrates the simplest way to use the BTemplate component. Here we are implementing the Model manually as an inner class.";
66             else if (key.equals("Hello")) return "Hello World! Hi Ma! Hi Pa!";
67             else return super.getItem(key);
68         }
69     }
70 }
71
Popular Tags