Java data model interface
DataModel is an abstraction around arbitrary data binding technologies that can be used to adapt a variety of data sources for use by JavaServer Faces components that support per-row processing for their child components (such as UIData .
The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a zero-relative cursor (row index). The APIs provide mechanisms to position to a specified zero-relative row index, and to retrieve an object that represents the data that corresponds to the current row index.
A concrete DataModel instance is attached to a particular collection of underlying data by calling the setWrappedData() method. It can be detached from that underlying data collection by passing a null parameter to this method.
Concrete DataModel implementations must provide a public zero-arguments constructor that calls setWrappedData(null) . A convenience constructor that takes a wrapped object of the appropriate type (and passes it on via a call to setWrappedData() , should also be provided.
Event listeners may be registered to receive notifications of when a new row index is selected.
Constructor Summary | |
---|---|
DataModel () |
Method Summary | |
---|---|
void | addDataModelListener (DataModelListener listener) Add a new DataModelListener to the set interested in notifications from this DataModel . |
DataModelListener[] | getDataModelListeners () Return the set of DataModelListener s interested in notifications from this DataModel . |
abstract int | getRowCount () Return the number of rows of data objects represented by this DataModel . |
abstract E | getRowData () Return an object representing the data for the currenty selected row index. |
abstract int | getRowIndex () Return the zero-relative index of the currently selected row. |
abstract java.lang.Object | getWrappedData () Return the object representing the data wrapped by this DataModel , if any. |
abstract boolean | isRowAvailable () Return a flag indicating whether there is rowData available at the current rowIndex . |
java.util.Iterator | iterator () Return a read-only Iterator over the row data for this model. |
void | removeDataModelListener (DataModelListener listener) Remove an existing DataModelListener from the set interested in notifications from this DataModel . |
abstract void | setRowIndex (int rowIndex) Set the zero-relative index of the currently selected row, or -1 to indicate that we are not positioned on a row. |
abstract void | setWrappedData (java.lang.Object data) Set the object representing the data collection wrapped by this DataModel . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DataModel
isRowAvailable
public abstract boolean isRowAvailable()
Return a flag indicating whether there is rowData available at the current rowIndex . If no wrappedData is available, return false .
Throws: FacesException — if an error occurs getting the row availability
getRowCount
public abstract int getRowCount()
Return the number of rows of data objects represented by this DataModel . If the number of rows is unknown, or no wrappedData is available, return -1.
Throws: FacesException — if an error occurs getting the row count
getRowData
Return an object representing the data for the currenty selected row index. If no wrappedData is available, return null .
Throws: FacesException — if an error occurs getting the row data IllegalArgumentException — if now row data is available at the currently specified row index
getRowIndex
public abstract int getRowIndex()
Return the zero-relative index of the currently selected row. If we are not currently positioned on a row, or no wrappedData is available, return -1.
Throws: FacesException — if an error occurs getting the row index
setRowIndex
public abstract void setRowIndex(int rowIndex)
Set the zero-relative index of the currently selected row, or -1 to indicate that we are not positioned on a row. It is possible to set the row index at a value for which the underlying data collection does not contain any row data. Therefore, callers may use the isRowAvailable() method to detect whether row data will be available for use by the getRowData() method.
If there is no wrappedData available when this method is called, the specified rowIndex is stored (and may be retrieved by a subsequent call to getRowData() ), but no event is sent. Otherwise, if the currently selected row index is changed by this call, a DataModelEvent will be sent to the rowSelected() method of all registered DataModelListener s.
Parameters: rowIndex — The new zero-relative index (must be non-negative) Throws: FacesException — if an error occurs setting the row index IllegalArgumentException — if rowIndex is less than -1
getWrappedData
public abstract java.lang.Object getWrappedData()
Return the object representing the data wrapped by this DataModel , if any.
setWrappedData
public abstract void setWrappedData(java.lang.Object data)
Set the object representing the data collection wrapped by this DataModel . If the specified data is null , detach this DataModel from any previously wrapped data collection instead.
If data is non- null , the currently selected row index must be set to zero, and a DataModelEvent must be sent to the rowSelected() method of all registered DataModelListener s indicating that this row is now selected.
Parameters: data — Data collection to be wrapped, or null to detach from any previous data collection Throws: java.lang.ClassCastException — if data is not of the appropriate type for this DataModel implementation
addDataModelListener
Add a new DataModelListener to the set interested in notifications from this DataModel .
Parameters: listener — The new DataModelListener to be registered Throws: java.lang.NullPointerException — if listener is null
getDataModelListeners
Return the set of DataModelListener s interested in notifications from this DataModel . If there are no such listeners, an empty array is returned.
removeDataModelListener
Remove an existing DataModelListener from the set interested in notifications from this DataModel .
Parameters: listener — The old DataModelListener to be deregistered Throws: java.lang.NullPointerException — if listener is null
iterator
Return a read-only Iterator over the row data for this model.
Specified by: iterator in interface java.lang.Iterable Since: 2.0
| ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2009-2011, Oracle Corporation and/or its affiliates. All Rights Reserved. Use is subject to license terms.
Generated on 10-February-2011 12:41
Spring MVC – Model Interface
The Spring Web model-view-controller (MVC) is an open-source framework used to build J2EE web applications. It is based on the Model-View-Controller design pattern and implements the basic features of a core spring framework – Dependency Injection. It is designed around a ‘DispatcherServlet’ that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, and theme resolution as well as support for uploading files. In the MVC framework, M stands for Model – used to work with the data, V stands for View – used to work with presenting data to user and C stands for Controller – that contains actual business logic to process the user input.
Spring Framework provides an Interface called Model(I) to work with the data. It defines a placeholder for model attributes and is primarily designed for adding attributes to the model. It is also used to transfer data between the view and controller of the Spring MVC application. Model interface is available in the org.springframework.ui package. It acts as a data container that contains the data of the application. That stored data can be of any form such as String, Object, data from the Database, etc.
Methods Available in Model(I)
Below are the methods available in Model(I).
1) addAttribute
It adds the specified attribute under the supplied name.
Model addAttribute(String attributeName, @Nullable Object attributeValue) attributeName - the name of the model attribute - can never be null attributeValue - the model attribute value - can be null
2) addAttribute
It adds the specified attribute to this Map using a generated name.
Model addAttribute(Object attributeValue) attributeValue - the model attribute value - can never be null
3) addAllAttributes
To copy all the attributes in the specified Collection into this Map, using attribute-name generation for each element.
Model addAllAttributes(Collection attributeValues) attributeValues - the model attribute values
4) addAllAttributes
To copy all the attributes in the supplied Map into this Map.
Model addAllAttributes(Map attributes)
5) mergeAttributes
To copy all the attributes in the supplied Map into this Map, with existing objects of the same name taking precedence, that is, they are not getting replaced.
Model mergeAttributes(Map attributes)
6) containsAttribute
To check whether this model contains an attribute of the given name?
boolean containsAttribute(String attributeName) attributeName - the name of the model attribute - not null It returns the value true/false.
7) getAttribute
To return the attribute value for the specified name, if present.
@Nullable Object getAttribute(String attributeName) attributeName - the name of the model attribute - not null It returns the corresponding attribute value. If not, it returns null.
It returns the current set of model attributes as a Map.
Spring MVC Application
We will create a simple Spring MVC application in Spring Tool Suite (STS) on how to use Model objects in holding the form data.
Steps to create the application:
- Create a Spring MVC project in Spring Tool Suite.
- In STS while creating the project based on the developer selection, it will download all the required maven dependencies, *.jar, lib files and it will provide an embedded server.
- We can see all the dependencies that are required are available in the ‘pom.xml’ file.
- Create a Bean class, Controller class, and the JSP view pages.
- Below is the final project structure of the Spring MVC project after creating *.java and *.jsp files.
Implementation:
Files to be created are as follows:
- Details.java – Bean class – To define the field properties and getter/setter methods of the properties.
- DetailsController.java – Controller class – To process the user request and generate the output.
- details.jsp – JSP file to interact with the user for the input.
- detailsSummary.jsp – JSP file to display the output to the user after processing the input.
1) Details.java file: