Struts2 Framework with SAP JCo3 – Business Object Repository

Struts2 is being used as the application framework with a series of plugins like the convention and json plugin. Servlets get data from the ABAP stack via SAP Java Connection and return to front-end either json or html content.

Convention plugin.
Compared to default struts2 standard code annotation configuration, convention plugin supports some advanced configuration options. For each struts2 action class you get to define the parent package, action url mapping(s), multiple result types at global class level or local action method level. All these using java annotations without using xml configurations files like struts.xml.

Here’s an example of such annotations for the GetBAPIList class, responsible for returning available methods for a given business object.

@ParentPackage("json-default")
@Action("get-bapi-list")
@Results({
    @Result(name="success", type="json"),
    @Result(name="error", location="error.jsp")
})
public class GetBAPIList extends ActionSupport {...}

The action parent package is defined as json-default, a package containing the json interceptor converting all bean properties to json format automatically. The url mapping for this class is defined as get-bapi-list. The most important feature in this example is the ability to define multiple results for the same action. A json string is returned on success and html content rendered via error.jsp is returned on error. This is not possible with standard struts2 annotations, can only be done via struts.xml configuration file which can dramatically grow in size as the application gets larger and more struts2 actions are defined and configured.

JSON plugin.
With the help of this plugin all action class bean properties are serialized into json format. Something really useful when communicating with the front-end via javascript ajax calls. To have the json interceptor activated for a given action, two elements are required:

  • the action needs to be in a package containing the json interceptor – most probably the json-default package.
  • the action needs to define the returned content type as json like in the above annotation example.

SAP Java Connector 3
There are two util classes used by all action classes to call abap remote functions:

  • SAPDestinationDataProvider implementing DestinationDataProvider holds all info regarding abap stack and login credentials.
  • SAPConnection initializes the JCo environment by registering the SAPDestinationDataProvider via com.sap.conn.jco.ext.Environment.registerDestinationDataProvider. Also responsible for returning JCoFunction instances for abap remote functions based on the abap function string name.

A typical call looks something like (code from GetBAPIInfo action class):

try {
   function = SAPConnection.getFunction("ZGET_BAPI_INFO");
   function.getImportParameterList().setValue("IM_BO_TYPE", objType);
   function.getImportParameterList().
   setValue("IM_METHOD_NAME", methodName);
   function.execute(SAPConnection.dConfig);

   structure = function.getExportParameterList().
   getStructure("EX_BAPI_INFO");
}
catch (JCoException e) {
   addActionError("JCo Error: " + e.getMessage() );
   return ERROR;
}

A JCoFunction reference to the corresponding abap remote function is acquired via custom SAPConnection util class. The function gets executed after setting the import parameters. Export parameters hold the function result and can be read via getStructure, getTable, etc..

In the above case, individual properties from EX_BAPI_INFO are accessed by structure.getString(“property_name”) such as structure.getString(“OBJTYPE”).

Leave a Reply

Your email address will not be published. Required fields are marked *