This sample shows a JSP TableView with an action provider, which involves creating a TableViewComposite. The TableViewComposite is a TransformationBeanTM that is composed of other TransformationBeans that include the following subcomponents: MenuBar, NavigationBar, TableView, Title and Footer.
This sample will demonstrate how to change the position of the TableView's sort Actions such that clicking on a column header cell will cause a sort to occur for that particular column. As shown on the Name column in the table on the Output tab in this sample, an up or down image will indicate which direction the column is currently sorted.
Here are the steps for creating a simple TableView:
The code shown in the Full Code tab will create a TableModel and ActionProvider and display a default TableViewComposite with that model and ActionProvider.
The sample is based on the Model 2 Servlet template that is available in SAS AppDev Studio. For simplicity, this sample uses its own static data model for the TableView. In most real-world use cases, users will connect to live data and not create their own data model. For more information about how to connect the TableView to live data, see Sample 25997.
This sample contains one customization for controlling the number of rows and columns that are displayed on one page. To make this customization, add the rowPageSize and columnPageSize attributes to the <sas:TableView> custom tag.
<sas:TableViewComposite id="sas_TableView1" model="sas_model" actionProvider="sas_actionProvider" scope="session">
<sas:RelationalMenuBar />
<sas:TableView rowPageSize="3" columnPageSize="8"/>
</sas:TableViewComposite>
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a Web application project and testing a Web application, see SAS Note 32218.
<%@ taglib uri="http://www.sas.com/taglib/sas" prefix="sas" %>
<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<link href="styles/sasComponents.css" rel="STYLESHEET" type="text/css">
</head>
<body>
<sas:TableViewComposite ref="tableView" scope="session" render="true">
<sas:RelationalMenuBar />
</sas:TableViewComposite>
</body>
</html>
|
package servlets;
public class Model2ExampleControllerServlet.java ...
sas_actionProvider.setControllerURL(request.getContextPath()+ "/Model2Example");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Model2ExampleViewer.jsp");
package servlets;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.sas.actionprovider.ActionOrderList;
import com.sas.actionprovider.BaseAction;
import com.sas.actionprovider.HttpActionProvider;
import com.sas.actionprovider.support.ActionProviderSupportTypes;
import com.sas.actionprovider.support.tableview.HttpTableViewSupport;
import com.sas.commands.relational.SortCommand;
import com.sas.servlet.tbeans.StyleInfo;
import com.sas.servlet.tbeans.dataselectors.html.RelationalMenuBar;
import com.sas.servlet.tbeans.menubar.html.MenuBar;
import com.sas.servlet.tbeans.tableview.html.TableView;
import com.sas.servlet.tbeans.tableview.html.TableViewComposite;
import com.sas.swing.models.SortableTableModelAdapter;
public class Model2ExampleControllerServlet extends
javax.servlet.http.HttpServlet {
// Declare a default version ID since parent class implements
// java.io.Serializable
private static final long serialVersionUID = 1L;
/*
* doPost() Respond to the Post message.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Note: Calling doGet to provide same behavior to POST and GET HTTP
// methods.
doGet(request, response);
}
/*
* doGet() Respond to the Get message.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Note: Add User DO_GET code here
HttpSession session = request.getSession();
// Setup the ActionProvider
HttpActionProvider actionProvider = null;
synchronized (session) {
if (session != null) {
actionProvider = (HttpActionProvider) session
.getAttribute("actionProvider");
}
// if ActionProvider is null, create one and put it on the session
if (actionProvider == null) {
actionProvider = new HttpActionProvider();
actionProvider.setLocale(request.getLocale());
actionProvider.setControllerURL(request.getContextPath()
+ "/Model2Example");
actionProvider.setName("actionProvider");
// store object in its scope
if (session != null)
session.setAttribute("actionProvider", actionProvider);
}
// else execute the ActionProvider command
else {
actionProvider.executeCommand(request, response, response
.getWriter());
}
}
TableViewComposite tableView = null;
synchronized (session) {
if (session != null) {
tableView = (TableViewComposite) session
.getAttribute("tableView");
}
if (null == tableView) {
tableView = new TableViewComposite();
String columnNames[] = { "Name", "Age", "Sex", "Hobby" };
String data1[] = { "Bob", "42", "M", "Sailing" };
String data2[] = { "Mary", "33", "F", "Gardening" };
String data3[] = { "Jane", "27", "F", "Shopping" };
String data4[] = { "Tom", "65", "M", "Reading" };
String data5[] = { "Dave", "23", "M", "Skiing" };
Object data[][] = new Object[5][4];
data[0] = data1;
data[1] = data2;
data[2] = data3;
data[3] = data4;
data[4] = data5;
TableModel defaultTableModel = new DefaultTableModel(data,
columnNames);
SortableTableModelAdapter adapter = new SortableTableModelAdapter(defaultTableModel);
tableView.setModel(adapter);
// set the actionProvider on the table
tableView.setActionProvider(actionProvider);
session.setAttribute("tableView", tableView);
// Create a custom ActionOrderList for the COLUMN_HEADER_AREA and add the
// appropriate actionTypes. All the default actionTypes that we are not including
// on this ActionOrderList ( e.g. MOVE_LEFT and MOVE_RIGHT ) will essentially be
// hidden to the viewer.
ActionOrderList columnHeaderAOL= new ActionOrderList(ActionOrderList.ROOT_NAME,3);
columnHeaderAOL.add(HttpTableViewSupport.SORT_COLUMN_ASCENDING_ACTION);
columnHeaderAOL.add(HttpTableViewSupport.SORT_COLUMN_DESCENDING_ACTION);
columnHeaderAOL.setEnabledIfEmpty(false);
// If custom ActionOrderList is suitable for all TableView components that use
// this instance of the ActionProvider, then we could specify null for the viewer argument
// instead of an instance of the TableView as we do here.
TableView table = (TableView)tableView.getComponent(tableView.TABLEVIEW_TABLEDATA);
actionProvider.setActionOrderList(ActionProviderSupportTypes.TABLEVIEW_SUPPORT,
columnHeaderAOL, table, HttpTableViewSupport.COLUMN_HEADER_AREA);
// Acquire the 'version' of the ascending sort action from which the APF clones all other
// versions that are returned to viewer components.
BaseAction sortAscendingAction =
actionProvider.getDefaultAction(ActionProviderSupportTypes.TABLEVIEW_SUPPORT,
HttpTableViewSupport.COLUMN_HEADER_AREA,
HttpTableViewSupport.SORT_COLUMN_ASCENDING_ACTION);
// Since either this Action or the sortDescendingAction will be taking the place of the
// COLUMN_HEADER_ACTION, we need to change the displayed text for this Action from
// "Sort Ascending" to the name of the column. This can be accomplished via the setting of
// a template string value that includes the AREA_VALUE_ATTRKEY. Such template values can be
// very useful in other scenarios where Action attribute values vary according to the specific
// area the Action is being used.
sortAscendingAction.putValue(sortAscendingAction.LABEL,
"%"+HttpTableViewSupport.AREA_VALUE_ATTRKEY);
// Position the column name text to the left of the image.
sortAscendingAction.putValue(sortAscendingAction.TEXT_ALIGNMENT,
sortAscendingAction.ALIGNMENT_LEFT);
// Set the Action's tooltip text.
sortAscendingAction.putValue(sortAscendingAction.SHORT_DESCRIPTION,
"Ascending Sort Of %"+HttpTableViewSupport.AREA_VALUE_ATTRKEY+" Column");
// Only return this Action to the viewer component when it is in an enabled state.
sortAscendingAction.setReturnStatus(sortAscendingAction.ENABLED);
// The 'sortDescendingImage' dynamic value is only available when the column is sorted
// ascendingly. If that value is not available, then we dont want an image on this Action.
sortAscendingAction.putValue(sortAscendingAction.SMALL_ICON_NAME,
"%sortDescendingImage(down_02b.gif)");
// Command should not be enabled ( and, consequently, the Action that uses it ) if the
// column is already sorted in this direction.
((SortCommand)sortAscendingAction.getCommand()).setDisabledIfCurrentState(true);
// .tableviewmenuLink not defined in default sasComponents.css so use the
// .tableviewmenuItemLink style class.
Map styleMap = new HashMap();
styleMap.put(MenuBar.MENU_LINK,new StyleInfo("menuItemLink"));
sortAscendingAction.putValue(sortAscendingAction.STYLE_MAP, styleMap);
// Do the same for the SORT_COLUMN_DESCENDING_ACTION
BaseAction sortDescendingAction =
actionProvider.getDefaultAction(ActionProviderSupportTypes.TABLEVIEW_SUPPORT,
HttpTableViewSupport.COLUMN_HEADER_AREA,
HttpTableViewSupport.SORT_COLUMN_DESCENDING_ACTION);
sortDescendingAction.putValue(sortDescendingAction.LABEL,
"%"+HttpTableViewSupport.AREA_VALUE_ATTRKEY);
sortDescendingAction.putValue(sortAscendingAction.TEXT_ALIGNMENT,
sortAscendingAction.ALIGNMENT_LEFT);
sortDescendingAction.putValue(sortAscendingAction.SHORT_DESCRIPTION,
"Descending Sort Of %"+HttpTableViewSupport.AREA_VALUE_ATTRKEY+" Column");
sortDescendingAction.setReturnStatus(sortDescendingAction.ENABLED);
sortDescendingAction.putValue(sortDescendingAction.SMALL_ICON_NAME,
"%sortAscendingImage(up_02b.gif)");
((SortCommand)sortDescendingAction.getCommand()).setDisabledIfCurrentState(true);
sortDescendingAction.putValue(sortAscendingAction.STYLE_MAP, styleMap);
}
}
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/Model2ExampleViewer.jsp");
rd.forward(request, response);
}
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Date Modified: | 2008-07-01 12:11:47 |
Date Created: | 2006-01-05 12:01:06 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS AppDev Studio | Microsoft Windows 2000 Professional | 9.1 TS1M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |||
Microsoft Windows XP Professional | 9.1 TS1M0 | |||
Microsoft Windows 2000 Server | 9.1 TS1M0 | |||
Microsoft Windows NT Workstation | 9.1 TS1M0 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 |