An example of Ajax asynchronous invocation using XMLHttpRequest combined with struts2 __ajax

Source: Internet
Author: User
Tags eval

On the Internet to find information, it is really tangled, patchwork, although this writing is very simple, but it cost me a lot of Kung Fu, in order to help the students behind, resolute paste code, hope for everyone useful.

Function Description: Implement the asynchronous invocation using XMLHttpRequest to achieve the effect of the Select Cascade Drop-down menu.

The screenshot of the project is as follows (mainly for everyone to see the introduction of the jar package):

The code for the Action class:

Package com.action;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Java.util.Set;

Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionSupport;
	public class AjaxTest1 extends Actionsupport {private map<string,list<string>> datas;
	Private list<string> provinces;
	Private List<string> citys;
		Public AjaxTest1 () {datas = new hashmap<string,list<string>> ();
		String[] C1 = new string[]{"Wuhan", "Xiangfan", "Jingzhou", "Yichang"};
		string[] C2 = new string[]{"Haidian", "Changping", "Chaoyang"};
		Datas.put ("Hubei", Arrays.aslist (C1));
	Datas.put ("Beijing", Arrays.aslist (C2));
	Public list<string> getprovinces () {return provinces;
	public void Setprovinces (list<string> provinces) {this.provinces = provinces;
	Public list<string> Getcitys () {return citys; } public void Setcitys (List<string> citys) {This.citys= Citys;
		Public String loadprovinces () {list<string> List = new arraylist<string> ();
		set<string> set = Datas.keyset ();
		for (String s:set) {List.add (s);
		} System.out.println (list);
		This.setprovinces (list);
		System.out.println ("Provinces:" +provinces);
	return SUCCESS;
		public string Loadcitys () {String province = Servletactioncontext.getrequest (). GetParameter ("province");
		This.setcitys (Datas.get ("Hubei"));
		System.out.println ("Returned province value is:" +province);
		list<string> list = new arraylist<string> ();
		This.setcitys (Datas.get (province));
	return SUCCESS; }

Struts.xml File:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.0//en" " Dtds/struts-2.0.dtd "> <struts> <constant name=" Struts.enable.DynamicMethodInvocation "value=" false "/&
    Gt <constant name= "Struts.devmode" value= "true"/> <!--<package name= "Default" namespace= "/" extends= "strut S-default "> <default-action-ref name=" index "/> <global-results> <result N
            Ame= "Error" >/error.jsp</result> </global-results> <global-exception-mappings> 

        <exception-mapping exception= "java.lang.Exception" result= "error"/> </global-exception-mappings> <action name= "index" > <result type= "redirectaction" > <param name= "act Ionname ">HelloWorld</param> <param name=" namespace ">/example</param> </result> </action> </package> <include file= "Example.xml "/> ADD packages here--> <package name=" test1 "namespace="/"extends=" Json-default "> ; Action name= "loadprovinces" class= "Com.action.AjaxTest1" method= "loadprovinces" > <result name= "Success" type = "JSON" > <!--<param name= "includeproperties" >provinces</param>--> <param N Ame= "Excludeproperties" >citys</param> </result> </action> <action name= " Loadcitys "class=" Com.action.AjaxTest1 "method=" Loadcitys "> <result type=" JSON "> <param name=" E Xcludeproperties ">provinces</param> </result> </action> </package> </str Uts>

index.jsp Paging File:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% @taglib prefix= "s" uri= "/struts-tags"
%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

Well, it's done, grind and grind carefully.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.