JSP from the database to obtain data fill the Drop-down box to achieve the two-level linkage menu Method _jsp programming

Source: Internet
Author: User
Tags json

This article describes the JSP from the database to obtain data to fill the Drop-down box to achieve the two-level linkage menu method. Share to everyone for your reference, specific as follows:

Project come to an ending, now will encounter the more practical things to record down, wrote many times, I would like to remember this, in preparation for viewing!

1, first in the database to get the first drop-down box data:

<s:select listkey= "Tsfrom" id= "T_tsfrom" cssclass= "required" listvalue= "Tsfrom" cssstyle= "width:90" 
Tsfrom1 " 
name=" Tsfrom "theme=" simple "headerkey=" " 
onchange=" gettsfromdescribe (this); "Headervalue="-- Please choose--"/>

Background processing:

@Autowired 
Private Custommamanager custommamanager;//annotation quite easy to use Ah, the code has saved a large section 
tsfrom1 = Custommamanager.gettsfrom (); 

Service:

Get the complaint source public 
list<custommanage> Gettsfrom () {return 
  custommanagedao.gettsfrom (); 
}

Dao

/** Get Product Type **/public 
list<custommanage> Gettsfrom () {return 
  this.find ("SELECT DISTINCT new custommanage (Tsfrom, ' 2 ') From Custommanage C WHERE C.tsfrom isn't NULL order by Tsfrom ASC ");


Note: Oracle takes out duplicate values in Java code Unlike SQL Server, it has to use:

Copy Code code as follows:
SELECT distinct new Custommanage (Tsfrom, ' 2 ') from Custommanage C WHERE C.tsfrom was not NULL order by Tsfrom ASC

The new entity bean (Custommanage) then sets the constructor for the corresponding field in the entity bean, or it will appear that the background has returned a value, but the Drop-down box does not appear in the foreground drop-down box.

Public Custommanage (String khname,int s) { 
  this.khname=khname; 
} 
Public Custommanage (String cpname,string type) { 
  if ("1". Equals (Type)) { 
   This.cpname=cpname 
  } else if ("2". Equals (Type)) { 
   this.tsfrom=cpname; 
  } else if ("3". Equals (Type)) { 
   this.khname=cpname 
  }} 


Here you need to add the corresponding construction method to the bean's entity class, which is used to reuse it.

So far the first level of data preparation has been almost. Use the list set of S tags to get the values that are passed in the background.

2, the second level drop-down menu cascade = = Use Ajax to get data

Written in the onchange event of the first level list:

//complaint information Source information cascade function Gettsfromdescribe (ts_describe) {var tsfrom = $ (" #t_tsfrom "). Val (); 
 var Tstsfrom = $ ("#ts_tsFrom"). Val (); Ext.Ajax.request ({url: ' ${ctx}/complaints/complaints!gettsfrom.action ', params: {tsfrom:tsfrom//through JSON format 
   The foreground of each fetch is passed to the background}, Success:function (response) {var json = Ext.util.JSON.decode (response.responsetext); 
    if (json.success) {var data = json.<strong>cmlist</strong>; 
     if ("" = data) {alert ("Please select complaint type"); 
     InputForm.t_tsfrom.focus (); 
     $ ("#ts_tsFrom"). Empty ()//each time the last data needs to be emptied} else {$ ("#ts_tsFrom"). empty (); 
     Iterate over the acquired data for (var i = 0; i < data.length i++) {var id = data[i]; 
     var name = Data[i]; 
    $ ("#ts_tsFrom"). Append ("<option value=" + ID + "' >" + name + "</option>"); 
    } dwr.util.removeAllOptions (' Tstsfrom '); 
 Dwr.util.addOptions (' Tstsfrom ', data); 
} 
} 
} 
}); }

Background return Data:

public void Gettsfrom () throws Exception { 
  HttpServletResponse response = Servletactioncontext.getresponse (); 
  String ts_names = Tsfrom; 
  list<custommanage> list = Complaintsmanager.gettsdescribe (ts_names); 
  Response.setcontenttype ("Text/javascript"); Background control code 
  printwriter writer = Response.getwriter (); 
  Converts the resulting list collection to a JSON object to the foreground processing 
  Jsonarray j = jsonarray.fromobject (list); 
  Writer.println ("{' Success ': true, ' <strong>cmList</strong> ':" + j.tostring () + "}"); 
}

The ability to get the value dynamically from the database and implement level two menu cascading is almost done. It would be good for you and me to publish it.

Here is a raise is the value of the Drop-down box is not saved when the last time you click submit the value saved in the database cannot be populated to the Drop-down box.

Treatment methods:

var OP1 = document.getElementById ("OP1"). Value; 
if (op1!=null) { 
  $ ("#cp_validity"). Val (OP1);//Drag the value out into the Drop-down box. A turnip a pit 
} 
<input type= "hidden" id= "OP1" value= "${compdisposal.validity}" >//use an expression to remove the values from the existing database. Put it in a hidden field. 

I hope this article will help you with JSP program design.

Related Article

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.