In this example, we will introduce you to how to change value of div tag using Ajax in struts, without reloading page.
1-index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>ADS_TO_REPLACE_1 <%@taglib uri="/struts-tags" prefix="s" %> <%@taglib uri="/struts-dojo-tags" prefix="sx" %> <html>ADS_TO_REPLACE_2 <head><title>Struts Ajax example</title><sx:head /></head> <body><h1>Struts Ajax example......</h1><hr/> <sx:div id="displayImage">Hello friends.................</sx:div>ADS_TO_REPLACE_3 <s:url id="imagePage" value="imageAction.action"></s:url> <sx:a href="%{imagePage}" targets="displayImage"> Change Content of Ajax div tag. </sx:a></body>ADS_TO_REPLACE_4 </html> |
2_ AjaxAction.java
package roseindia; import com.opensymphony.xwork2.ActionSupport; public class AjaxAction extends ActionSupport{ADS_TO_REPLACE_5 public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } } |
3_ struts.xml
<?xml version="1.0" encoding="UTF-8"?>ADS_TO_REPLACE_6 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">ADS_TO_REPLACE_7 <struts> <constant name="struts.devMode" value="true" /> <package name="roseindia" extends="struts-default">ADS_TO_REPLACE_8 <action name="indexpage"> <result >index.jsp</result> </action>ADS_TO_REPLACE_9 <action name="imageAction" class="roseindia.AjaxAction"> <result name="success">hello.jsp</result> </action>ADS_TO_REPLACE_10 </package> </struts> |
4_ hello.java
<img alt="Image not display" src="images/roseindialogo.jpeg"> New content of ajax div tag |
OutputADS_TO_REPLACE_11
Download Source CodeADS_TO_REPLACE_12
Advertisements
Ads
Ads