SpringMVC的表单标签库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37787069/article/details/79143955
1、 作用:将pojo的对象属性值与表单组件的内容绑定
2、 如何使用SpringMVC的标签库?
(1) JSP文件的顶部加入以下指令
<%@taglib uri="http://www.springframework.org/tags/form" 
prefix="form" %>

(2) 在form表单的modelAttribute/commandName属性名与加载到model的key一致的对应pojo对象进行绑定
默认form表单设置绑定model的key=command的value对象
radiobutton标签:实现单选
radiobuttons标签:实现一组单选
checkbox标签:
 1、绑定boolean数据
 2、绑定列表数据
checkboxs标签:绑定列表数据
select标签
 1、实现下拉菜单功能
 2、选项可以通过option实现

3、通过实例进一步了解SpringMVC的标签库

     前端页面:form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form:form modelAttribute="user" action="${pageContext.request.contextPath}/saveUser" >
	用户名:<form:input path="username"/><br/>
	密码:<form:input path="password"/><br/>
	性别:<form:radiobutton path="gender" value="男" label="男"/><form:radiobutton path="gender" value="女" label="女"/><br/>
	手机操作系统:<form:radiobuttons path="mobilesys" items="${allmobilesys}" /><br/>
	是否同意此协议:<form:checkbox path="agree"/><br/>
	技能:
		<form:checkbox path="telcols" value="画画" label="画画"/>
		<form:checkbox path="telcols" value="游泳" label="游泳"/>
		<form:checkbox path="telcols" value="编程" label="编程"/>
		<form:checkbox path="telcols" value="足球" label="足球"/>
		<br/>
		<!-- 
			items:数据源,以集合的el表达式描述
			itemLabel:设置那个属性作为显示文本
			itemValue:设置选中的值
		 -->
	拥有的车型:<form:checkboxes items="${allCarList}" path="carlist" itemLabel="brand" /><br/>
	来自那个国家:<form:select path="country">
				<%-- <form:option value="中国">中国</form:option>
				<form:option value="美国">美国</form:option>
				<form:option value="韩国">韩国</form:option> --%>
				<form:options items="${allCountry}" />
			  </form:select><br/>
	<input type="submit" value="提交"/>		  
</form:form>
</body>
</html>
后台控制器:FormTagController.java
package com.gec.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gec.pojo.Car;
import com.gec.pojo.User;

@Controller
public class FormTagController {

	@RequestMapping("/toForm")
	public String toForm(Model model)
	{
		User user=new User();
		user.setUsername("张三");
		user.setPassword("1111");
		
		user.setGender("女");
		
		List<String> allMobileSys=new ArrayList<>();
		allMobileSys.add("android");
		allMobileSys.add("ios");
		allMobileSys.add("winphone");
		allMobileSys.add("ubuntu");
		
		user.setMobilesys("winphone");
		
		user.setAgree(false);
		
		List<String> telcols=new ArrayList<>();
		telcols.add("画画");
		telcols.add("游泳");
/*		telcols.add("编程");
		telcols.add("足球");*/
		
		user.setTelcols(telcols);
		
		List<Car> allCarList=new ArrayList<>();
		
		Car car01=new Car("宝马","粤A000001",1000000.00f);
		Car car02=new Car("奔驰","粤A000002",1000000.00f);
		Car car03=new Car("奇瑞","粤A000003",100000.00f);
		Car car04=new Car("奥迪","粤A000004",1100000.00f);
		Car car05=new Car("QQ","粤A000005",50000.00f);
		
		allCarList.add(car01);
		allCarList.add(car02);
		allCarList.add(car03);
		allCarList.add(car04);
		allCarList.add(car05);
		
		List<Car> userCarlist=new ArrayList<>();
		userCarlist.add(car01);
		userCarlist.add(car02);
		userCarlist.add(car04);
		
		user.setCarlist(userCarlist);
		
		user.setCountry("美国");
		
		List<String> allCountry=new ArrayList<>();
		allCountry.add("中国");
		allCountry.add("美国");
		allCountry.add("澳大利亚");
		allCountry.add("沙特");

		model.addAttribute("user", user);
		model.addAttribute("allmobilesys", allMobileSys);
		model.addAttribute("allCarList", allCarList);
		model.addAttribute("allCountry", allCountry);

		return "form";
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(User user, Model model) {
		model.addAttribute("user", user);
		return "list";
	}
	
}
表单绑定的pojo:User.java
package com.gec.pojo;

import java.util.List;

public class User {
	
	private String username;
	private String password;
	private String gender;
	private String mobilesys;
	private boolean agree;
	
	private List<String> telcols;
	private List<Car> carlist;
	
	private String country;
	
	public User() {
		// TODO Auto-generated constructor stub
	}
	public User(String username, String password) {
		this.username = username;
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	public String getMobilesys() {
		return mobilesys;
	}
	public void setMobilesys(String mobilesys) {
		this.mobilesys = mobilesys;
	}
	
	public boolean isAgree() {
		return agree;
	}
	public void setAgree(boolean agree) {
		this.agree = agree;
	}
	
	public List<String> getTelcols() {
		return telcols;
	}
	public void setTelcols(List<String> telcols) {
		this.telcols = telcols;
	}
	
	public List<Car> getCarlist() {
		return carlist;
	}
	public void setCarlist(List<Car> carlist) {
		this.carlist = carlist;
	}
	
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}
}
 Car.java
package com.gec.pojo;

public class Car 
{
	private String brand;
	private String brandNo;
	private float price;
	
	public Car() {
		super();
	}
	public Car(String brand, String brandNo, float price) {
		this.brand = brand;
		this.brandNo = brandNo;
		this.price = price;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getBrandNo() {
		return brandNo;
	}
	public void setBrandNo(String brandNo) {
		this.brandNo = brandNo;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return brand + "," + brandNo + "," + price;
	}

}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页