Java Reference
In-Depth Information
System.out.println("writeTo.");
StringBuilder sb = new StringBuilder("<html><body>");
sb.append("<h1>User</h1>");
sb.append("User ID: ").append(user.getId()).append("<br/>");
sb.append("Name: ").append(user.getName()).append("<br/>");
sb.append(String.format("Created On: %1$tm.%1$te.%1$tY",
user.getCreatedDate()) );
sb.append("<h2>View Another User</h2>");
int count = 0;
for (User u : Database.users.values()) {
sb.append("<a href='user/").append(u.getId());
sb.append("'>");
sb.append(u.getName()).append("</a> has ID ");
sb.append(u.getId());
sb.append("<br />");
count++;
}
sb.append("There are ").append(count).append(" users.");
//will create another GET request
sb.append("<br/><br/><a href=''>Create Another
User</a>");
sb.append("</body></html>");
out.write(sb.toString().getBytes());
}
}
The UserHtmlWriter class is pretty standard stuff. Its main job is to produce HTML repres-
enting a user on creation, along with a list of existing users to give the client some navigation
choices. Clients can view details for specific users by clicking their names, or navigate to cre-
ate another user. Clicking on the “Create Another User” link has an href value of '' because
it simply sends a new GET request to the same page, so you'll run back through the service
and end up at the start page of the application.
The important thing about the writer implementation is that you have to use the type parameter
correctly. On encountering an invocation of the entity method in a root resource class, the
runtime will look through the list of all writer implementations it loaded at startup and invoke
their isWriteable methods in an attempt to find a match. If the entity created is assignable to
the type parameter of a given writer, the runtime will invoke that implementation's writeTo
Search WWH ::




Custom Search