How To Load AppThena Objects in a View

Introduction

This article shows you how to load an object in a view JSP. It assumes that the AppThena Controller has already handled the action and forwarded control to the view.

This method is useful if you want to add one or two objects to a particular view. Note that it might be easier to load the object with an ActionEventHandler instead. This keeps the code in the controller layer where it belongs

If you need to load an object in a JSP which isn't a controller view then follow the example in How To Load AppThena Objects in a JSP.

A Note on TagLibs

The release version of AppThena will include a JSP tag library that will make this task simpler. The tag library will allow you to load objects without using scriptlets (embedded Java). For example,

<app:model>
  <app:object var="myPage" type="page" key="4">
    <h3>Title</h3>
    <p>${myPage.property["Title"]}</p>
    <h3>Key</h3>
  </app:object>
</app:model>

Solution

Summary

The solution is to use the Model that the Controller created. It's passed to the view in a request attribute called "model".

You need to know the type and key of the object you wish to load. The easiest way to find these is to open the object in the standard Edit screen and examine the page source. The type and key are shown in hidden form values. e.g.

<form id="form_EDIT_dbo.Directory" method="post" action="/autoedit/appthena.do">
  <fieldset class="wrapper">
    <input name="antiCSRFPostToken" type="hidden" value="" />
    <input name="o.currentAction" type="hidden" value="EDIT" />
    <input name="o.action" type="hidden" value="undefined" />
    <input name="o.modified" type="hidden" value="false" />
    <!-- Note that key will be empty if this is a new unsaved object -->
    <input name="o.key" type="hidden" value="4" />
    <input name="o.type" type="hidden" value="dbo.Directory" />

Example

This example is a JSP fragment that you can include in the view JSP. You'll need to change the object type, its key and the name of the properties that are displayed in order to make it work with your system.

You must import certain classes by putting a "page" tag at the head of the page. e.g.

<%@ page session="true" contentType="text/html;charset=ISO-8859-1" %>
<%@ page import="com.glasspaw.appthena.info.*,com.glasspaw.appthena.model.*" %>
...

You can now access the model and load the object.

<%
  // Get the model that the Controller passed to this view
  Model model = (Model)request.getAttribute("model");

  // The context passes messages and objects between the JSP and
  // any registered event handlers.
  ModelEventContext context = new ModelEventContextImpl(request);

  // Ask the model to load an object
  // You must know the type and key of the object
  ObjectInfo directoryObject = model.getObject("dbo.Directory", "1", context);

  // Stash the object so that we can get at it with JSTL etc
  pageContext.setAttribute("directory", directoryObject);
%>
<!-- We can now use the object -->
<h2>Directory is ${directory.displayName}</h2>