KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > HelloWorldPortlet


1 package example;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5
6 import javax.portlet.*;
7
8 public class HelloWorldPortlet extends GenericPortlet {
9
10   private String JavaDoc _view = "/hello.jsp";
11
12   /**
13    * Set the jsp for performing the render, the default is "/hello.jsp".
14    */

15   public void setView(String JavaDoc view)
16   {
17     _view = view;
18   }
19
20   public void init(PortletConfig portletConfig)
21     throws PortletException
22   {
23     super.init(portletConfig);
24
25     String JavaDoc p = getInitParameter("view");
26     if (p != null)
27       setView(p);
28   }
29
30   public void processAction(ActionRequest request, ActionResponse response)
31     throws PortletException, IOException JavaDoc
32   {
33     PortletMode mode = request.getPortletMode();
34
35     if (mode.equals(PortletMode.EDIT)) {
36
37       // get the values submitted with the form
38

39       String JavaDoc identity = request.getParameter("identity");
40       String JavaDoc color = request.getParameter("color");
41
42       // set the values of the render parameters
43

44       response.setRenderParameter("identity", identity);
45       response.setRenderParameter("color", color);
46
47       // switch to View mode
48

49       response.setPortletMode(PortletMode.VIEW);
50     }
51   }
52
53   /**
54    * Called just prior to a render, set's the following request attributes:
55    *
56    * <dl>
57    * <dt>identity
58    * <dd>the identity
59    * <dt>color
60    * <dd>the color
61    * </dl>
62    */

63   protected void prepareObjects(RenderRequest request, RenderResponse response)
64   {
65     String JavaDoc identity = request.getParameter("identity");
66     if (identity == null)
67       identity = "World";
68
69     String JavaDoc color = request.getParameter("color");
70     if (color == null)
71       color = "silver";
72
73     request.setAttribute("identity", identity);
74     request.setAttribute("color", color);
75   }
76
77   /**
78    * Dispatch to a jsp or servlet.
79    */

80   protected void dispatch( RenderRequest request,
81                            RenderResponse response,
82                            String JavaDoc path )
83     throws PortletException, IOException JavaDoc
84   {
85     PortletContext ctx = getPortletContext();
86     PortletRequestDispatcher dispatcher = ctx.getRequestDispatcher(path);
87     dispatcher.include(request, response);
88   }
89
90   /**
91    * Call prepareObjects() then dispatch to the view.
92    */

93   public void render(RenderRequest request, RenderResponse response)
94     throws PortletException, IOException JavaDoc
95   {
96     prepareObjects(request, response);
97     dispatch(request, response, _view);
98   }
99
100   public void destroy()
101   {
102   }
103 }
104
105
106
Popular Tags