搜索一下 下拉列表 联动问题 答案很多

解决方案 »

  1.   

    整个程序如下:
    index.jsp文件
    <%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
    <%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
    <c:choose>
        <c:when test="${requestScope.collection==null}">
    <jsp:useBean id="jspbean" class="mypackage.DBbean" scope="page"/>
    <c:set var="bean" value="${jspbean.collection}"/>
        </c:when>
        <c:otherwise>
    <c:set var="bean" value="${requestScope.collection}"/>
        </c:otherwise>
    </c:choose>
    <html:html locale="true">
    <head>
    <title>
    这是一个实验!
    </title>
    <html:base/>
    </head>
    <body bgcolor="#ffffff">
    <html:form action="myform.do" method="post" name="ceshiForm" type="mypackage.CeshiForm">
    <html:radio value="A" property="tableBean.radioAB" onclick="ceshiForm.submit()"/>A
    <html:radio value="B" property="tableBean.radioAB" onclick="ceshiForm.submit()"/>B
    <br>
    <html:select property="tableBean.sel">
       <html:options collection="bean" labelProperty="name" property="id"/>
    </html:select>
    </html:form>
    </body>
    </html:html>
    -------------------------------------------------
    Action文件
    package mypackage;
    import mypackage.*;
    import org.apache.struts.action.*;
    import javax.servlet.http.*;
    import java.util.*;
    public class CeshiAction extends Action {
      public ActionForward perform(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        CeshiForm ceshiForm = (CeshiForm) actionForm;
        DBbean bean=new DBbean();
        Collection coll=bean.getCollection(ceshiForm.getTableBean().getRadioAB());
        httpServletRequest.setAttribute("collection",coll);
        return actionMapping.findForward("success");
      }
    }
    --------------------------------------------------
    Form文件
    package mypackage;import mypackage.*;
    import org.apache.struts.action.*;
    import javax.servlet.http.*;public class CeshiForm extends ActionForm {
      TableBean tableBean=new TableBean();  public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
        ActionErrors errors=new ActionErrors();
        return errors;
      }
      public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
        this.tableBean=new TableBean();
      }
      public TableBean getTableBean() {
        return tableBean;
      }
      public void setTableBean(TableBean tableBean) {
        this.tableBean = tableBean;
      }
    }
    ---------------------------------------------------
    连接数据库,封装业务逻辑的bean
    package mypackage;
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import mypackage.*;
    public class DBbean {
      private Connection conn;
      public DBbean() {
        try{
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          conn=DriverManager.getConnection("jdbc:odbc:jspdev","sa","");
        }
        catch(Exception e){
          e.printStackTrace();
        }
      }
      public Collection getCollection(){
        return this.getCollection("A");
      }
      public Collection getCollection(String tableName){
        Collection collection=new ArrayList();
        try{
          Statement stmt=conn.createStatement();
          ResultSet rs=stmt.executeQuery("select * from "+tableName);
          while(rs.next()){
            TableBean temp=new TableBean();
            temp.setName(rs.getString(1));
            temp.setId(rs.getString(2));
            collection.add(temp);
          }
          rs.close();
          stmt.close();
        }
        catch(Exception e){
          e.printStackTrace();
        }    return collection;
      }
    }
    ------------------------------------------
    存储表中的数据的bean
    package mypackage;public class TableBean {
      private String name;
      private String id;
      private String radioAB;
      private String sel;
      public String getRadioAB() {
        return radioAB;
      }
      public void setRadioAB(String radioAB) {
        this.radioAB = radioAB;
      }
      public String getSel() {
        return sel;
      }
      public void setSel(String sel) {
        this.sel = sel;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getId() {
        return id;
      }
      public void setId(String id) {
        this.id = id;
      }
    }
    -------------------------------------------
    struts-config.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
    <struts-config>
      <form-beans>
        <form-bean name="ceshiForm" type="mypackage.CeshiForm" />
      </form-beans>
      <action-mappings>
        <action name="ceshiForm" type="mypackage.CeshiAction" validate="true" input="/index.jsp" scope="request" path="/myform">
         <forward name="success" path="/index.jsp"/>
        </action>
      </action-mappings>
    </struts-config>
    ---------------------------------------------
    数据库
    use jspdev
    go
    create table A
    (id varchar(10),
     name varchar(10),
    )
    create table B
    (id varchar(10),
     name varchar(10),
    )
    //请自己添加数据配置jspdev的odbc数据源,并创建数据库jspdev
    ----------------------------------------------
    程序用jb9完成,struts1.0,jstl都是jb9自带的