Ajax example with Struts 2- struts-dojo plugin

Leave a Comment
Struts uses the DOJO framework for the AJAX tag implementation. First of all, to proceed with this example, you need to add struts2-dojo-plugin-2.2.3.jar to your classpath.

In new dynamic web project create index.jsp file and copy following code.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="sx" uri="/struts-dojo-tags" %>
    <%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<sx:head/>
</head>
<body>
<s:form>
<sx:autocompleter label="Favourite Color" list="{'red','green','blue','orange','pink','yellow'}"></sx:autocompleter></br>
<sx:datetimepicker label="Delivery Date" name="deliveryDate" displayFormat="dd/MM/yy"></sx:datetimepicker></br>
<s:url id="url" value="/hello.action"></s:url>
<sx:div href="%{#url}" delay="2000">Initial Content</sx:div>
</br>
<sx:tabbedpanel id="tabContainer">
<sx:div label="Tab1">Tab1 contents</sx:div>
<sx:div label="Tab2">Tab2 contents</sx:div>
</sx:tabbedpanel>
</s:form>
</body>
</html>

2. edit web.xml file to support struts as follow.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
  
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <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>
</web-app>

3. create struts.xml file and copy following code.
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="hello" class="com.ActionClass" method="helloAction">
            <result name="success">success.jsp</result>
        </action>
    </package>
</struts>
 4. create page success.jsp to load on ajax call.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>Hello World From Success.jsp
</body>
</html>
5. now create action class and copy following code.
package com;

import com.opensymphony.xwork2.ActionSupport;

public class ActionClass extends ActionSupport {


    public String execute() {
        return SUCCESS;
    }
   
    public String helloAction() {
        return SUCCESS;
    }
}

0 comments:

Post a Comment