Post back

Let's now learn how to post back to server

Post back
Is the act of POSTING data BACK to the server for processing.
Putting it differently: It's an HTTP POST Request back to the server with data to be processed.

Introducing Frontiers

You are lost right? What do frontiers have to deal with posting back? In Hi-Framework, frontiers are what you use to post back.

Frontiers
CDI Managed beans annotated with @Frontier, with public methods that can be called from client-side, statically.

Frontier class example

Let us show you some frontier example:


    @Frontier //Required
    @ApplicationScoped //This is the most convenient CDI scope
    public class Cars{  
    
        public long save(Car car){
            
            //Save and return car Id
        
        }
        
        public Car getDetails(long carId){
            
            //Get car record from storage
            
        }
        
    }
    

Any of the public methods of the frontier class we just created can be called from client-side, statically. You can call the methods from the view controller or from whatever script you want.They are static methods.

Simple frontier call example

So, how do we call a frontier method?

    
   var car = { brand:"Toyota", model:"Vitz" };
    
   //Call the frontier method
   Cars.save(car).try(function(carId){
      
       console.log("Car id is "+carId);
      
   });


Handling frontier call errors

We have already seen how to call the frontier method, now we will show you how to catch errors:


   var car = { brand:"Toyota", model:"Vitz" };

   //Call the frontier method
   Cars.save(car).try(function(carId){
           
       console.log("Car id is "+carId);
           
   }).catch(function(err){//Handle errors
            
       console.error("Failed to persist car");   
      
   });
    

Piece of cake right? There is nothing hard about frontiers.

Why use frontiers instead of direct ajax requests?

We have been there and done that and we concluded direct ajax requests aren't a big deal.
Frontiers give you out-of-the-box CSRF protection and also a convenient way to mock back-end when testing the client-side, plus the syntax which we believe to be clean, as you pass the same parameters you specified in your Java classes.

The JSON objects you pass as arguments in your JavaScript code, are automatically converted into Java compatible objects.
Frontiers will also very soon offer you parameters hints (via your IDE) when calling a method, considering the parameters name API introduced in Java 8.

Github Repository