Portlets dynamically retrieve the weather conditions of the regions represented by the designated postal coding from the weather service. In addition, the message is no longer hard-coded, but entrusts JSP to process.
Figure 5. Enhanced Weather portlet code sample
Package com.ibm.wps.samples.Weather; import java.io. *; import vendor.service.weather. *; import org.apache.jetspeed.portlet. *; import org.apache.jetspeed.portlets. *; import org.apache.jetspeed.portlet.service *;.?. / ** * WeatherPortlet.java A more extensive example to display weather * / public class WeatherPortlet extends PortletAdapter {protected static final String jsp = "/ WEB-INF / weather / html / DisplayWeather.jsp "; public void init (PortletConfig portletConfig) throws UnavailableException {super.init (portletConfig);} public void doView (PortletRequest request, PortletResponse response) throws PortletException, IOException {// Create a Weather Bean weatherBean weatherBean =. new WeatherBean (); // Get the portlet session PortletSession session = request.getPortletSession ();. // Get the portlet configuration PortletConfig config = getPortletConfig ();. // Get the portlet context PortletContext context = config.getContext (). ; // get portal user object. User user = session.getuser (); // Get the user's full name or null if not available weatherBean.setFullName (user.getFullName ());. // Get the weather service for determining the temperature WeatherService weatherService = (WeatherService) context.getService (WeatherService.class);. // Get the current temperature for the specified zip code weatherBean.setTemperature (weatherService.getTemperature (Integer.parseInt (config.getInitParameter ( "ZipCode"))));.. // Put the bean in the request request.setAttribute ( "weatherBean", Weatherbean; // invoke the jsp to do the actual rendering for the portlet view. Context.include (JSP, Request, Response);}}
Let's take a closer study to examine some important components. The first thing to pay attention to is the WeatherPortlet class no longer inherited the AbstractPortlet class, but inherits the portletadapter class. The portletadapter class is a central portlet abstraction, an implementation of the portlet interface. It provides default empty implementations for the following portlet: init, login, service, logout and destroy. Next, it is important to note that the class is no longer directly overridden service method. Instead, it covers the method associated with a particular portlet mode. Portlet has four modes:
Portlet.Mode.View portlet.mode.edit portlet.mode.help portlet.mode.configure
It can be seen from the names of these modes. For each mode, the PortletAdapter class provides a default empty implementation method. In Figure 5, portlet only supports the View mode, so there is only one corresponding DOVIEW method being overwritten. The default implementation of the service method determines the portlet mode and calls the appropriate mode method. The code snippet in Figure 6 indicates this:
Figure 6. Determine portlet mode
... public void service (PortletRequest request, PortletResponse response) throws PortletException, IOException {// Get the mode of the portlet Portlet.Mode mode = request.getMode ();. If (mode == Portlet.Mode.VIEW) doView (REQUEST, RESPONSE); Else if (Mode == Portlet.mode.edit) Doedit (Request, Response); Else If (Mode == Portlet.Mode.Help) DOHELP (REQUEST, RESPONSE); Else IF (Mode == Portlet.Mode.configure) Doconfigure (request, response);} ...
Let's study what happens when calling portlets to deal with it.
The first thing that happens is to create a WeatherBean instance (Figure 7), which will contain all related data required to display the display, in this example, is the weather conditions of the area represented by the username and pre-configured postal coding. As shown by the code, the user name is retrieved from the User object. To determine the temperature, you first retrieve postal codes from the PortletConfig object (here, our simplified assumption is that this portlet is all configured by the administrator for all users representing the regions representing a postal code). The second thing is to get a reference to the portletService (specifically to this example is to reference the WeatherService object). To limit the complexity of this example, we will assume that weather services have been registered with the portal server, and the portlet can take advantage of this service to get the current weather of the region represented by the specific postal coding. Once these two messages are obtained, they are stored in the WeatherBean.
Figure 7. WEATHERBEAN code sample
Package com.ibm.wps.samples.Weather; / ** * Weatherbean.java? a bean buy to pass data to the jsp display. * / public class weatherbean {/ ** the user's full name. * / private string fullname = "";. / ** The current temperature * / private int degrees = 0; public void setFullName (String name) {fullName = name;} public String getFullName () {return fullName;} public void setTemperature (int degrees) {this PUBLIC INT GETTEMPERATURE ()} PUBLIC INT GETTEMPERATURE () {Return Degrees;} This bean is passed from the PortletApter class to JSP, which will eventually display. This is done by the SetAttribute method of the portletRequest object. The JSP will then use the UseBean tag to establish a reference to the WeatherBean and embed the full name value of the user and the current temperature into the HTML output flag (Markup).
Figure 8. Displayweather code sample
<% @ page contenttype = "text / html" errorpage = ""%>
hi <% = WeatherBean.getFullName () == NULL? ": WeatherBean.getFullName ()%>
The Current Temperature in Your Area is <% = WeatherBean.getTemperature ()%>.
The output of this portlet (Figure 9) looks similar to what we have previously described.
Figure 9. Enhanced Weather Portlet
So far, we only study the portlets that strictly press the browser (such as IE and Netscape, which implement the entire HTML specification). But what if you want to extend portlets to these fat clients, and make the content can be used by a limited device (such as a pocket PC, handheld device or smart phone)? In the following sections, we will study additional API provided by IBM WebSphere Portal, which can help these devices work. We will also study more advanced issues, which are currently not resolved in these APIs, and we will even present the method of increasing the release method. This example does not discuss details of some more advanced portlet API topics, such as window status, event processing, instance data, etc. For information, please refer to WebSphere Portal InfoCenter (see Resources).
Introduce popularization programming
One of the object-oriented design is a model-view-controller, also known as MVC. MVC is simple and surprising, but it is a very useful model that requires developers to construct applications according to the following three basic components:
Model is the primary component, which maintains the status and data model presenting in the application. Controller is a bridge between the model and its view. It is a component that can use it to change the underlying data model. The view (view) is a graphical representation of the data model for the user.
The power of this design is that it provides:
Pilot? The logical components of this design are clearly separated. Memore The various components of the application can be called up in your needs, making debugging work easier. Multiple views? The view is scalable in multiple implementations, which can be used to reflect the same data model. Scalable design? As the application grows, various components can grow while do not affect the underlying application logic.
Figure 10. Model - View - Controller (MVC) Design Mode
Popular PORTLET programming technology
Since we understand the background knowledge of modular methods developed, then we apply it to the popular programming. We can analyze the components in the weather example:
Model: Core weather applications, such as retrieving the current temperature by accessing the full name or weather service of the user. Controller: Supply bean, which connects the data between portlet code and JSP. View: Processing the DISPLAYWEATHER JSP of the graphical display of portlet content.
It is easy to see what the actual display of the handle is to be seen, and the model remains unchanged. The controller between each view is basically constant, but may need to perform some specific custom work in accordance with the desired view. The view component has the flexibility of processing data to make them best suggested. For example, the smart phone displays content in WML (WiReless Markup Language)). Thus, for our weather example, we can define a separate view JSP for each desired device: Define an HTML JSP for your browser to define a WML JSP for your smart phone. Look at the sample portlet in Figure 11, which uses this design mode and the popularity of WebSphere Portal.
Figure 11. Define a separate view for each device
Package com.ibm.wps.samples.Weather; import java.io. *; import com.ibm.wps.portlets. *; import vendor.service.Weather. *; import org.apache.jetspeed.portlet. *; import org.apache.jetspeed.portlets *;. import org.apache.jetspeed.portlet.service *;.. / ** * WeatherBaseController.java * Base controller class containing common controller functionality * / public class WeatherBaseController extends AbstractPortletController {/ ** The View for HTML Devices (EG Internet Explorer, Netscape). * / Protected string jsphtml = "/ web-inf / weather / html /"; / ** The view for WML Devices (EG smartphones). * / Protected string jspwml = "/ WEB-INF / weather / wml /"; public void init (PortletConfig config) throws UnavailableException {super.init (config); // Load the HTML View from the configuration jspHTML = config.getInitParameter ( "view.HTML" ); // load the WML View from the configuration. Jspwml = config.getinitParameter ("view.wml");} protected void createbean (portletRequest Request, portletResponse response) {////// Get the portlet configuration object PortletConfig config = getPortletConfig ();. // Create a Weather Bean WeatherBean weatherBean = new WeatherBean ();. // Set the user's full name or null if not available weatherBean.setFullName (request.getPortletSession (). . .getUser () getFullName ()); // Get the weather service for determining the temperature weatherService weatherService = (weatherService) config.getContext () getService (WeatherService.class);.. // Get the current temperature for the specified zip WeatherBean.SetTemperature (Integer.Parseint (Config.GetInitParameter ("Zipcode"))))
// INSERT Bean INTO The Request for the JSP To Use. Request.SetaTRibute ("Weatherbean", WeatherBean;}} package com.ibm.wps.samples.weather; import java.io. *; import com.ibm.wps .portlets *;. import org.apache.jetspeed.portlet *;. / ** * WeatherHTMLController.java * Controller for calling the HTML View * / public class WeatherHTMLController extends WeatherBaseController {public void doView (PortletRequest request, PortletResponse response) throws. PortletException, IOException {createBean (request, response);. getPortletConfig () getContext () include (jspHTML, request, response);.}} package com.ibm.wps.samples.weather; import java.io. *; import com .ibm.wps.portlets *;. import org.apache.jetspeed.portlet *;. / ** * WeatherWMLController.java * Controller for calling the WML View * / public class WeatherWMLController extends WeatherBaseController {public void doView (PortletRequest request,. PortletResponse response) throws portletException, ioException {createbean (request, response); getportletconf Ig (). getContext (). include (jspwml, request, response);}} This example shows a basic controller that contains the public functionality of HTML and WML views. First, in the init method, load the name of the HTML and WML view JSP from the portlet configuration. Then, a method called CreateBean is provided, which creates a WeatherBean instance and implant the model data to it. Finally, the CreateBean method plugs the bean into the request so that it can be used by the view JSP.
The next two classes (Fig. 12) provide a controller for each view type. The first is the HTML controller, the second is the WML controller. Both controller classes inherit the controller base class to inherit the general function provided by the latter. In addition to simplicity, there is not much difference between the two controllers in addition to the different views of the call. But you can consider providing special functions customized for each device type according to the operation taken by the user. For example, a fat client controller can provide drag and drop control on the icon, while the thin client controller is not.
To make these controllers, you need to register them to the portlet web.xml file, the method is to add the following configuration flags. (Web.xml and portlet.xml files are not in this example. For more information, see the WebSphere Portal InfoCenter documentation for the reference part.)
Figure 12. HTML and WML controller
...
Figure 13. HTML and WML JSP
<% @ page contenttype = "text / html" error "=" "%>
hi <% = weatherbean.getfullname () == null? ": weatherbean.getfullname () %>!
The capital Temperature in Your Area is <% = weatherbean.gettemperature ()%> ------------------------ -------------------------------------------------- ---> - Displayweatherwml.jsp - a JSP View for Rendering WML. -> <% @ Page ContentType = "Text / WML" ErrorPage = ""%>
current temperature is <% = weatherbean.gettemperature ()%>. p> Figure 14 and Figure 15 show How to deal with these portlets on your browser and smartphones.
Figure 14. Weather portlet using HTML JSP
Figure 15. Weather portlet using WML JSP