In this lesson, you learn how to create the server-side Java™ classes to hold the data for
the application.
About this task
The application accesses the data using the JSON data
interchange format. The data can be accessed in one of two ways:
- Directly from a JSON file that holds the data
- From a server-side service that obtains the data from server-side Java objects, such as an EJB or
POJO, and produces a JSON file
Table 1. Data access. Options for accessing
data using the JSON data interchange format| Option |
Description |
| To use the JSON file as a data source |
Download the JSON file.
Save the JSON file to the WebContent folder and then skip to Lesson 5. |
| To use an RPC adapter service as a data source |
Complete Lesson
3 and Lesson 4. |
In this tutorial, all of the data is
hard-coded into the class to simplify the tutorial. If you implement
a web application in production, the data would come from databases,
EJBs, or other server-side components.
To create the server-side Java classes:
Procedure
- Create the web2 Java package:
- In the Enterprise Explorer view, expand MyMovieProject.
- Right-click Java Resources and
click to open the Java Package wizard.
- In the Name field, type web2,
and then click Finish. The web2 Java package is created:
The web2 package contains the server-side
objects to expose to the Dojo user interface that you create in Lesson 5: Add a Dojo layout widget to the page. For Dojo to interact with the Java objects that you created, ensure
that you can run their methods using a URL.
- Create the Movie Java class:
- Right-click web2 and select .
- In the Name field, type Movie,
and then click Finish. The
Movie.java file opens in the editor.
- Edit Movie.java so that it looks
like the following code snippet:
package web2;
public class Movie {
private String title;
private String director;
private String actor;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
The Movie class is a simple bean with string fields for a title, director,
actor, and description.
- Create the MovieService Java class:
- Right-click web2 and select to
open the Java Class wizard.
- In the Name field, type MovieService,
and then click Finish. The MovieService.java file
opens in the editor.
- Edit MovieService.java, so that
it looks like the following code snippet:
package web2;
import java.util.ArrayList;
import java.util.List;
public class MovieService {
public List<Movie> getMovieList() {
List<Movie> movieList = new ArrayList<Movie>();
Movie goneWithTheWind = new Movie();
goneWithTheWind.setTitle("Gone with the Wind");
goneWithTheWind.setDirector("Victor Fleming");
goneWithTheWind.setActor("Vivien Leigh");
goneWithTheWind.setDescription("Going with the wind");
movieList.add(goneWithTheWind);
Movie backToTheFuture = new Movie();
backToTheFuture.setTitle("Back To The Future");
backToTheFuture.setDirector("Robert Zemeckis");
backToTheFuture.setActor("Michael J Fox");
backToTheFuture.setDescription("Going back to the future");
movieList.add(backToTheFuture);
Movie starWars = new Movie();
starWars.setTitle("Star Wars");
starWars.setDirector("George Lucas");
starWars.setActor("Harrison Ford");
starWars.setDescription("Wars in the stars");;
movieList.add(starWars);
return movieList;
}
}
The MovieService class contains one method, getMovieList(),
which returns a list of Movie objects.
Results
In the Enterprise Explorer view of the Web perspective, the Java package of your
MyMovieProject web
project looks similar to the following screen capture: