Accessing portlet API objects from JSP files
Add attributes to portlet API objects (such as PortletData and PortletPreferences); add controls to portlet JSP files to add, modify or delete the attributes; and insert controls to access attributes or bind attributes to existing controls in portlet JSP files.
The IBM® portlet API and the JSR 168 portlet API define portlet API objects that encapsulate data for portlet objects. For instance, the IBM portlet API defines a PortletData object that, when combined with a concrete portlet, defines a concrete portlet instance. In the JSR 168 portlet API, a PortletPreferences object is combined with a portlet object to define a portlet entity.
Use the Page Data view of a portlet JSP file to:
- Add attributes to any of the portlet API object types listed below.
- For IBM portlet API projects
- PortletApplicationSettings
- PortletSettings
- PortletData
- User
- For JSR 168 and JSR 286 portlet API projects
- PortletPreferences
- USER_INFO
- Insert or add controls in the JSP file to display, add, modify or delete the values of the attributes
Follow the steps below to add attributes to the portlet objects and then add controls to a JSP file to create, modify, delete or display the data:
- Open a portlet JSP file by double-clicking on it in the Project Explorer view.
- Open the Page Data view (you may need to change to the web perspective), located in the bottom left corner of the workbench. There are several stacked views in that corner, so you may need to tab over to see the Page Data view.
- In the Page Data view, expand
Portlet.
- Highlight one of the portlet API objects.
- Right-click
New | selected_object_type | Add Attribute.
- Provide the name of the attribute.
- For PortletData attributes, which are available using the IBM portlet API, also select the Java™ type of the attribute. All other types of data objects, for both portlet APIs, only allow java.lang.String as the Java type.
Note: If you are using a complex (non-String) Java type for a PortletData attribute, do not drop or drag the variable onto a JSP page. Instead, use the <jsp:usebean> tag, as shown in this example:
<jsp:useBean id="date" class="java.util.Date" scope="request"> <% date = (java.util.Date)portletRequest.getData().getAttribute("date"); %> </jsp:useBean>You can use the date bean to access the data (for example, ${date.year}). To store the variable in the PortletData object, the portlet must be in edit mode and should call
<% portletRequest.getData().setAttribute("date", new java.util.Date()); %>or equivalent.
- Click OK.
- In the Page Data view, expand the portlet API object where you created the new attribute. The new attribute object is listed.
- Drag the attribute object from the Page Data view onto the JSP file in the editor area. The Insert Record wizard opens.
- On the
Configure Data Controls page of the wizard, select one of these choices under
Create controls for:
- Displaying an existing record (read-only)
- This option provides code to display the value of the attribute. For each attribute listed in the
Fields to display box, you can select how the attribute will be displayed by selecting a Control Type from the drop down menu. Choices are listed below:
- Output field
- Output link (an output field within a hyperlink).
- Updating an existing record
- This option provides a form with Submit and Delete buttons to alter the value of the attribute. For each attribute listed in the
Fields to display box, you can select how the attribute will be displayed by selecting a Control Type from the drop down menu. Choices are listed below:
- Input field
- Combo box
- Output field
- Output link (an output field within a hyperlink).
- Creating a new record
- This option provides a form with a Submit button to create an attribute. For each attribute listed in the
Fields to display box, you can select how the attribute will be displayed by selecting a Control Type from the drop down menu. Choices are listed below:
- Input field
- Combo box
- Select the attributes to add to the page from the list under
Fields to display.
- Click
Options to set any of the following values:
Buttons: Use the buttons tab to select or deselect Submit and Delete buttons and to provide labels for the buttons. The buttons tab only appears for the update and create options, not for the display option. The Delete button is only available with the update option.
Label formatting: Use the label formatting tab to select the alignment (left, right or center). There are also choices for
- Appending a colon to each label. This option is on by default.
- Capitalizing the first letter of each label. This option is on by default.
Advanced: Use the advanced tab to set the following options:
- Depth. This value refers to how deep into the object hierarchy to retrieve attributes for display. For instance, you might have an object with an array field containing more objects, which might have more arrays and objects. The default depth value is 3.
- Select
Use as default depth to use the Depth value as the default. This option is off by default.
- Select
Only create data controls to create the underlying code, but omit the labels. This option is off (labels are created) by default.
- Click OK to save the options.
- Click Finish to add the controls to the JSP file.
If you want to add additional controls for an attribute to the JSP file, right-click on the attribute in the Page Data view and select
Insert New Controls.
Related concepts
Related tasks
Testing and debugging portlets