How To Load AppThena Objects in a JSP

Introduction

This article shows you how to use the Model directly from a JSP without having to use AppThena's Controller or the standard GUI.

This is useful if you want to use AppThena as the model in a different model/view/controller implementation such as Apache Struts™. It's also handy when you want to add small amounts of data to existing web pages.

If you need to load an object in a JSP which is the Controller is using as a view then follow the example in How To Load AppThena Objects in a View instead. Do not use the pattern in this document as you will run into problems with the Model's transaction.

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 instantiate the AppThena Model object using PluginManager.getModel() and then ask it to load the object you want using Model.getObject().

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 complete 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.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page session="true" contentType="text/html;charset=ISO-8859-1" %>
<%@ page import="com.glasspaw.appthena.*,com.glasspaw.appthena.info.*" %>
<%@ page import="com.glasspaw.appthena.utils.*,com.glasspaw.appthena.model.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>This Object Was Loaded Directly from the Model</title>
</head>
<%-- Warning: The widespread use of escapeXml() is intended to prevent
     page corruption and cross site scripting attacks from user supplied
     data. No attempt is made to exclude values that can't be modified by
     the user out of paranoia. --%>
<body>
  <%
    // Load the Model to get access to the database ...
    Model model = PluginManager.getModel();
    // ... and start a database transaction
    model.beginTransaction();

    // 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);
  %>
  <h1>This Object Was Loaded Directly from the Model</h1>
  <!-- We can now read the object's data members and navigate to it's properties.
       See ObjectInfo class for details of accessors. -->
  <table border="1">
    <tbody>
      <tr><th>Type Name</th><td>${directory.type.name}</td></tr>
      <tr><th>Type Display Name</th><td>${directory.type.displayName}</td></tr>
      <tr><th>Name Property</th><td>${directory.property["Title"]}</td></tr>
    </tbody>
  </table>
  <h1>Access Our Object Without JSTL</h1>
  <table border="1">
    <tbody>
      <tr>
        <th>With jsp:getProperty</th>
        <td><jsp:getProperty name="directory" property="key"/></td>
      </tr>
      <tr>
        <th>With scriptlet</th>
        <td><%= directoryObject.getProperty("Id").getValue() %></td>
      </tr>
    </tbody>
  </table>
  <%
    // Save any changes to the database. Use rollback() to abandon changes.
    model.commit();
  %>
</body>
</html>