不知道大家有没有这样做过:
在一个Web系统或网站的页面中会有很多的select域(<select><option></option></select>),这样的选择,所以在每个都得写一段相应的代码,实在很麻烦。如:
<select name=userType>
    <option>游客</option>
    <option>普通用户</option>
  <option>VIP用户</option>
</select>
<select name=postType>
    <option>岗位一</option>
    <option>岗位二</option>
  <option>岗位三</option>
</select>
<select name=jobType>
    <option>技术类</option>
    <option>业务类</option>
  <option>外语类</option>
</select>
假设这些都是基础的类型,那么请问怎样实现所有JSP中的select的这段代码都是用Struts的标签来实现呢?假如这三个select都在一个JSP或者不同的JSP中,但只要一打开相应的页面时,就可以通过struts的标签把相应类型的基础数据列出来呢?
是否需要自定义struts标签呢?相应的tld文件又该如何写呢?
不知道大家是否明白我的意思?

解决方案 »

  1.   

    看一下struts的例子吧,自己学最快
    <%@ page import="java.util.*, org.apache.struts.util.*"%>
    <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
    <%@ taglib uri="/tags/struts-html" prefix="html" %>
    <%@ taglib uri="/tags/struts-logic" prefix="logic" %>
    <html:html>
      <head>
        <title>Test html:select Tag</title><%
              String multipleValues[] =
               { "Multiple 0", "Multiple 1", "Multiple 2", "Multiple 3", "Multiple 4",
                 "Multiple 5", "Multiple 6", "Multiple 7", "Multiple 8", "Multiple 9" };
              pageContext.setAttribute("multipleValues", multipleValues);          Vector options = new Vector();
              options.add(new LabelValueBean("Label 0", "Value 0"));
              options.add(new LabelValueBean("Label 1", "Value 1"));
              options.add(new LabelValueBean("Label 2", "Value 2"));
              options.add(new LabelValueBean("Label 3", "Value 3"));
              options.add(new LabelValueBean("Label 4", "Value 4"));
              options.add(new LabelValueBean("Label 5", "Value 5"));
              options.add(new LabelValueBean("Label 6", "Value 6"));
              options.add(new LabelValueBean("Label 7", "Value 7"));
              options.add(new LabelValueBean("Label 8", "Value 8"));
              options.add(new LabelValueBean("Label 9", "Value 9"));
              pageContext.setAttribute("options", options);          String withNulls[] =
               { "String 0", null, "String 2" };
              pageContext.setAttribute("withNulls", withNulls);        %>
      </head>
      <body bgcolor="white">
        <div align="center">
          <h1>Test struts-html Select Tag</h1>
        </div>
        <p>Whatever changes you make to properties should be reflected when the page is redisplayed. Press "Save" to update, or "Cancel" to return to the main menu.</p><%--
                 Ensure that the form bean exists before the form tag is processed. This
                 is a simple (if not entirely clean) way of ensuring that the initial
                 values assigned during bean instantiation will be available within the
                 form, since reset() will not be called when the form bean already exists.             The right way to fix this is to modify this webapp so that it does not
                 refer directly to JSP pages, but goes through Action classes, and to
                 either modify the TestBean class, adding an initialize() method, or to
                 have an Action class set the initial values.
            --%>
        <jsp:useBean id="testbean" scope="session" class="org.apache.struts.webapp.exercise.TestBean" />
        <html:form action="/html-select-submit">
          <table border="0" width="100%">
            <tr>
              <th align="right">Single Select Allowed:</th>
              <td align="left">
                <html:select property="singleSelect" size="10">
                  <html:option value="Single 0">Single 0</html:option>
                  <html:option value="Single 1">Single 1</html:option>
                  <html:option value="Single 2">Single 2</html:option>
                  <html:option value="Single 3">Single 3</html:option>
                  <html:option value="Single 4">Single 4</html:option>
                  <html:option value="Single 5">Single 5</html:option>
                  <html:option value="Single 6">Single 6</html:option>
                  <html:option value="Single 7">Single 7</html:option>
                  <html:option value="Single 8">Single 8</html:option>
                  <html:option value="Single 9">Single 9</html:option>
                </html:select>
              </td>
            </tr>
            <tr>
              <th align="right">Multiple Select Allowed:</th>
              <td align="left">
                <html:select property="multipleSelect" size="10" multiple="true">
                  <html:options name="multipleValues" labelName="multipleValues" />
                </html:select>
              </td>
            </tr>
            <tr>
              <th align="right">Multiple Select From A Collection (Using &lt;html:options&gt;):</th>
              <td align="left">
                <html:select property="collectionSelect" size="10" multiple="true">
                  <html:options collection="options" property="value" labelProperty="label" />
                </html:select>
              </td>
            </tr>
            <tr>
              <th align="right">Multiple Select From A Collection (Using &lt;html:optionsCollection&gt;):</th>
              <td align="left">
                <html:select property="beanCollectionSelect" size="10" multiple="true">
                  <html:optionsCollection name="testbean" property="beanCollection" />
                </html:select>
              </td>
            </tr>
            <tr>
              <th align="right">Select With Labels From Resources:</th>
              <td align="left">
                <html:select property="resourcesSelect" size="3">
                  <html:option value="Resources 0" key="resources0" />
                  <html:option value="Resources 1" key="resources1" />
                  <html:option value="Resources 2" key="resources2" />
                </html:select>
              </td>
            </tr>
            <tr>
              <th align="right">Collection with null labels and values:</th>
              <td align="left">
                <html:select property="withNulls" size="3">
                  <html:options name="withNulls" labelName="withNulls" />
                </html:select>
              </td>
            </tr>
            <tr>
              <td align="right">
                <html:submit>Save</html:submit>
              </td>
              <td align="left">
                <html:reset>Reset</html:reset>
                <html:cancel>Cancel</html:cancel>
              </td>
            </tr>
          </table>
        </html:form>
      </body>
    </html:html>
      

  2.   

    看一下struts的帮助 options collection 功能是解决这个问题的
      

  3.   

    有没有简单点的例子啊?不知道你们是否明白偶的意思?
    就算用Struts的标签来实现,select中那些基础的选项怎样通过配置文件来设定而不是在每个页面的代码中写呢?
      

  4.   

    你可以继承org.apache.struts.taglib.html.OptionsTag,写自己的标签。
      

  5.   

    to down0011:
    你的例子中的
    <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
    <%@ taglib uri="/tags/struts-html" prefix="html" %>
    <%@ taglib uri="/tags/struts-logic" prefix="logic" %>
    可不可以把源码贴出来啊
      

  6.   

    <html:select property="catalog.id" styleClass="select">
    <logic:present name="allIssuecatalogs">
    <logic:notEmpty name="allIssuecatalogs">
    <html:options collection="allIssuecatalogs" property="id" labelProperty="name"/>
    </logic:notEmpty >
    </logic:present>
    </html:select>allIssuecatalogs是catalog的一个集合。
      

  7.   

    <html:select size="1" name="myForm" property="strCd" onchange="allChange();">
    <html:optionsCollection property="list_strCd" value="value" label="label" name="myForm" />
    </html:select>
    其中,myForm是你的ActionForm名.myForm中有属性String strCd.和List list_strCd.
    在Action中将ActionForm中的list_strCd赋值.list_strCd存储的是一堆HashMap.每个HashMap里有两个长度元素map.put("value",data1);map.put("label",data2);list_strCd.add(map);data1,data2是你从数据库中查到的value和label.比如为'01'和"岗位1".
      

  8.   

    如果是把
    <select name=userType>
        <option>游客</option>
        <option>普通用户</option>
      <option>VIP用户</option>
    </select>
    改用<html:radio>标签来显示又该如何实现呢?