SpringWeb <form:options>标签:定义下拉选择框或列表框的多个选项

  • 内容
  • 评论
  • 相关

该标签是 <form:select> 标签的子标签,用于定义表单的下拉选择框或列表框的多个选项。

语法:

<form:options itemValue="itemValue" itemLabel="itemLabel" items="items"/>

参数说明:

  • items:选项的提示信息。
  • itemValue:选项的值。
  • itemLabel:选项的值。

示例1

使用 Spring Web 标签实现在页面中添加下拉列表。
在 Fans 类中定义 fanList 属性,模拟一个控制器的表单对象,关键代码如下:

public class Fans{
  List fanList = new ArrayList();  //定义一个List集合
  public List getFanList(){  //生成get和set方法
    return fanList;
  }
  public void setFanList(List fanList){
    this.fanList = fanList;
  }
}

用 <form:options> 标签定义表单的下拉选择框组件,关键代码如下:

<%@page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
  <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
  <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%
       com.Fans fanBean = new com.Fans();
       List list = new ArrayList();  //定义一个List集合
       list.add("周一");//为List集合添加数据
       list.add("周二");
       list.add("周三");
       list.add("周四");
       list.add("周五");
       fanBean.setFanList(list);  //把集合保存到Fans对象中
       request.setAttribute("command",fanBean);  //把Fans对象保存到request中
     %>
      <form:form>
        计划完成日期在本周的:
        <form:select path = "fanList"multiple="false">
          <form:options items = "${command.fanList}"/>
        </form:select>
      </form:form>

示例2

应用 Spring Web 标签,将保存在 Map 集合中的信息以下拉列表形式显示在页面中。

在 Fans 类中定义 fanMap 属性,模拟一个控制器的表单对象,关键代码如下:

public class Fans{
  Map fanMap;  //定义一个Map集合
  public Map getFanMap(){  //生成get和set方法
    return fanMap;
  }
  public void setFanMap(Map fanMap){
    this.fanMap = fanMap;
  }
}

使用 <form:options> 标签定义表单的下拉选择框组件,关键代码如下:

<%@page contentType="text/html" import="java.util.*" pageEncoding="UTF-8"%>
  <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
  <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <%
       com.Fans fanBean = new com.Fans();
       Map map = new TreeMap();  //定义一个Map集合
       map.put("1","周一");  //为Map集合添加数据
       map.put("3","周三");
       map.put("5","周五");
       map.put("7","周日");
       fanBean.setFanMap(map);  //把集合保存到Fans对象中
       request.setAttribute("command",fanBean);  //把Fans对象保存到request中
     %>
      <form:form>
        下次招聘时间定在本周的:
        <form:select path="fanMap" multiple="false">
          <form:options items="${command.fanMap}"/>
        </form:select>
      </form:form>

本文标题:SpringWeb <form:options>标签:定义下拉选择框或列表框的多个选项

本文地址:https://www.hosteonscn.com/6573.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注