Skip to main content

Posts

Showing posts from March, 2015

How to escape Html in Angular js

Points To Remember You need to use SCE  to render html as it is on the page. There are two ways to use sce  either you use it in the angular controller to prevent escape html or you can create a filter to do this where ever you need it. Using it with a filter is a much better way to do so, if you need to do it at multiple places. Following Plunker example shows how you can prevent escaping of Html Loading plunk... Prevent Escape Html from angularjs Controllers <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> </head> <body ng-app="demo"> <div ng-controller="home"> Original Text : {{text}}<br/><br /><br /> EscapedText : <span ng-bind-html="text"></span> </div> <script> var app = angular.module("demo",[]); app.controller('home', function ($scope, $sce) { $scope.text = $sce.trustAs

Hibernate : fetch results using Orderby in Criteria Query

Syntax to use OrderBy in Hibernate Criteria Query The order can be specified using addOrder on a Criteria Object criteria.addOrder(Order.asc("propertyName")) criteria.addOrder(Order.desc("propertyName")) Order results according to an order in Hibernate  Suppose we have a class Category.java package com.ekiras.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="category") public class Category { public Category(){} public Category(Long id){ this.id = id; } public Category(Long id, String name){ this.id = id; this.name = name; } @Id @Column(nullable=false, name="id") @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(nullable=false, name="name") private String name; // Gett

Hibernate Criteria Query to find List of Domain class.

Syntax to get List of domain Object list can be used on the Criteria object as shown below. criteria.list() How to get a List of a Domain class using Hibernate's Criteria Query. If we have a domain class Category  as shown below. Catgeory.class package com.ekiras.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="category") public class Category { public Category(){} public Category(Long id){ this.id = id; } public Category(Long id, String name){ this.id = id; this.name = name; } @Id @Column(nullable=false, name="id") @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(nullable=false, name="name") private String name; // Getters and Setters } We can get the list of all the categories

How to Upload and Read a Csv in Grails

Points To Remember Add the plugin Csv Grails plugin . You can do it manually without the plugin if the you have a uniform structure of the csv file. Upload a Csv  You need to add the dependency for the csv file reader in BUildConfig.groovy as shown below BuildConfig.groovy compile ":csv:0.3.1" And you have a csv file like the following. upload.jsp <g:uploadForm action="upload"> <input type="file" name="file"> <g:submitButton name="file" value="Upload"/> </g:uploadForm> When the above form is submitted then the request will be handled by the controller action as shown below UploadController.groovy def upload() { MultipartFile file = request.getFile( 'file' ) file.inputStream.eachCsvLine { row -> String name = row[1] ?: "NA"; String email = row[2] ?: "NA"; // Business Logic here } } Now you can do your bus

Pagination in Spring Hibernate Mvc application

Points To Remember Create a Pagination Taglib using this example . Add Bootstrap css and js in the jsp you want to do pagination and include the taglib in the jsp. Pagination in Spring Hibernate Application using Bootstrap Create a Domain Person. Create a Service PersonService Create a Dao PersonDao . Create a Controller PersonController . Create a Taglib PaginationTaglib. Person.java package com.ekiras.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="person") public class Person { public Person(){}; public Person(String name,Integer age){ this.name = name; this.age = age; } @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private Long id; @Column(name="name") private String name; @Column(name="age") private Integer age;

How to get results from a fixed position in Hibernate Criteria Query

Points To Remember Offset may be required to do pagination or get the results after a particular result from the database. Using offset you can define the first result to be included in the result set from the database.  You can use setFirstResult  on the CriteriaQuery  to set the first result that you want. Using offset to get the results SampleDAO.java @SuppressWarnings("unchecked") public List<Person> getCategories(Integer offset){ return getSession() .createCriteria(Person.class) .setFirstResult(offset!=null?offset:0) .setMaxResults(10) .list(); } This is how you can set the first result you want to fetch from the database. In this example we have set the first result and the maximum number of records that we need to fetch.

How to create Pagination Taglib in Java, Spring

Points To Remember Bootstrap is one of the major UI componnet used worldwide, So you may want to implemnt pagination using bootstrap in java , j2ee or spring application. So all you need to do is Create a Class that will behave like a taglib. Create a Tld file that will map the tablib class with the jsp's. Use the taglib on jsp using the tld file. Create Pagination Taglib for Jsp with Bootstrap. Following is an example of the a custom taglib built for Bootstrap . This taglib takes in uri -> action to be hit when clicked. offset -> the offset of pagination. count -> total number of elements to be shown. max -> maximum number of pages to be shown in the pagination bar. steps -> maximum number of elements to be shown per page. previous -> text to be shown for previous page link. next -> text to be shown for next page link. PaginationTaglib.java This is a java class that will behave like a taglib on jsp's. package com.ekiras.taglib; import java.io.Writer