Spring MVC表单复选框有助于同时选择多个选项。该标签呈现类型为checkbox的HTML输入标签。
句法
<
form:checkbox path="abc" value="http://www.srcmini.com/element"/>
除了复选框标签之外, Spring MVC表单标签库还包含复选框标签。该标签呈现带有类型复选框的多个HTML输入标签。仅当你不想列出视图页面中的所有元素时才使用此标记。在这种情况下, 你可以在运行时提供元素并将其传递给标签。由于用户可以选择多个选项, 因此你需要传递Array, List或Map类型的元素。
句法
<
form:checkboxes path="abc" items="${object.elementList}"/>
Spring MVC表单复选框的示例 1.将依赖项添加到pom.xml文件。
<
!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<
dependency>
<
groupId>
org.springframework<
/groupId>
<
artifactId>
spring-webmvc<
/artifactId>
<
version>
5.1.1.RELEASE<
/version>
<
/dependency>
<
!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<
dependency>
<
groupId>
javax.servlet<
/groupId>
<
artifactId>
servlet-api<
/artifactId>
<
version>
3.0-alpha-1<
/version>
<
/dependency>
<
!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<
dependency>
<
groupId>
javax.servlet<
/groupId>
<
artifactId>
jstl<
/artifactId>
<
version>
1.2<
/version>
<
/dependency>
<
!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<
dependency>
<
groupId>
org.apache.tomcat<
/groupId>
<
artifactId>
tomcat-jasper<
/artifactId>
<
version>
9.0.12<
/version>
<
/dependency>
2.创建bean类
Reservation.java
package com.srcmini;
public class Reservation { private String firstName;
private String lastName;
private String Gender;
private String[] Food;
public Reservation() { } public String getFirstName() {return firstName;
} public void setFirstName(String firstName) {this.firstName = firstName;
} public String getLastName() {return lastName;
} public void setLastName(String lastName) {this.lastName = lastName;
} public String getGender() {return Gender;
} public void setGender(String gender) {Gender = gender;
} public String[] getFood() {return Food;
} public void setFood(String[] food) {Food = food;
}}
3.创建控制器类
【Spring MVC表单复选框用法示例】ReservationController.java
package com.srcmini;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/reservation")@Controllerpublic class ReservationController { @RequestMapping("/bookingForm")public String bookingForm(Model model){//create a reservation object Reservation res=new Reservation();
//provide reservation object to the model model.addAttribute("reservation", res);
return "reservation-page";
}@RequestMapping("/submitForm")public String submitForm(@ModelAttribute("reservation") Reservation res){ return "confirmation-form";
}}
4.在web.xml文件中提供控制器的条目
web.xml
<
?xml version="1.0" encoding="UTF-8"?>
<
web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<
display-name>
SpringMVC<
/display-name>
<
servlet>
<
servlet-name>
spring<
/servlet-name>
<
servlet-class>
org.springframework.web.servlet.DispatcherServlet<
/servlet-class>
<
load-on-startup>
1<
/load-on-startup>
<
/servlet>
<
servlet-mapping>
<
servlet-name>
spring<
/servlet-name>
<
url-pattern>
/<
/url-pattern>
<
/servlet-mapping>
<
/web-app>
5.在xml文件中定义bean
spring-servlet.xml
<
?xml version="1.0" encoding="UTF-8"?>
<
beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">
<
!-- Provide support for component scanning -->
<
context:component-scan base-package="com.srcmini" />
<
!--Provide support for conversion, formatting and validation -->
<
mvc:annotation-driven/>
<
!-- Define Spring MVC view resolver -->
<
bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<
property name="prefix" value="http://www.srcmini.com/WEB-INF/jsp/">
<
/property>
<
property name="suffix" value="http://www.srcmini.com/.jsp">
<
/property>
<
/bean>
<
/beans>
6.创建请求的页面
index.jsp
<
!DOCTYPE html>
<
html>
<
head>
<
title>
Railway Registration Form<
/title>
<
/head>
<
body>
<
a href="http://www.srcmini.com/reservation/bookingForm">
Click here for reservation.<
/a>
<
/body>
<
/html>
7.创建视图组件
Reservation-page.jsp
<
%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<
!DOCTYPE html>
<
html>
<
head>
<
title>
Reservation Form<
/title>
<
/head>
<
h3>
Railway Reservation Form<
/h3>
<
body>
<
form:form action="submitForm" modelAttribute="reservation">
First name: <
form:input path="firstName" />
<
br>
<
br>
Last name: <
form:input path="lastName" />
<
br>
<
br>
Gender: Male<
form:radiobutton path="Gender" value="http://www.srcmini.com/Male"/>
Female<
form:radiobutton path="Gender" value="http://www.srcmini.com/Female"/>
<
br>
<
br>
Meals:BreakFast<
form:checkbox path="Food" value="http://www.srcmini.com/BreakFast"/>
Lunch<
form:checkbox path="Food" value="http://www.srcmini.com/Lunch"/>
Dinner<
form:checkbox path="Food" value="http://www.srcmini.com/Dinner"/>
<
br>
<
br>
<
input type="submit" value="http://www.srcmini.com/Submit" />
<
/form:form>
<
/body>
<
/html>
确认页面.jsp
<
%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<
!DOCTYPE html>
<
html>
<
body>
<
p>
Your reservation is confirmed successfully. Please, re-check the details.<
/p>
First Name : ${reservation.firstName} <
br>
Last Name : ${reservation.lastName} <
br>
Gender: ${reservation.gender}<
br>
Meals: <
ul>
<
c:forEach var="meal" items="${reservation.food}">
<
li>
${meal}<
/li>
<
/c:forEach>
<
/ul>
<
/body>
<
/html>
输出
文章图片
文章图片
文章图片
文章图片
下载此示例(使用Eclipse开发)
推荐阅读
- Spring MVC Tiles用法示例详解
- Spring MVC分页示例图解
- Spring MVC数字验证项目示例图解
- Spring MVC表单下拉列表用法示例
- Spring MVC自定义验证项目示例图解
- Android 模拟器下载编译及调试
- [Android开发]emulator无法启动的问题
- (转)android系统开发 AP 和 BP 简要说明
- Android Studio安装部署系列二十八Android Studio查看其它APP的布局结构