Monday, January 19, 2015

How to make AJAX call to Struts2 action class method using Wildcard in struts.xml file

STRUTS.XML

<package name="validationPkg" namespace="/validate" extends="json-default">

<interceptors>
<interceptor name="transaction" class="com.raj.TransactionInterceptor" />
</interceptors>

<action name="*/*/*" class="com.raj.controller.{1}.{2}CTRL" method="{3}">
<interceptor-ref name="basicStack"></interceptor-ref>
<interceptor-ref name="transaction"/>
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>

</package>

ACITON CLASS METHOD

private String packageName;

private InputStream inputStream;


public InputStream getInputStream() {
                return inputStream;
        }
public void setInputStream(String responseText) {
this.inputStream =  new ByteArrayInputStream(responseText.getBytes());

}


public String doValidation(){

PkgData pkgData = PkgDAO.getPkgByName(packageName);

if( packageName==null || packageName.trim().length()==0 ){ 
setInputStream("<div style='color:red;'> Empty Name");

}else if( packageName.matches(NVSMXUtility.NAME_VALIDATION_REGEX_PATTERN) ){ 
if( pkgData != null ){
setInputStream("<div style='color:red;'> Name already exist</div>");
}else{
setInputStream("<div style='color:green;'> Valid Name</div>");
}
        }else{
        setInputStream("<div style='color:red;'> Invalid Name (Only alphanumeric allowed)</div>");
        }
  return Results.SUCCESS;

}

JAVA SCRIPT


function verifyUniqueness(fieldName,actionName){    
 var pkgNameVal = $("#"+fieldName).val();
 $.ajax({
   type: "POST",
   url: '/nvsmx/'+actionName,
   data: {packageName:pkgNameVal},
   success: function(data){    
    $("#message").show();
    $("#message").html(data);
   }
}); 

}


JSP

<s:textfield name="pkgData.name" id="pkgName" onblur="verifyUniqueness('pkgName','/validate/pkg/Pkg/doValidation');"  />

No comments:

Post a Comment

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...