Este artigo descreve o efeito do menu de link secundário implementado por Java. Compartilhe -o para sua referência, como segue:
Código JSP:
<%@ Page Language = "Java" PageEncoding = "UTF-8"%> <html> <head> <title> Menu secundário Demonstração de Linkage </ititle> <script type = "text/javascript"> var req; window.onload = function () {// Função quando carregamento de página} função alteração_select () {// Chame essa função quando a opção da primeira caixa suspensa altera var província = document.getElementById ('Província'). var url = "selecione? id ="+ escape (província); if (window.xmlHttPrequest) {req = new XmlHttPrequest (); } else if (window.activexObject) {req = new ActivexObject ("microsoft.xmlhttp"); } if (req) {req.open ("get", url, true); // Especifique a função de retorno de chamada para retorno de chamada req.onreadystatechange = retorno de chamada; req.send (nulo); }} // Função de retorno de chamada Função de chamada () {if (req.readyState == 4) {if (req.status == 200) {parseMessage (); // parse xml document} else {alert ("não pode obter informações de descrição:" + req.statustext); }}} // Analisar o método que retorna a função xml parseMessage () {var xmldoc = req.Responsexml.documentElement; // obtenha o documento XML retornado var xsel = xmldoc.getElementsByTagname ('select'); // Obtenha todas as tags <select> no documento XML var select_root = document.getElementById ('City'); // Obtenha a segunda caixa suspensa na página da web select_root.options.length = 0; // Toda vez que você obtém novos dados, limpe o comprimento de cada dois quadros suspensos por 0 para (var i = 0; i <xsel.length; i ++) {var xValue = xsel [i] .childnodes [0] .FirstChild.NodEvalue; // Obtenha o valor da primeira tag em cada tag <select>, ou seja, o valor da tag <Value> var xText = xsel [i] .childnodes [1] .FirstChild.NodEvalue; // Obtenha o valor da segunda tag em cada tag <select>, ou seja, o valor da opção <text> tag var option = nova opção (xText, xvalue); // Crie um objeto de opção com base nos valores de cada conjunto de valores e tags de texto tente {select_root.add (opção); // Adicionar objeto Opção ao segundo caixa suspenso} catch (e) {}}} </sCript> </frige> <body> <body> align = "center" <xemening "</script> </head> <cody> <body> <d align =" center "<xorm> <form) =" Métodos "Post </bodyp"> <body> <body> <body> "Center = Center =" ("Post1" </bodyp "> <body>" Center = "Center =" <. <td align = "Center"> Exemplo de ligação secundária </td> </tr> <tr> <td> <select name = "província" id = "província" onchange = "alteração_select ()"> <! primeiro menu suspenso <opção </"0" 0 "> selecione </option> <puttion> <puttion> <! value = "3"> shandong </pption> </leclect> <select name = "city" id = "city"> <! Segundo menu suspenso> <opção value = "0"> selecione </pption> </leclect> </td> </tr> <tr> <td> <td> </td> </table> </form> </body- body> </body> </body> </body> </body> </body> </corporaCódigo Java:
pacote com; importar java.io.ioException; importar javax.servlet.Servletexception; importar javax.servlet.http.httpServlet; importar javax.servlet.http.htpServletLeTreQuest; import Javox.Servlet.httP.HttStLeStletRenstLeStLeFestrTleStLeFestrTletLeRestLePREFREFREFREFREFREFORTREFORTRETSLEFORTRAX.HTTP.HTTSTLEFTREFORTRESTLEFTREFORTRAX. SelectServlet estende httpServlet {private estático final serialversionuid = 1L; public selectServlet () {super (); } public void Destroy () {super.Destroy (); } public void Doget (solicitação httpServletRequest, httpServletResponse resposta) lança servletexception, ioexception {// Response.Setcharacterencoding ("GBK"); Response.setContentType ("Text/XML"); Response.setheader ("Cache-Control", "No-Cache"); request.Setcharacterencoding ("GBK"); Response.Setcharacterencoding ("UTF-8"); String TargetId = request.getParameter ("ID"). ToString (); System.out.println (TargetId); // Obtenha o valor do ID do parâmetro na solicitação string xml_start = "<selects>"; String xml_end = "</leclects>"; String xml = ""; if (TargetId.EqualSignorecase ("0")) {xml = "<select> <Value> 0 </value> <text> Selecione </sext> </leclect>"; } else if (TargetId.EqualSignorecase ("1")) {xml = "<select> <value> 1 </alue> <xext> ChanGping </text> </leclect>"; xml += "<select> <value> 2 </value> <xet> fengtai </sext> </select>"; xml += "<select> <value> 3 </value> <text> haidian </sext> </leclect>"; xml += "<select> <value> 4 </alue> <xt> Chaoyang </sext> </leclect>"; } else if (TargetId.equalsignorecase ("2")) {xml = "<select> <value> 1 </alue> <xext> distrito de Tanggu xml += "<select> <value> 2 </value> <text> distrito de Hangu </sext> </leclect>"; xml += "<select> <value> 3 </value> <xet> distrito de Dagang </sext> </leclect>"; xml += "<select> <value> 4 </value> <text> distrito dongli </xt> </leclect>"; } else {// Se for 3, o seguinte caractere será retornado xml = "<select> <value> 1 </value> <text> jinan </sext> </leclect>"; xml += "<select> <value> 2 </alue> <xt> xml += "<select> <value> 3 </value> <text> zibo </sext> </select>"; xml += "<select> <value> 4 </talue> <sext> zaozhuang </text> </leclect>"; } String last_xml = xml_start + xml + xml_end; Response.getWriter (). Write (last_xml); } public void DoPost (solicitação httpServletRequest, httpServletResponse resposta) lança servletexception, ioexception {doget (solicitação, resposta); } public void init () lança servletexception {}}Código XML:
<? xml versão = "1.0" coding = "utf-8"?> <web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http:/wwwwwwwwww.w3.org/200: xsi =" http:/wwwwwwwwww.w3.org/200: xsi ... xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <ervletname> selectSormtlet </sirletname> </servlet> <Verlet-mapping> <Verlet-name> selectServlet </servlet-name> <url-pattern>/select
Para mais conteúdo relacionado ao Java, os leitores interessados neste site podem visualizar os tópicos: "Estrutura de dados Java e tutorial de algoritmo", "Resumo da operação Java Dom Node Tips", "Resumo de Dicas de Operação de Arquivos e Diretórios de Java" e "Resumo das dicas de operação do Java Cache"
Espero que este artigo seja útil para a programação Java de todos.