Under WEB-INF folder, we need to create a new XML file named hivemodule.xml. Right-click WEB-INF in the Projects view, select New > File/Folder..., then select 'XML' in Categories and 'XML' for file types. Enter 'hivemodule' as the file name on the next page of the wizard; leave the selected 'Well-formed Document' for Document Type. Now change the contents of the new file to look like this: <?xml version="1.0" encoding="UTF-8"?>
<module id="com.devshed.tapestry.celebrities" version="1.0.0"> <contribution configuration-id="tapestry.state.ApplicationObjects"> <state-object name="dataSource" scope="session"> <create-instance class="com.devshed.tapestry.celebrities.DataSource"/> </state-object> </contribution> </module> Let's see what we have here. The <module> element is the root of hivemodule.xml; we shall have it in every file like this. Note that the version attribute should look exactly as shown, i.e. it must contain three numbers separated by periods, or the application won't start. The <contribution> element tells HiveMind that we are going to contribute something of our own to its already existing extensive list of services and objects. The tapestry.state.ApplicationObjects value for configuration ID explains what we are going to do with this object; it indicates an ASO. The <state-object> element gives the ASO its name under which we shall be requesting it in the pages. Also we have specified that the scope for this ASO is session. Session is basically a piece of memory associated specifically with the given user; nobody else can access this memory. So in our case there will be a separate instance of DataSource for each user, which makes sense to me, as different collectors will have different collections. Alternatively, we could use an application scope, and in that case all the users would share the same DataSource. Finally, the <create-instance> element specifies which class to instantiate to create the desired ASO. Now that we made available the DataSource as an Application State Object, we don't need to store its instance in the CelebritiesList page class. Remove the line of code where we've instantiated it before: private DataSource dataSource = new DataSource(); And replace it with the following code: @InjectState("dataSource") public abstract DataSource getDataSource(); These two lines of code can be used in any page and any component of the application to obtain a reference to the DataSource. All that remains now is to replace all the references to the removed dataSource property with invocations of the newly created method. Here is the completed code for the DependenciesList class: public abstract class CelebritiesList extends BasePage {
@InjectState("dataSource") public abstract DataSource getDataSource();
@InjectPage("Details") public abstract Details getDetailsPage();
public List getCelebrities() { return getDataSource().getCelebrities(); }
public IPage onShowDetails(int id) {
Celebrity celebrity = getDataSource().getCelebrityById(id);
Details nextPage = getDetailsPage(); nextPage.setCelebrity(celebrity);
return nextPage; } } Now it is the time to create a page for creating a new Celebrity.
blog comments powered by Disqus |
|
|
|
|
|
|
|