The Struts2.2 configuration Json+ajax implements a three cascade (any cascade) drop-down list box.

Source: Internet
Author: User

Preface: Found some related information on the Internet, chose a way to apply to their own SSH framework, encountered a lot of difficulties (mainly in the background did not write well), tossing the two days finally realized the Ajax local refresh function, Million method to one, like login check, refresh table and other functions can be used this way, More than say the code.

1.action class:

private string Id= "";p rivate string Schoolid;private string Name= "";p rivate string classid;private string code=null;    Private Gangedservice Gangedservice; Public Gangedservice Getgangedservice () {return gangedservice;} public void Setgangedservice (Gangedservice gangedservice) {this.gangedservice = Gangedservice;} Public String Queryallschool () throws exception{//list schoollist = new Cascademenudao (). Findallschool (); list<school> schoollist = This.gangedService.findAllSchool ();//writes to the string received by the foreground page, the ID and name are separated by _ "School" for (int i = 0; I < schoollist.size (); i++) {School School = (School) schoollist.get (i); id + = School.getid () + "_"; name + = School.getname () + "_";} Code = ID + "|" + name + "|school";//system.out.println (code); return Actionsupport.success;//return SUCCESS;} Public String Queryallclass () throws Exception{id= ""; name= ""; int sId = Integer.parseint (schoolid);// Convert the parameters from the foreground to the int type//list schoollist = new Cascademenudao (). Findclassbyschoolid (SId); Writes the string, ID and Nam that are received by the foreground pageE is separated by _ list<class1> classlist = This.gangedService.findClassBySchoolId (sId);//End With "Class" System.out.println (" Hah "); for (int i = 0; i < classlist.size (); i++) {Class1 Class1 = (Class1) classlist.get (i); id + = Class1.getid () +" _ "; name + = Class1.getname () + "_";} Code = ID + "|" + name + "|class"; SYSTEM.OUT.PRINTLN (code); return Actionsupport.success;//return SUCCESS;} Public String queryallstudent () {id= ""; name= ""; int cId = Integer.parseint (classId);//list schoollist = new Cascademenudao (). Findstubyclassid (CId); Writes to the string received by the foreground page, the ID and name are separated by _ list<student> studentlist = This.gangedService.findStudentByclass (cId);//with " Student "End for" (int i = 0; i < studentlist.size (); i++) {Student student = (student) studentlist.get (i); id + = Student.ge TId () + "_"; name + = Student.getname () + "_";} Code = ID + "|" + name + "|student";//return actionsupport.success;return SUCCESS;} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String Getschoolid () {return schooLId;} public void Setschoolid (String schoolid) {this.schoolid = SchoolID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetClassID () {return classId;} public void Setclassid (String classId) {this.classid = classId;} @JSON (name= "code") public String GetCode () {return code;} public void Setcode (String code) {this.code = code;}

dao layer:

Public list<school> Findallschool () {//TODO auto-generated method Stubhibernatetemplate ht = This.gethibernatetemplate (); String hql = "from School s ORDER by s.id ASC",///from User u order by u.id asc@suppresswarnings ("unchecked") List<schoo    l> schoollist = Ht.find (HQL);    for (School school1:schoollist) {school1.getname (); } return schoollist;} @Overridepublic list<class1> findclassbyschoolid (int schoolid) {//TODO auto-generated method Stubhibernatetemplate ht = This.gethibernatetemplate (); String hql = "from Class1 where school_id = '" +schoolid+ "'"; @SuppressWarnings ("Unchecked") list<class1> classlist = Ht.find (HQL); return classlist;} @Overridepublic list<student> findstudentbyclass (int classId) {//TODO auto-generated method Stubhibernatetemplate ht = This.gethibernatetemplate (); String hql = "from Student where class_id= '" +classid+ "'"; @SuppressWarnings ("Unchecked") list<student> Studentlist = Ht.find (HQL); return studentlist;}

Struts.xml configuration:

    <package name= "ganged" extends= "Json-default" >          <action name= "Allschool" class= "Gangedaction" method= " Queryallschool ">             <result name=" Success "type=" JSON ">                <param name=" root ">code</param>             </result>          </action>          <action name= "Allclass" class= "Gangedaction" method= " Queryallclass ">              <result name=" Success "type=" JSON ">                <param name=" root ">code</param>            </result>          </action>          <action name= "Allstu" class= "Gangedaction" method= " Queryallstudent ">              <result name=" Success "type=" JSON ">                <param name=" root ">code</param >            </result>          </action>      </package>  


front ganged.jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%@ taglib prefix=" s "uri="/struts-tags "%><% String path = REQUEST.G  Etcontextpath ();  String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >



Table structure:

Show Results:



two.the difference between post and get in Ajax Xmlhttp.open () method

2010-05-13 11:58:40 |   Category: default category |   Tags: | report | font size large small &NBSP; subscription

Post: Send data with "POST", can be as large as 4MB, use request.form["..." on receiving page to get
Get: Use "get" method to send data, only 256KB, on the receiving page using request.querystring["..." Get

///////////////////////////////////////////////////////////////////////////////////////////////////////////

In general, parameters that are submitted using Ajax are simple strings that can be written directly to the URL parameter of the open method using the Get method, where the parameter of the Send method is null.
For example:
var url = "Login.jsp?user=xxx&pwd=xxx";
Xmlhttp.open ("GET", url,true);
Xmlhttp.send (NULL);
Alternatively, you can use the Send method to pass parameters. Using the Send method to pass parameters using the Post method, you need to set the Content-type header information, analog HTTP POST method to send a form, so that the server will know how to handle the content of the upload. The submission format of the parameter is the same as the URL in the Get method. You must first call the Open method before you can set the header information.
For example:
Xmlhttp.open ("POST", "login.jsp", true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
Xmlhttp.send ("user=" +username+ "&pwd=" +password);
It is important to note that depending on the method of submission, the Doget method and the Dopost method of the backend are called respectively by the two submissions.


The Struts2.2 configuration Json+ajax implements a three cascade (any cascade) drop-down list box.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.