Monday, February 2, 2015

HOW TO SEND AND RECEIVE ARRAYLIST TO ACTION IN STRUTS2

jars
gson-2.2.4.jar
struts2-json-plugin-2.3.16.3.jar
and other required jar files for struts2


STRUTS.XML

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />

<package name="searchPkg" namespace="/searchNs" extends="json-default">

<action name="*/*/*" class="com.elitecore.nvsmx.policydesigner.controller.{1}.{2}CTRL" method="{3}">
<interceptor-ref name="basicStack"></interceptor-ref>
<result type="json">
<param name="root">pageDataList</param>
</result>
</action>

</package>  


</struts>

ACTION/CONTROLLER

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

/**
 * @author Kirpalsinh.raj
 *
 */
public class PkgCTRL extends ActionSupport {
private List<PkgData> pageDataList = new ArrayList<PkgData>(10);

public String getPageData(){
 
try {        
            Gson gson = new Gson();
            List<PkgData> pkgDatas = gson.fromJson(listData, new TypeToken<List<PkgData>>() {}.getType());             
           
            int iBeginIndex = 3;                        
            int iEndIndex = 10;
           
            if(iEndIndex > pkgDatas.size()){
               iEndIndex = pkgDatas.size();
            }
           
            pageDataList.clear();
            pageDataList =  pkgDatas.subList(iBeginIndex, iEndIndex);    
       }
   }      
}catch (Exception e) {
   LogManager.getLogger().trace(MODULE,e);
}

return Results.SUCCESS;
}

}


JSP
<%@page import="com.google.gson.reflect.TypeToken"%>
<%@page import="java.util.*"  %>
<%@page import="com.raj.nvsmx.pd.model.pkg.PkgData,com.google.gson.Gson" %>
<%@page import="com.google.gson.Gson" %>
<%@page import="java.lang.reflect.Type" %>

<%
List<PkgData> pkgsList = (List<PkgData>) request.getAttribute("pkgsList");
Type pkgListType = new TypeToken<List<PkgData>>() {}.getType();
String json = new Gson().toJson(pkgsList,pkgListType);
%>
<script>

function getListData(){
 var url = "/searchNs/pkg/Pkg/getPageData";

 $.ajax({
   type: "POST",    
   url: contextPath+url,
   data: {listData:'<%=json%>'},    
   success: function(jsonDataArray){     
    for (var i = 0; i < jsonDataArray.length; i++) {    
    jsonDataArray[i].description
    jsonDataArray[i].packageMode
    jsonDataArray[i].status
    jsonDataArray[i].validityPeriod            
        }
   },
   error: function(jsonData){
   }
});
}


</script>

POJO

public class PkgData implements Serializable {


private static final long serialVersionUID = 1L;

private String id;          
private String name;                
private String description;         
private String type;                
private String status;              
private String packageMode;                
private Integer validityPeriod;     
private String validityPeriodUnit;
private Double price;               
private Timestamp createdDate;        
private Long createByStaffId;

...setter and getter method

}

Friday, January 30, 2015

HOW TO GET DISPLAY TAG CURRENT PAGE NUMBER IN ACTION CLASS

String currentPage = request.getParameter((new ParamEncoder("tableId").encodeParameterName(TableTagParameters.PARAMETER_PAGE)));

session-timeout in web.xml vs setMaxInactiveInterval method


WEB.XML

<session-config>
    <session-timeout>10</session-timeout> 
</session-config>

Info : session timeout configured in web.xml will be applied to whole application's all sessions.
Here session timeout has value in minutes. means all the sessions in the whole application will be terminated if user not performing any action on the respective session for 10mins

JAVA

mySession.setMaxInactiveInterval(10);
Info:
setMaxInactiveInterval() method takes value in seconds.
so here mySession will be terminated after 10sec.



   

Thursday, January 29, 2015

BOOTSTRAP : HOW TO SHOW BOOTSTRAP CONFIRMATION DIALOGUE

JSP 
<html lang="en">

<head>
<script src="${pageContext.request.contextPath}/jquery/jquery.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap-tooltip.js"></script>

<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap-confirmation.js"></script>
</head>
<body>

<a class="btn" data-href="/pkg/Pkg/delete?                                             
                    pkgId=${pkgData.id}&pkgName=${pkgData.name}" 
                    data-toggle="confirmation" 
                    data-placement="left" 
                    data-popout="true" 
                    data-singleton="true" 
                    data-original-title="" title="">

<img src="${pageContext.request.contextPath}/images/delete.jpg"  />
</a>
</body>
</html>

JAVA SCRIPT
$(document).ready(function() {

$('[data-toggle="confirmation"]').confirmation({
title : 'Are you sure ?',
animation : true,
popout: true,
btnOkLabel : 'Yes',
btnCancelLabel : 'No',
onConfirm : function() {
},
onCancel : function() {
$('[data-toggle="confirmation"]').confirmation('hide');
}

});
});

Wednesday, January 28, 2015

HOW TO DISPLAY ROW NUMBER IN DISPLAY TAG

<display:table id="pkgData" name="pkgsList" requestURI="/pkg/Pkg/search">

<display:column title="Sr.#" >${pkgData_rowNum}</display:column>

</display:table>

Tuesday, January 27, 2015

STRUTS2 UNKNOWN ACTION HANDLER CLASS (UnknownHandler)

struts.xml

<struts>


<bean type="com.opensymphony.xwork2.UnknownHandler" name="handler" class="com.raj.mv.pd.controller.pkg.UnknownActionController"/>
...
...

</struts>

UNKNOWN ACTION HANDLER

import org.apache.struts2.dispatcher.Dispatcher;

import com.raj.commons.logging.LogManager;
import com.raj.mv.pd.constants.Results;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.UnknownHandler;
import com.opensymphony.xwork2.XWorkException;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.RuntimeConfiguration;
import com.opensymphony.xwork2.config.entities.ActionConfig;

public class UnknownActionController implements UnknownHandler{
public static final String MODULE = UnknownActionController.class.getSimpleName();

@Override
public ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException {

LogManager.getLogger().debug(MODULE,"Method called handleUnknownAction()");

ConfigurationManager configurationManager = Dispatcher.getInstance().getConfigurationManager();
   RuntimeConfiguration runtimeConfiguration = configurationManager.getConfiguration().getRuntimeConfiguration();
   ActionConfig actionConfig = runtimeConfiguration.getActionConfig(namespace, actionName);
   
   if(actionConfig == null) { // invalid url request, and this need to be handled
       actionConfig = runtimeConfiguration.getActionConfig("", "pkg/Pkg/error");
   }
   return actionConfig;
}

@Override
public Object handleUnknownActionMethod(Object arg0, String arg1) throws NoSuchMethodException {
LogManager.getLogger().debug(MODULE,"Method called handleUnknownActionMethod()");
return Results.EXCEPTION;
}

@Override
public Result handleUnknownResult(ActionContext arg0, String arg1, ActionConfig arg2, String arg3) throws XWorkException {
LogManager.getLogger().debug(MODULE,"Method called handleUnknownResult()");
return null;
}

}

Monday, January 26, 2015

STRUTS2 Display exception detail on the JSP

To Display exception message

<s:label label="Exception :" value='%{exception}' />

To Display exception stack stack

<s:label label="Exception :" value='%{exceptionStack}' />

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...