Featured

Tuesday 18 March 2014

How to call Servlet using ajax calling in cq5

Here is the sample ajax call to call a SlingServlet which is registered in cq.

---> Here we will get the values of below two textfields and pass to the servlet by using their 'id'.
---> After this you need to get the values in that appropriate servlet and apply your business logic.


<script>
$(document).ready(function() { 
$( ".perform" ).click(function() {
       var failure = function(err) {
             alert("Unable to retrive data "+err);
   };
 

  var val1=$('v1').val();
  var val2=$('v2').val();

    //Use JQuery AJAX request to post data to a Sling Servlet
    $.ajax({
         type: 'POST',  
         url:'/bin/imgcounter',

         data:{'value1' : val1,'value2' : val2},          //passing values to servlet
         success: function(msg){
            //Success logic here(The response from servlet)

         }
     });
    location.reload();
  });
    
});
</script>

 <form action="<%=currentPage.getPath()%>.html">
            Value1:<input type="text" id="v1"/>

            Value2:<input type="text" id="v2"/>
           <input type="submit" class="perform" value="Post to servlet!!!"/>
</form>



Thanks,

Hope it helps :) 
Thanks,
SonyCharan

Thursday 6 March 2014

client side dialog validation in cq5

Here we will create a component which will have some basic fields. Lets say we will use two textfields and do validation on both of them.

Here the process follows,

1)Create dialog for the component as following,

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:Dialog"
    xtype="dialog">
    <items jcr:primaryType="cq:TabPanel">
        <items jcr:primaryType="cq:WidgetCollection">
            <validationtab
                jcr:primaryType="cq:Panel"
                title="Validation Example Tab">
                <items jcr:primaryType="cq:WidgetCollection">
                    <exampletext0
                            jcr:primaryType="cq:Widget"
                            fieldLabel="First TextField"
                            fieldDescription="First field Description"
                            name="./text0"
                            xtype="textfield"/>
                    <exampletext1
                            jcr:primaryType="cq:Widget"
                            fieldLabel="Second TextField"
                            fieldDescription="Second field Description"
                            name="./text1"
                            xtype="textfield"/>
</items> </validationtab> </items> </items> <listeners jcr:primaryType="nt:unstructured" beforesubmit="function(dialog) {return Example.checkFields(dialog);};"/> </jcr:root>
 
 
2) After creating the dialog we will create a js file to get the values from 
the dialog and make it vaid.

This js file we make include in our clientLibrary folder ,
by including such, we make it call on load.
Here we are getting the two textfield values which are in the dailog.
 
Example = function() {
        return {
        
        // Dialog field cross validation.
        checkFields : function(dialog,maxWidth,maxHeight) {
            
            var textfieldArray = dialog.findByType("textfield");
            var textfieldLength0 = textfieldArray[0].getValue().length;
            var textfieldLength1 = textfieldArray[1].getValue().length;
            
            if((textfieldLength0 > 0 && textfieldLength1 > 0) || 
               (textfieldLength0 == 0 && textfieldLength1 == 0)) {
                // Both fields have or do not have a value
                return true;
            } else {
                // Cross validation fails
                CQ.Notification.notify(CQ.I18n.getMessage("Validation Error"),
                CQ.I18n.getMessage("Both fields must be filled or left empty."));
                return false;  
            }
        }
    };
    
}();
 

Hope it helps you.
Comments are Welcome!!!!


Wednesday 5 March 2014

How to get users members of a group in cq5

Here is the process for getting the members of a group.

As per my requirement i did this, U may change lot in this :)

<%--

  GroupUsersGettingBasedOnGroupId component.

  GroupUsersGettingBasedOnGroupId comp

--%><%
%><%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" %>
<%@page contentType="text/html"
        pageEncoding="utf-8"
        import="com.day.text.Text,
                org.slf4j.Logger,org.slf4j.LoggerFactory,
                org.apache.jackrabbit.api.security.user.*,
                org.apache.sling.api.resource.ResourceResolver,
                javax.jcr.Session" %>
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%
%><cq:defineObjects/>
<%!
    private final Logger log = LoggerFactory.getLogger(getClass());
%>

<%
 java.util.Iterator<Authorizable> users, groups ;
Object obj = null; User user = null;String id = null;int i=0;
String groupName="everyone";

    UserManager userManager = resourceResolver.adaptTo(UserManager.class);

try{
    final Group groupd = (Group) userManager.getAuthorizable(groupName);
     %><font color="orange"><%=groupd%><br/><%
  users = groupd.getMembers();

           while(users.hasNext()){
                            obj = users.next();
                                    if(!(obj instanceof User)){
                                         continue;
                                     }

                            user = (User)obj;
                            id = user.getID();
                           if(id.contains("@")){
                                       i++;
                             %><br/>User <%=i%>:<%=id%><%
                           }
           }
}catch(Exception e){
%><br/>Group "<%=groupName%>" Not Found<br/><%
}
%></font>