激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringMVC表單標(biāo)簽知識點詳解

SpringMVC表單標(biāo)簽知識點詳解

2021-01-24 11:24Erola Java教程

這篇文章主要為大家詳細介紹了SpringMVC表單標(biāo)簽知識點,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本篇我們來學(xué)習(xí)spring mvc表單標(biāo)簽的使用,借助于spring mvc提供的表單標(biāo)簽可以讓我們在視圖上展示webmodel中的數(shù)據(jù)更加輕松。

一.首先我們先做一個簡單了例子來對spring mvc表單表單標(biāo)簽的使用有一個大致的印象,然后再結(jié)合例子對各個標(biāo)簽介紹一下如何使用。

1.首先,在com.demo.web.models包中添加一個模型tagsmodel內(nèi)容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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內(nèi)容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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內(nèi)容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<%@ 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 標(biāo)簽:<form:input path="username"/><br/>
  password 標(biāo)簽:<form:password path="password"/><br/>
  綁定boolean的checkbox 標(biāo)簽:<br/>
  <form:checkbox path="testboolean"/><br/>
  綁定array的checkbox 標(biāo)簽:<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 標(biāo)簽:<br/>
  <form:checkboxes path="selectarray" items="${contentmodel.testarray}"/><br/>
  綁定map的checkboxs 標(biāo)簽:<br/>
  <form:checkboxes path="selectids" items="${contentmodel.testmap}"/><br/>
  綁定integer的radiobutton 標(biāo)簽:<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 標(biāo)簽:<br/>
  <form:radiobuttons path="selectid" items="${contentmodel.testmap}"/><br/>
  綁定map的select 標(biāo)簽:<br/>
  <form:select path="selectid" items="${contentmodel.testmap}"/><br/>
  不綁定items數(shù)據(jù)直接在form:option添加的select 標(biāo)簽:<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數(shù)據(jù)直接在html的option添加的select 標(biāo)簽:<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 標(biāo)簽:<br/>
  <form:select path="selectid">
   <option/>請選擇人員
   <form:options items="${contentmodel.testmap}"/>
  </form:select><br/>
  textarea 標(biāo)簽:
  <form:textarea path="remark"/><br/>
 
  <input type="submit" value="submit" />
  
 </form:form>
</body>
</html>

4.運行測試:

SpringMVC表單標(biāo)簽知識點詳解

二.下面我們來介紹各個標(biāo)簽的使用方法。

1.要使用spring mvc提供的表單標(biāo)簽,首先需要在視圖頁面添加:

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

2.form標(biāo)簽:

?
1
<form:form modelattribute="contentmodel" method="post">

modelattribute屬性指定該form綁定的是哪個model,當(dāng)指定了對應(yīng)的model后就可以在form標(biāo)簽內(nèi)部其它表單標(biāo)簽上通過為path指定model屬性的名稱來綁定model中的數(shù)據(jù)了,method屬性指定form的提交方式如get、post等。

3.input標(biāo)簽:

?
1
<form:input path="username"/>

會生成一個type為text的html input標(biāo)簽,通過path屬性來指定要綁定的model中的值。

4.password標(biāo)簽:

?
1
<form:password path="password"/>

會生成一個type為password的html input標(biāo)簽,通過path屬性來指定要綁定的model中的值。

5.checkbox標(biāo)簽:

會生成一個type為checkbox的html input標(biāo)簽,支持綁定boolean、數(shù)組、list或set類型的數(shù)據(jù)。

綁定boolean數(shù)據(jù)會生成一個復(fù)選框,當(dāng)boolean為true該復(fù)選框為選定狀態(tài),false為不選定狀態(tài)。

?
1
<form:checkbox path="testboolean"/>

SpringMVC表單標(biāo)簽知識點詳解

綁定數(shù)組、list或set類型的數(shù)據(jù)(以數(shù)組作為演示)如果綁定的數(shù)據(jù)中有對應(yīng)checkbox指定的value時則為選定狀態(tài),反之為不選定狀態(tài):

綁定array的checkbox 標(biāo)簽:<br/>

?
1
2
3
4
<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 路人丁

SpringMVC表單標(biāo)簽知識點詳解

6.checkboxs標(biāo)簽:

會根據(jù)綁定的items數(shù)據(jù)生成一組對應(yīng)的type為checkbox的html input標(biāo)簽,綁定的數(shù)據(jù)可以是數(shù)組、集合或map,其中checkboxs的path屬性也必指定,當(dāng)path中的數(shù)據(jù)有和items中的數(shù)據(jù)值同的時候?qū)?yīng)的checkbox為選定狀態(tài),反之為不選定狀態(tài)。

綁定集合數(shù)據(jù)(以數(shù)組作為演示):

?
1
2
綁定array的checkboxs 標(biāo)簽:<br/>
<form:checkboxes path="selectarray" items="${contentmodel.testarray}"/>

SpringMVC表單標(biāo)簽知識點詳解

這里需要注意的是當(dāng)使用el表達式綁定時需要連model的名稱一起指定如${contentmodel.testarray}而不能像path一樣只指定model對應(yīng)的屬性名稱。

但通常情況下我們需要的是checkbox顯示的是名稱,但選擇后提交的是對應(yīng)名稱的值,比如id,我們就可以通過綁定map來實現(xiàn)這個功能:

?
1
2
綁定map的checkboxs 標(biāo)簽:<br/>
<form:checkboxes path="selectids" items="${contentmodel.testmap}"/>

SpringMVC表單標(biāo)簽知識點詳解

生成的一組checkbox中其中一個checkbox的html代碼:

 

復(fù)制代碼 代碼如下:
<span><input name="selectids" type="checkbox" value="1" checked="checked"/><label for="selectids1">mapitem 路人甲</label></span>

 

7.radiobutton標(biāo)簽:

會生成一個type為radio的html input標(biāo)簽,如果綁定的數(shù)據(jù)的值對應(yīng)radiobutton指定的value時則為選定狀態(tài),反之為不選定狀態(tài):

?
1
2
3
4
綁定integer的radiobutton 標(biāo)簽:<br/>
<form:radiobutton path="radiobuttonid" value="0"/>0
<form:radiobutton path="radiobuttonid" value="1"/>1
<form:radiobutton path="radiobuttonid" value="2"/>2

SpringMVC表單標(biāo)簽知識點詳解

8.radiobuttons標(biāo)簽:

會根據(jù)綁定的items數(shù)據(jù)生成一組對應(yīng)的type為radio的html input標(biāo)簽,綁定的items數(shù)據(jù)可以是數(shù)組、集合或map,其中radiobuttons的path屬性也必指定,當(dāng)path的值和items中的某條數(shù)據(jù)值相同的時候?qū)?yīng)的radio為選定狀態(tài),反之為不選定狀態(tài),用法和checkboxs很相似。但要注意的是:checkboxs的path綁定的是集合radiobuttons的path綁定的是單個值:

?
1
2
綁定map的radiobuttons 標(biāo)簽:<br/>
<form:radiobuttons path="selectid" items="${contentmodel.testmap}"/>

SpringMVC表單標(biāo)簽知識點詳解

9.select標(biāo)簽:

會生成一個html select標(biāo)簽,綁定的items數(shù)據(jù)可以是數(shù)組、集合或map會根據(jù)items的內(nèi)容生成select里面的option選項,當(dāng)path的值和items中的某條數(shù)據(jù)值相同的時候?qū)?yīng)的option為選定狀態(tài),反之為不選定狀態(tài),用法與radiobuttons很相似:

?
1
2
綁定map的select 標(biāo)簽:<br/>
<form:select path="selectid" items="${contentmodel.testmap}"/>

SpringMVC表單標(biāo)簽知識點詳解

上面的是根據(jù)指定的items自動生成的option選項,但我們也可以不指定items手動添加select的option選項:

?
1
2
3
4
5
6
7
不綁定items數(shù)據(jù)直接在form:option添加的select 標(biāo)簽:<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>

SpringMVC表單標(biāo)簽知識點詳解

其中添加<option>請選擇人員</option> 可以讓在沒有進行選擇的情況下不指定任何默認值。

下面看一下form:option 與option的區(qū)別:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
不綁定items數(shù)據(jù)直接在form:option添加的select 標(biāo)簽:<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數(shù)據(jù)直接在html的option添加的select 標(biāo)簽:<br/>
<form:select path="selectid">
 <option>請選擇人員</option>
 <option value="1">路人甲</option>
 <option value="2">路人乙</option>
 <option value="3">路人丙</option>
</form:select><br/>

SpringMVC表單標(biāo)簽知識點詳解

由截圖的結(jié)果可以看出form:option 正確選擇了path中指定的selectid而option沒有,說明form:option有數(shù)據(jù)綁定功能option沒有。

另外我們也可以不為select指定items,而把items指定到form:option 上這兩種效果基本是一樣的,一點區(qū)別就是為select指定items再在select里面添加option是不起作用的會被items生成的option覆蓋掉,而把items指定到form:option 上則可以再在select里面添加option:

?
1
2
3
4
5
用form:option綁定items的select 標(biāo)簽:<br/>
<form:select path="selectid">
 <option/>請選擇人員
 <form:options items="${contentmodel.testmap}"/>
</form:select>

SpringMVC表單標(biāo)簽知識點詳解

10.textarea標(biāo)簽:

?
1
2
textarea 標(biāo)簽:
<form:textarea path="remark"/>

SpringMVC表單標(biāo)簽知識點詳解

會生成一個html textarea標(biāo)簽,通過path屬性來指定要綁定的model中的值。

11.hidden標(biāo)簽:

會生成一個type為hidden的html input標(biāo)簽,通過path屬性來指定要綁定的model中的值。

12.errors標(biāo)簽:

errors標(biāo)簽的用法在系列(6)—>數(shù)據(jù)驗證中已經(jīng)說明了,這里不在贅述。

spring mvc表單標(biāo)簽的內(nèi)容到此結(jié)束。

代碼下載

注:之前沒注意前11篇的示例代碼,不知道為什么當(dāng)時打包上傳上去的是沒有.project項目文件的,導(dǎo)致下載后不能直接導(dǎo)入eclipse運行,虛擬機又 被我刪掉了,這些示例代碼也沒有備份,但是代碼文件還在的,所以可以新建一個dynamic web project把對應(yīng)的配置文件和controller還有view導(dǎo)入就可以了,給大家造成的不便說聲抱歉。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/liukemng/p/3754211.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日日狠狠久久偷偷四色综合免费 | 国产一区成人 | 色女孩网 | 黄免费观看视频 | 极品国产91在线网站 | 欧美国产日韩在线 | 激情黄页 | 亚洲嫩草av | 亚洲国产精品久久久久久久久久久 | 欧美一区久久久 | 91久久91久久精品免观看 | 深夜影院一级毛片 | 色在线视频网站 | 精品久久久一二三区播放播放播放视频 | 91精品国产综合久久久动漫日韩 | 久久综合入口 | 精品久久久久久久久久久久久 | 国产拍拍拍三级费视频在线观看 | 免费嗨片首页中文字幕 | 久久福利剧场 | 成人福利在线免费观看 | 成人免费毛片在线观看 | 在线亚洲观看 | 深夜毛片免费看 | 免费人成年短视频在线观看网站 | 蜜桃精品视频在线观看 | 黄色小视频免费在线观看 | 久久精品久久精品国产大片 | 99精品无人区乱码在线观看 | 日韩精品一区二区亚洲 | 日韩在线视频观看免费 | 国产免费专区 | 免费午夜视频在线观看 | 2021国产精品| 九九视频精品在线观看 | 国产日产精品一区二区三区四区 | 色的综合| 日韩欧美精品电影 | 国产精品视频海角社区88 | 香蕉国产在线视频 | 国产小视频在线观看 |