Struts 2.x : [A Framework from ASF] :: Struts 2.x with AJAX
Struts 2.x with AJAX |
Struts 2.x with AJAX Example.
Example for Struts 2.x with AJAX |
Project Structure
AjaxCountryAction.java
package com.javaskool.struts;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import com.opensymphony.xwork2.ActionSupport;
public class AjaxCountryAction extends ActionSupport {
private static final long serialVersionUID = 1L;
//private String data = "'Afghanistan', 'Zimbabwe', 'India', 'United States', 'Germany', 'China', 'Israel','Uganda', 'Geneva', 'Ghana'";
private List<String> countries;
private String country;
public String execute() throws Exception{
/*
countries = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(data, ",");
while (st.hasMoreTokens()) {
countries.add(st.nextToken().trim()); }
*/
return SUCCESS; }
public String getCountry() { return this.country; }
public List<String> getCountries() { return countries; }
public void setCountries(List<String> countries) { this.countries = countries; }
public void setCountry(String country) { this.country = country; }
}
index.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>Welcome</title>
<sx:head/> <!-- Will help to add required javascript and css -->
</head>
<body>
<h2>Struts 2.x Auto complete Example!</h2>
<s:url id="url" value="/success.jsp" />
<sx:div href="%{#url}" delay="2000">
Loading..........
</sx:div>
<s:form action="ajaxdemo">
<sx:autocompleter list="{'Afghanistan', 'Zimbabwe', 'India', 'United States',
'Germany', 'China', 'Israel','Uganda', 'Geneva', 'Ghana'}" name="country"
label="Country"/>
<s:submit></s:submit>
</s:form>
You have selected : <s:property value="country"/>
</body>
</html>
success.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
Welcome <s:property value="user"/>
Successful........
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<sx:tree name="tt" label="Tree">
<sx:treenode label="xyz" value="xyz">
<sx:treenode label="x"></sx:treenode>
<sx:treenode label="y"></sx:treenode>
<sx:treenode label="z"></sx:treenode>
</sx:treenode>
<sx:treenode label="abc"></sx:treenode>
<sx:treenode label="pqr"></sx:treenode>
</sx:tree>
struts.xml
<?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>
<constant name="struts.devMode" value="true"></constant>
<package name="p1" extends="struts-default">
<action name="ajaxdemo" class="com.javaskool.struts.AjaxCountryAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>10_Struts2WithAJAX</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
Click here to download complete code with jar files [5.2 MB]
Recent Comments