欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年10月24日 10:40:02   作者:Erola  
這篇文章主要為大家詳細(xì)介紹了SpringMVC表單標(biāo)簽知識點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

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.其次,在包c(diǎn)om.demo.web.controllers添加一個(gè)TagsController內(nèi)容如下:

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)容如下:

<%@ 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.運(yùn)行測試:

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

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

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

2.form標(biāo)簽:

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

modelAttribute屬性指定該form綁定的是哪個(gè)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)簽:

<form:input path="username"/>

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

4.password標(biāo)簽:

<form:password path="password"/>

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

5.checkbox標(biāo)簽:

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

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

<form:checkbox path="testBoolean"/>

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

綁定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 路人丁

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ù)值同的時(shí)候?qū)?yīng)的checkbox為選定狀態(tài),反之為不選定狀態(tài)。

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

綁定Array的checkboxs 標(biāo)簽:<br/>
<form:checkboxes path="selectArray" items="${contentModel.testArray}"/>

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

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

綁定Map的checkboxs 標(biāo)簽:<br/>
<form:checkboxes path="selectIds" items="${contentModel.testMap}"/>

生成的一組checkbox中其中一個(gè)checkbox的html代碼:

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

7.radiobutton標(biāo)簽:

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

綁定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

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ù)值相同的時(shí)候?qū)?yīng)的radio為選定狀態(tài),反之為不選定狀態(tài),用法和checkboxs很相似。但要注意的是:checkboxs的path綁定的是集合radiobuttons的path綁定的是單個(gè)值:

綁定Map的radiobuttons 標(biāo)簽:<br/>
<form:radiobuttons path="selectId" items="${contentModel.testMap}"/>

9.select標(biāo)簽:

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

綁定Map的select 標(biāo)簽:<br/>
<form:select path="selectId" items="${contentModel.testMap}"/>

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

不綁定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>

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

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

不綁定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/>

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

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

用form:option綁定items的select 標(biāo)簽:<br/>
<form:select path="selectId"> 
 <option/>請選擇人員
 <form:options items="${contentModel.testMap}"/> 
</form:select>

10.textarea標(biāo)簽:

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

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

11.hidden標(biāo)簽:

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

12.errors標(biāo)簽:

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

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

代碼下載

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

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

相關(guān)文章

  • RabbitMQ消息有效期與死信的處理過程

    RabbitMQ消息有效期與死信的處理過程

    利用DLX,當(dāng)消息在一個(gè)隊(duì)列中變成死信?(dead?message)?之后,它能被重新publish到另一個(gè)Exchange,這個(gè)Exchange就是DLX,本文重點(diǎn)給大家介紹RabbitMQ消息有效期與死信的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • IDEA報(bào)錯(cuò)java.lang.OutOfMemoryError:Java?heap?space的解決辦法

    IDEA報(bào)錯(cuò)java.lang.OutOfMemoryError:Java?heap?space的解決辦法

    這篇文章主要給大家介紹了關(guān)于IDEA報(bào)錯(cuò)java.lang.OutOfMemoryError:Java?heap?space的解決辦法,出現(xiàn)這個(gè)問題的主要原因是項(xiàng)目運(yùn)行時(shí)的堆內(nèi)存不足引起的報(bào)錯(cuò),需要的朋友可以參考下
    2024-02-02
  • SpringBoot啟動并初始化執(zhí)行sql腳本問題

    SpringBoot啟動并初始化執(zhí)行sql腳本問題

    這篇文章主要介紹了SpringBoot啟動并初始化執(zhí)行sql腳本問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng)

    Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng)

    基礎(chǔ)扎不扎實(shí)只有在實(shí)戰(zhàn)中才能顯現(xiàn),本篇文章手把手帶你用Java+swing+Mysql實(shí)現(xiàn)商品銷售管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2022-01-01
  • JAVA圖形界面(GUI)之表格的示例代碼

    JAVA圖形界面(GUI)之表格的示例代碼

    這篇文章主要介紹了JAVA圖形界面(GUI)之表格的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 如何解決Maven無法拉取SNAPSHOT依賴問題

    如何解決Maven無法拉取SNAPSHOT依賴問題

    在使用Maven管理項(xiàng)目時(shí),可能會遇到無法拉取SNAPSHOT版本依賴的問題,這通常是因?yàn)镸aven默認(rèn)不支持直接拉取SNAPSHOT版本,遇到這樣的問題,可以通過在項(xiàng)目的pom.xml文件中添加<repositories>標(biāo)簽,并配置啟用SNAPSHOT的倉庫地址來解決
    2024-10-10
  • 基于maven的ssm框架整合的示例代碼

    基于maven的ssm框架整合的示例代碼

    本篇文章主要介紹了基于maven的ssm框架整合的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • 關(guān)于Hystrix的監(jiān)控及可視化面板

    關(guān)于Hystrix的監(jiān)控及可視化面板

    這篇文章主要介紹了關(guān)于Hystrix的監(jiān)控及可視化面板,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java使用Freemarker頁面靜態(tài)化生成的實(shí)現(xiàn)

    Java使用Freemarker頁面靜態(tài)化生成的實(shí)現(xiàn)

    這篇文章主要介紹了Java使用Freemarker頁面靜態(tài)化生成的實(shí)現(xiàn),頁面靜態(tài)化是將原來的動態(tài)網(wǎng)頁改為通過靜態(tài)化技術(shù)生成的靜態(tài)網(wǎng)頁,FreeMarker?是一個(gè)用?Java?語言編寫的模板引擎,它基于模板來生成文本輸,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-06-06
  • 淺談Storm在zookeeper上的目錄結(jié)構(gòu)

    淺談Storm在zookeeper上的目錄結(jié)構(gòu)

    這篇文章主要介紹了淺談Storm在zookeeper上的目錄結(jié)構(gòu)的相關(guān)內(nèi)容,涉及storm使用zookeeper的操作以及詳細(xì)結(jié)構(gòu)圖,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10

最新評論