5.5.2 Adding a JavaBean
Another way to store information to be accessed and displayed by the View mode JSP is to use a JavaBean. In this exercise, you will add a JavaBean to your project and use it to display information when it is run. JavaBeans are a special type of Java class that contain the business logic of the application. They are used to temporarily store and process data and access back-end resources such as databases.
1. The Java source files used to invoke the JSPs to render content are located in the /Java Resources/JavaSource/helloworld/ folder of your project as seen in the Project Explorer view.
Figure 5-30 Project Explorer view
2. Right-click the helloworld package and select New | Class.
Figure 5-31 Adding a new class
3. Name the class HelloWorldPortletViewBean. Click Finish to add it to your project.
Figure 5-32 Naming the new class
4. The HelloWorldPortletViewBean.java file will now appear under the /Java Resources/JavaSource/helloworld/ folder. Double-click this file for editing. Modify the source code according to the following example. When you are finished, select File | Save All to save your changes. Example 5-2 JavaBean code
public class HelloWorldPortletViewBean {
private String myName = ""; public String getMyName() { return myName; } public void setMyName(String myName) { this.myName = myName; } }
5. Next, you will need to modify the HelloWorldPortlet.java file.You will add code that will use the set information in the bean you just created. This code is inserted in the doView() method of the HelloWorldPortlet.java file. Example 5-3 doView() method code modification
public void doView(PortletRequest request, PortletResponse response) throws PortletException, IOException { //Make a bean HelloWorldPortletViewBean viewBean = new HelloWorldPortletViewBean(); //Set your name viewBean.setMyName("John Doe"); //Save bean in the request so the view jsp can read it request.setAttribute("HelloWorldPortletViewBean", viewBean); // Invoke the JSP to render getPortletConfig().getContext().include(VIEW_JSP+getJspExtension(request), request, response); }
![]()
6. Now that the bean is created and the portlet can successfully store a value in this bean, it is necessary to modify the code to the HelloWorldPortletView.jsp file so that the value can be extracted from the bean and shown on the screen. Double-click the HelloWorldPortletView.jsp file and make the following changes. The useBean tag tells the JSP file that it will be accessing values stored in a JavaBean. Example 5-4 View JSP code modification
<jsp:useBean id="HelloWorldPortletViewBean" class="helloworld.HelloWorldPortletViewBean" scope="request"></jsp:useBean>
<%@ page session="false" contentType="text/html" import="java.util.*, helloworld.*"%> <%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %> <portletAPI:init/> <DIV style="margin: 6px">
<H3 style="margin-bottom: 3px">Welcome!</H3> This is a sample <B>view mode</B> page. You have to edit this page to customize it for your own use.<BR> The source file for this page is "/Web Content/helloworld/jsp/html/HelloWorldPortletView.jsp".
<br> Current time: <%=new java.util.Date() %> <br> Hostname: <%= request.getRemoteHost() %>
<br> Updated by <%= HelloWorldPortletViewBean.getMyName() %>
</DIV>
7. Again save your changes by clicking File | Save All.
8. Again, right-click the HelloWorld project, and select Run | Run on Server. Click Finish on the Server Selection window to run it on the existing test environment server. Your changes will be shown in the Web browser.
Figure 5-33 Portlet with changes
ibm.com/redbooks