Home » , » Partial page refresh in Struts 2 using JQuery (AJAX)

Partial page refresh in Struts 2 using JQuery (AJAX)

Rendyon | 10:38 AM | 0 comments
It is very hot question among the newbie. So here i am going to give example of partial page refresh using JQuery (AJAX) in Struts 2. Here i will refresh (or update)  a div (id='leftDiv' with result.jsp ).


Let us consider you have a jsp page say test.jsp like this:

<%@taglib  prefix="s" uri="/struts-tags" %>
<div id="leftDiv">
<s:include value="/pages/profile.jsp"></s:include>
</div>
<div>
<a href="#" id="idAnchor">Partial Refresh </a>
</div>
<script language="JavaScript" type="text/javascript">
$(document).ready(function(){
$('#idAnchor').click(function(){

$.ajax({
url: 'getResultAction', // action to be perform
type: 'POST', //type of posting the data
data: { name: 'Jeetu', age: '24' }, // data to set to Action Class
       dataType: 'html',
success: function (html) {
(
'#leftDiv).html(html); //set result.jsp output to leftDiv
},
error: function(xhr, ajaxOptions, thrownError){
alert('An error occurred! ' + thrownError);
},

});
});
})
</script>

Struts.xml
-----------------------
<package name="extype" extends="struts-default">
<action name="getResultAction" method="getInfo" class="com.jkit.TestAction">
<result name="success">/pages/result.jsp</result>
</action>
</package>

TestAction.java
-----------------------

public class TestAction {
private String name;
private String age;

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

result.jsp
--------------------

<%@taglib  prefix="s" uri="/struts-tags" %>
<h1>Call getName() from TestAction.java</h1>
Name: <s:property value="name"/>
<h1>Call getAge() from TestAction.java</h1>
Age: <s:property value="age"/>

After the Ajax call result.jsp output will be setting to div with id="leftDiv". So partial page update or refresh can be done easily with the help of Jquery(AJAX).
Share this article :

0 comments:

 
Copyright © 2011. Find Updates - All Rights Reserved
Template Modify by Creating Website
Proudly powered by Blogger