Array
(
    [0] => WP_Term Object
        (
            [term_id] => 306
            [name] => AngularJS
            [slug] => angularjs
            [term_group] => 0
            [term_taxonomy_id] => 306
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
            [cat_ID] => 306
            [category_count] => 2
            [category_description] => 
            [cat_name] => AngularJS
            [category_nicename] => angularjs
            [category_parent] => 0
        )

    [1] => WP_Term Object
        (
            [term_id] => 2
            [name] => Featured
            [slug] => featured
            [term_group] => 0
            [term_taxonomy_id] => 2
            [taxonomy] => category
            [description] => It is list of posts given by Editorial hand-picked. May be you enjoy all of this articles.
            [parent] => 0
            [count] => 81
            [filter] => raw
            [cat_ID] => 2
            [category_count] => 81
            [category_description] => It is list of posts given by Editorial hand-picked. May be you enjoy all of this articles.
            [cat_name] => Featured
            [category_nicename] => featured
            [category_parent] => 0
        )

    [2] => WP_Term Object
        (
            [term_id] => 20
            [name] => Fun Stuff
            [slug] => fun-stuff
            [term_group] => 0
            [term_taxonomy_id] => 20
            [taxonomy] => category
            [description] => All of this articles are totally enjoyable and you can take a rest from your daily work to read some funny stuffs.
            [parent] => 0
            [count] => 33
            [filter] => raw
            [cat_ID] => 20
            [category_count] => 33
            [category_description] => All of this articles are totally enjoyable and you can take a rest from your daily work to read some funny stuffs.
            [cat_name] => Fun Stuff
            [category_nicename] => fun-stuff
            [category_parent] => 0
        )

    [3] => WP_Term Object
        (
            [term_id] => 128
            [name] => Java
            [slug] => java
            [term_group] => 0
            [term_taxonomy_id] => 128
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 6
            [filter] => raw
            [cat_ID] => 128
            [category_count] => 6
            [category_description] => 
            [cat_name] => Java
            [category_nicename] => java
            [category_parent] => 0
        )

)
///////////

Struts2 AngularJS Read Operation Full Example

0
46
Struts2 AngularJS Insert Operation Example
Struts2 AngularJS Insert Operation Example

Formerly we discuss about how to insert data in database using Struts2 and AngularJS. Now we’ll see how to implement Struts2 AngularJS read operation full example which is equally important for developer point of view.

Lets discuss in details :

In my opinion, you must read insert data article for better understanding because i clearly explain the whole code.

Lets start with UI part:

<div class="container" ng-app="myApp" ng-controller="productsCtrl">
    <div class="row">
        <div class="col s12">
            <h4>Products</h4>
            
          
            
            <!-- used for searching the current list -->
<input type="text" ng-model="search" class="form-control" placeholder="Search product..." />
 
<!-- table that shows product record list -->
<table class="hoverable bordered">
 
    <thead>
        <tr>
            <th class="text-align-center">ID</th>
            <th class="width-30-pct">Name</th>
            <th class="width-30-pct">Description</th>
            <th class="text-align-center">Price</th>
            <th class="text-align-center">Action</th>
        </tr>
    </thead>
 
    <tbody ng-init="getAll()">
        <tr ng-repeat="d in names | filter:search">
            <td class="text-align-center">{{ d.id }}</td>
            <td>{{ d.name }}</td>
            <td>{{ d.description }}</td>
            <td class="text-align-center">{{ d.price }}</td>
            <td>
                <a ng-click="readOne(d.id)" class="waves-effect waves-light btn margin-bottom-1em"><i class="material-icons left">edit</i>Edit</a>
                <a ng-click="deleteProduct(d.id)" class="waves-effect waves-light btn margin-bottom-1em"><i class="material-icons left">delete</i>Delete</a>
            </td>
        </tr>
    </tbody>
</table>
            
            
             
        </div> <!-- end col s12 -->
    </div> <!-- end row -->
</div> <!-- end container -->

Explanation of above code:

Here we create a search box and show list of data fetch from database.

  1. Line 9: It is normal textfield with ng-model=”search” and used for searching the current list.
  2. Line 24: We used ng-init with getAll() function and the ngInit directive allows you to evaluate an expression in the current scope. That means function getAll() will be initiated when page read that table. Implementation of that function will be discuss below.
  3. Line 25: ng-repeat is like a loop for AngularJS and this directive instantiates a template once per item from a collection. Like foreach loop in Java getAll() function return the set of data in names. We just put the value of names in another variable named d. You can use many filters with AngularJS by using single pipeline(|). Here we use filter:search and search is the model name of our search textfield.  Now we can search by typing in text field.
  4. Line 27-29: You’ll see double curly braces({{}}) notation to bind expressions to elements is built-in AngularJS markup. You’ll read the JSON file which return by function getAll() and put all data in d. So we’ll read name by {{ d.name }} and so on.

Now lets move on JS part where our all implementation will be held:

    // read products
$scope.getAll = function(){
    $http.get("readproducts.action").success(function(response){
        $scope.names = response.records;
    });
}

 What was in above code?

Here you can see that firstly we define function getAll() and then call post function on $http directive with readproducts.action which generate all data in JSON format. All we’ll discuss the implementation of that Action class below. Then after when success response get we put all response in names by using $scope directive so we can access names anywhere in our application.

Now map readproducts.action with struts.xml:

<!-- Fetch all products -->  
     <action name="readproducts" class="co.tellmehow.json.ProductAction" method="readAllProducts">
           <result type="json" />
     </action>

 Now add method readAllProducts in ProductAction class:

private List<Product> records = new ArrayList<Product>();
  private static Product pd = new Product();
  
  
   public List<Product> getRecords() {
      return records;
    }

    public void setRecords(List<Product> records) {
      this.records = records;
    }
    
    
    
  
  
  
  public String readAllProducts()
  {
    List<Product> ps   = ProductDB.selectAll();
     
    for(Product p : ps)
    {
      
      pd.setName(p.getName());
      pd.setId(p.getId());
      pd.setDescription(p.getDescription());
      pd.setPrice(p.getPrice());
      
      records.add(p);
    }
    return Action.SUCCESS;
  }

 From above code you’ll get JSON like:

{"records":[{"description":"dd","id":1,"name":"GeekOnJava","price":123},{"description":"asdf","id":2,"name":"TellMeHow","price":123},]}

Finally, you complete the tutorial how to fetch data from database in AngularJS using Struts2.

Hope you like this article and if you’ve any queries, problem or suggestion please let me know in comment section.

Share your thoughts

Loading Facebook Comments ...
Loading Disqus Comments ...