There are following step for using Ajax with Struts2.
1. create a web based application in your IDE.
2. you need include the basic jar files to run struts2 application.
3. Include the struts2 FilterDispatcher Listener entry into web.xml file.
4. Now create a struts.xml file into your src folder and do entry for action request process.
5. Now create the Struts2 Action class
6. Create the JSP page .
Now start your server and use URL to check this example:
If you have any question so please free feel to ask.
1. create a web based application in your IDE.
2. you need include the basic jar files to run struts2 application.
3. Include the struts2 FilterDispatcher Listener entry into web.xml file.
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>struts2</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
4. Now create a struts.xml file into your src folder and do entry for action request process.
<!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"
/>
<
package
name
=
"struts2"
extends
=
"struts-default"
namespace
=
"/"
>
<
action
name
=
"ajaxExample"
class
=
"com.example.actions.AjaxAction"
>
<
result
>/ajax.jsp</
result
>
</
action
>
<
action
name
=
"callAJax"
class
=
"com.example.actions.AjaxAction"
method
=
"ajaxMethod"
>
<
result
type
=
"stream"
>
<
param
name
=
"contentType"
>text/html</
param
>
<
param
name
=
"inputName"
>inputStream</
param
>
</
result
>
</
action
>
</
package
>
</
struts
>
5. Now create the Struts2 Action class
package
com.example.actions;
import
java.io.InputStream;
import
java.io.StringBufferInputStream;
import
com.opensymphony.xwork2.ActionSupport;
public
class
AjaxAction
extends
ActionSupport {
private
String intValue;
private
InputStream inputStream;
public
InputStream getInputStream() {
return
inputStream;
}
public
String ajaxMethod() {
System.out.println(
"Hello i m ajax Methods"
);
System.out.println(
"intValue "
+intValue);
int
myVal =
0
;
try
{
myVal=Integer.parseInt(intValue);
inputStream=
new
StringBufferInputStream(
""
);
}
catch
(Exception e) {
inputStream=
new
StringBufferInputStream(
"Value Should be Numberic"
);
}
return
SUCCESS;
}
public
String execute() {
return
SUCCESS;
}
public
String getIntValue() {
return
intValue;
}
public
void
setIntValue(String intValue) {
this
.intValue = intValue;
}
}
6. Create the JSP page .
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>AJAX JSP PAGE - AOI BLOG</
title
>
<
script
type
=
"text/javascript"
>
var httpObject = null;
// Get the HTTP Object
function getHTTPObjectForBrowser(){
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Browser does not support AJAX...........");
return null;
}
}
// Change the value of the outputText field
function setAjaxOutput(){
if(httpObject.readyState == 4){
document.getElementById('intValueDiv').innerHTML = httpObject.responseText;
}
}
// Implement business logic
function doAjaxCall(){
httpObject = getHTTPObjectForBrowser();
if (httpObject != null) {
httpObject.open("GET", "callAJax.action?intValue="+document.getElementById('intValueId').value, true);
httpObject.send(null);
httpObject.onreadystatechange = setAjaxOutput;
}
}
//-->
</
script
>
</
head
>
<
body
>
<
center
>
<
h1
>
Use Ajax in Struts2
</
h1
>
</
center
>
<
div
id
=
"intValueDiv"
style
=
"color: red;font-weight: bold"
></
div
>
Please Enter the Integer value :
<
s:textfield
value
=
""
id
=
"intValueId"
name
=
"intValue"
theme
=
"simple"
onblur
=
"doAjaxCall();"
></
s:textfield
>
<
br
/>
<
s:submit
value
=
"submit"
align
=
"left"
></
s:submit
>
</
body
>
</
html
>
Now start your server and use URL to check this example:
http://localhost:8080/yourprojectname/ajaxExample.action
If you have any question so please free feel to ask.
No comments:
Post a Comment