I have a Struts2 application in which the home page is horizontally split into Header, Menu, Content and Footer. On click of the menu, a "navigate" action is invoked via JQuery and Ajax. The menu name is passed as a parameter to the action through the s:url tag. Based on the menu name, the JSP page name is dynamically prepared. However the Ajax response does not get loaded in the Content part of the page but gets redirected to a new page. I am not sure why this is happening. Below are the code snippets.
Menu.jsp
<!-- Dynamic Menu Bar -->
<div id="menu" class="divMenu">
<ul>
<s:iterator var="parent" value="menuList">
<li>
<s:a href="#">
<s:property value="#parent.menuDesc" />
</s:a>
<ul class="sub-menu">
<s:iterator var="child" value="#parent.childrenList">
<li>
<s:url var="urlValue" action="navigate">
<s:param name="pageName" value="#child.menuDesc" />
</s:url>
<s:a method="post" href="%{urlValue}" onclick="menuNavigate(this)">
<s:property value="#child.menuDesc" />
</s:a>
</li>
</s:iterator>
</ul>
</li>
</s:iterator>
</ul>
</div>
JavaScript Function
function menuNavigate(action) {
alert(action);
$.ajax({
type : "POST",
url : action + ".action",
datatype: "html",
success : function(response) {
alert(response);
$("#content").html(response);
},
error : function(err) {
$("#content").html(err);
}
});
}
BTW, I am able to see the response using the alert under the success part of the Ajax call. On clicking OK, the response gets displayed in a new window.
Struts2 XML
<action name="navigate" class="com.web.action.home.HomePageAction" method="navigate">
<result name="success">/WEB-INF/pages/${pageName}</result>
</action>
Adding an event.preventDefault()
worked for me. Thanks for all of them who provided their valuable suggestions.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments