SpringMVC表单标签详解编程语言

本篇我们来学习Spring MVC表单标签的使用,借助于Spring MVC提供的表单标签可以让我们在视图上展示WebModel中的数据更加轻松。

一.首先我们先做一个简单了例子来对Spring MVC表单表单标签的使用有一个大致的印象,然后再结合例子对各个标签介绍一下如何使用。

1.首先,在com.demo.web.models包中添加一个模型TagsModel内容如下:

复制代码
package com.demo.web.models; 
import java.util.List; 
import java.util.Map; 
public class TagsModel{ 
private String username; 
private String password; 
private boolean testBoolean; 
private String[] selectArray; 
private String[] testArray; 
private Integer radiobuttonId; 
private Integer selectId; 
private List<Integer> selectIds;     
private Map<Integer,String> testMap; 
private String remark; 
public void setUsername(String username){ 
this.username=username; 
} 
public void setPassword(String password){ 
this.password=password; 
} 
public void setTestBoolean(boolean testBoolean){ 
this.testBoolean=testBoolean; 
} 
public void setSelectArray(String[] selectArray){ 
this.selectArray=selectArray; 
} 
public void setTestArray(String[] testArray){ 
this.testArray=testArray; 
} 
public void setRadiobuttonId(Integer radiobuttonId){ 
this.radiobuttonId=radiobuttonId; 
} 
public void setSelectId(Integer selectId){ 
this.selectId=selectId; 
} 
public void setSelectIds(List<Integer> selectIds){ 
this.selectIds=selectIds; 
} 
public void setTestMap(Map<Integer,String> testMap){ 
this.testMap=testMap; 
} 
public void setRemark(String remark){ 
this.remark=remark; 
} 
public String getUsername(){ 
return this.username; 
} 
public String getPassword(){ 
return this.password; 
} 
public boolean getTestBoolean(){ 
return this.testBoolean; 
} 
public String[] getSelectArray(){ 
return this.selectArray; 
} 
public String[] getTestArray(){ 
return this.testArray; 
} 
public Integer getRadiobuttonId(){ 
return this.radiobuttonId; 
} 
public Integer getSelectId(){ 
return this.selectId; 
} 
public List<Integer> getSelectIds(){ 
return this.selectIds; 
} 
public Map<Integer,String> getTestMap(){ 
return this.testMap; 
} 
public String getRemark(){ 
return this.remark; 
} 
}
复制代码

2.其次,在包com.demo.web.controllers添加一个TagsController内容如下:

复制代码
package com.demo.web.controllers; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.Map; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import com.demo.web.models.TagsModel; 
@Controller 
@RequestMapping(value = "/tags") 
public class TagsController { 
@RequestMapping(value="/test", method = {RequestMethod.GET}) 
public String test(Model model){ 
if(!model.containsAttribute("contentModel")){         
TagsModel tagsModel=new TagsModel(); 
tagsModel.setUsername("aaa"); 
tagsModel.setPassword("bbb"); 
tagsModel.setTestBoolean(true); 
tagsModel.setSelectArray(new String[] {"arrayItem 路人甲"}); 
tagsModel.setTestArray(new String[] {"arrayItem 路人甲","arrayItem 路人乙","arrayItem 路人丙"}); 
tagsModel.setRadiobuttonId(1); 
tagsModel.setSelectId(2); 
tagsModel.setSelectIds(Arrays.asList(1,2)); 
Map<Integer,String> map=new HashMap<Integer,String>(); 
map.put(1, "mapItem 路人甲"); 
map.put(2, "mapItem 路人乙"); 
map.put(3, "mapItem 路人丙"); 
tagsModel.setTestMap(map); 
tagsModel.setRemark("备注..."); 
model.addAttribute("contentModel", tagsModel); 
} 
return "tagstest"; 
} 
}
复制代码

3.最后,在views文件夹下添加视图tagstest.jsp内容如下:

复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8" 
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" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<form:form modelAttribute="contentModel" method="post">      
input 标签:<form:input path="username"/><br/> 
password 标签:<form:password path="password"/><br/> 
绑定boolean的checkbox 标签:<br/> 
<form:checkbox path="testBoolean"/><br/> 
绑定Array的checkbox 标签:<br/> 
<form:checkbox path="testArray" value="arrayItem 路人甲"/>arrayItem 路人甲 
<form:checkbox path="testArray" value="arrayItem 路人乙"/>arrayItem 路人乙 
<form:checkbox path="testArray" value="arrayItem 路人丙"/>arrayItem 路人丙 
<form:checkbox path="testArray" value="arrayItem 路人丁"/>arrayItem 路人丁<br/> 
绑定Array的checkboxs 标签:<br/> 
<form:checkboxes path="selectArray" items="${contentModel.testArray}"/><br/> 
绑定Map的checkboxs 标签:<br/> 
<form:checkboxes path="selectIds" items="${contentModel.testMap}"/><br/> 
绑定Integer的radiobutton 标签:<br/> 
<form:radiobutton path="radiobuttonId" value="0"/>0 
<form:radiobutton path="radiobuttonId" value="1"/>1 
<form:radiobutton path="radiobuttonId" value="2"/>2<br/> 
绑定Map的radiobuttons 标签:<br/> 
<form:radiobuttons path="selectId" items="${contentModel.testMap}"/><br/> 
绑定Map的select 标签:<br/> 
<form:select path="selectId" items="${contentModel.testMap}"/><br/> 
不绑定items数据直接在form:option添加的select 标签:<br/> 
<form:select path="selectId">   
<option>请选择人员</option> 
<form:option value="1">路人甲</form:option> 
<form:option value="2">路人乙</form:option> 
<form:option value="3">路人丙</form:option> 
</form:select><br/> 
不绑定items数据直接在html的option添加的select 标签:<br/> 
<form:select path="selectId">   
<option>请选择人员</option>  
<option value="1">路人甲</option> 
<option value="2">路人乙</option> 
<option value="3">路人丙</option>   
</form:select><br/> 
用form:option绑定items的select 标签:<br/> 
<form:select path="selectId">   
<option/>请选择人员 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11214.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论