关于<form:select>

本文详细介绍了如何在SSM框架中使用Spring的form:select标签来创建下拉列表,并展示了如何设置modelAttribute、path、items、itemValue及itemLabel等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  

  今天写基于SSM框架的程序,用到了<form:select>。由于一开始遇到了问题,所以在这里加以记录,供以后查看。

  直接看jsp页面的代码

 1   <%@ page language="java" contentType="text/html; charset=UTF-8"
 2       pageEncoding="UTF-8"%>
 3   <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
 4   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5   <html>
 6   <head>
 7   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8   <title>Insert title here</title>
 9   </head>
10   <body>
11       <form:form method="post" modelAttribute="employee">
12           姓名:<form:input path="lastName"/><br>
13           邮箱:<form:input path="email"/><br>
14           性别:<form:input path="gender"/><br>
15           部门:<form:select path="department" items="${departments}" itemValue="id" itemLabel="departmentName"></form:select>
16       </form:form>
17   </body>
18   </html>

  

  1.由于我们要引用Spring封装的form标签,所以一开始要配置引用标签:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

  

  2.对于

 <form:form method="post" modelAttribute="employee">

  其modelAttribute="employee"中的employee是  在controller层中跳转到该页面的方法中  绑定的对象。 即:

@RequestMapping("/add")
    public ModelAndView toAddPage(){
        ModelAndView mView=new ModelAndView();
        mView.addObject("employee",new Employee());
        mView.setViewName("add-stu");
        mView.addObject("departments", departmentDao.getDepartments());
        return mView;
    }

 

  3对于

部门:<form:select path="department.id" items="${departments}" itemValue="id" itemLabel="departmentName"></form:select>

  path="department"   为后续提取本方法中的对象预留了接口(类似于<input type="text" name="email">中的name

  items="${departments}"   表示你要遍历的列表

  itemValue="id"   表示列表选项中的value属性(当对下拉列表做出选择时后台传递的数据)【可能有些偏颇,欢迎指教】

  itemLabel="departmentName"  表示页面列表中现实的属性值

 

  实现结果:                                                  

        

  查看页面源码:

 

转载于:https://www.cnblogs.com/alternative/p/7413576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值