Scenario: When the user registers, enter the user name in the text input box, and after the user has entered, determine if the user name exists in the database. In this scenario, the server simply returns TRUE or FALSE.
Write in Java action:
* * for Ajax
/private InputStream InputStream;
Public InputStream getInputStream () {return
inputstream;
}
private String name;
public void SetName (String name) {
this.name = name;
}
Public String Validatename () throws Unsupportedencodingexception {
Boolean f = employeeservice.isnamevalidate ( name);
System.out.println (f);
InputStream = new Bytearrayinputstream (f?) 1 ":" 0 "). GetBytes (" UTF-8 "));
return "ajax-success";
}
Configuration in Struts2.xml file:
<result type= "Stream" Name= "ajax-success" >
<param name= "ContentType" >text/html</param>
<param name= "InputName" >inputStream</param>
</result>
Front:
//send ajax request var url = "Emp-validatename.action"; var args = {"Name": Val, "Time": New Date ()}; $.po
St (URL, args, function (data) {if (data== "1") {$this. After ("<font color= ' green ' > name available </font>");
}else if (data= "0") {$this. After ("<font color= ' Red ' > name not Available </font>");
}else{alert ("Server Exception"); }
});