Controllers, At Last!

image

The buzz-worthy release of Oracle's WebCenter Sites 12c gives us many reasons to perk up and take note. Among the unveiled features is a long-overdue Model-View-Controller framework; this shiny, new implementation finally provides developers with a clear and clean path for wiring together and rendering content. In the past, we had described a few work-arounds that were viable stand-ins for this piece of functionality that was missing out-of-the-box; namely, using the GST Site Foundation for WebCenter Sites libraries to supplement standard WCS development. But now, with the release of12c, a product standard for the model, view, and controller has been provided.

The introduction of the Controller interface and the BaseController class gives developers a better way to decouple business logic away from these JSP templates and only expose relevant pieces of the model to the view. The new role of Sites' JSP templates serves as just the view, as expected. The model is, lastly, exposed and accessed through a key-value map.

To begin making use of the controller framework, let's take a look at the methods to create a new custom Controller:

1. Using the Administrator UI

The Controller section is accessible from the side panel tree.

Selecting the Controller section provides an overview of all known and compiled controllers. As a cursory check for a controller validity, the status  of a working controller should be "compiled"; this indicates that WebCenter Sites can compile the groovy or java controller code without issue. Execution/runtime errors are still possible, but as a first step, there are no noted compilation errors.

Selecting the New option from the black navigation bar provides a resulting screen where creating a New Controller is possible. 

2. Using the csdt Eclipse plugin - be sure to use the correct .jar version for the 12c Sites Developer Tools. (Example jar to install in Eclipse: com.oracle.sites.developertools.plugins_12.2.1.183363.jar)

Open the Oracle WebCenter Sites Perspective. (Window > Perspective > Open Perspective > Other… )

When the Sites Perspective is active, there is a button shortcut to create a new Controller.

After filling out general details for the new Controller, a groovy controller shell is provided.

Note: The described samples use WebCenter Sites 12.2.1 and Eclipse version 4.5.2.
 

Basic Usage

In a simple use of a controller to load the contents of a given asset into the accessible map, the controller code would look something like the following:

public class MyFirstController extends BaseController {
    @RequiredParams(query="c,cid")
    public void doWork(Map models) {
      Map assetMap = newAssetReader()
        .forAsset(getAssetId())
        .selectAll(true)
        .read();
      models.put("asset", assetMap);
    }
}

The template JSP could then access the contents of the  asset :

<c:forEach var="a" items="${asset}">
  ${a.key} : ${a.value}
</c:forEach>

 

Extending the base REST API

The controller framework also allows for a developer to extend the REST API functionality. While there is a substantial set of REST API calls in the current release of WebCenter Sites 12c, the controller framework also allows for custom REST endpoints to be created.

The entirety of the new API call can be contained within a single controller element. So, to begin:

  • Create a new controller
  • Import the needed RESTful service resources 
     
import javax.ws.rs.FormParam;
import javax.ws.rs.*;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.*;
  • Create a new path for the request (GET|POST) and return the desired value(s) after performing some given operations or tasks.
@GET
@Path("/helloWorld")
public String HelloWorld(@DefaultValue("avisports") @QueryParam("site") String mySite,@DefaultValue("2000-01 01:10:05.445") @QueryParam("someDate") String myDate) {
    
    /* 
    
    // do some work here

    */
    
    return "Hello World on " + myDate;
    
}

In this sample, a dummy string is returned, however in a more useful scenario a complex JSON representation of asset data can be returned instead.

Once the controller is compiled the new REST endpoint to access this functionality will be:

http://localhost:7003/sites/REST/controller/MyNewController/helloWorld?site=avisports&someDate=2000-01-01

The application of a complex JSON response in this RESTful interaction leaves open a large number of possibilities to interact with WebCenter Sites; whether it is building integrations, regularly polling for changed data, or simply using this interface as a simple query tool.

The new controller framework is a powerful new addition to WebCenter Sites and has some very exciting implications and uses.

Subscribe to Our Newsletter

Stay In Touch