SpringMVC中參數(shù)綁定的作用是什么?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)是一家專業(yè)提供新?lián)崞髽I(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為新?lián)岜姸嗥髽I(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
參數(shù)綁定的定義
所謂參數(shù)綁定,簡(jiǎn)單來(lái)說(shuō)就是客戶端發(fā)送請(qǐng)求,而請(qǐng)求中包含一些數(shù)據(jù),那么這些數(shù)據(jù)怎么到達(dá) Controller。從客戶端請(qǐng)求key/value數(shù)據(jù)(比如get請(qǐng)求中包含的數(shù)據(jù)),經(jīng)過(guò)參數(shù)綁定,將key/value數(shù)據(jù)綁定到controller方法的形參上。springmvc中,接收頁(yè)面提交的數(shù)據(jù)是通過(guò)方法形參來(lái)接收。而不是在controller類定義成員變量接收。
SpringMVC中默認(rèn)支持的類型
自定義參數(shù)類型進(jìn)行綁定
對(duì)于有些參數(shù)類型,由于我們輸入的參數(shù)的類型與實(shí)體類中的參數(shù)類型不同,因此導(dǎo)致傳值不能成功,此時(shí),需要進(jìn)行參數(shù)類型綁定。下面以Date類型為例介紹怎樣進(jìn)行自定義參數(shù)類型綁定。
User.java
import java.util.Date; public class User { private Integer id; private String username; private String sex; private Date birthday; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex == null ? null : sex.trim(); } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
JSP頁(yè)面:注意輸入框的 name 屬性值和上面 POJO 實(shí)體類的屬性保持一致即可映射成功。
<form action="pojo" method="post"> 用戶id:<input type="text" name="id" value="2"></br> 用戶名:<input type="text" name="username" value="Marry"></br> 性別:<input type="text" name="sex" value="女"></br> 出生日期:<input type="text" name="birthday" value="2017-08-25"></br> <input type="submit" value="提交"> </form>
因?yàn)槲覀冚斎氲腷irthday是String類型,但是實(shí)體類中的birthday是Date類型,此時(shí),就不會(huì)綁定成功,請(qǐng)求相應(yīng)的controller會(huì)出現(xiàn)如下錯(cuò)誤:
因此我們需要進(jìn)行參數(shù)綁定。
參數(shù)綁定主要包括兩步:
1. 新建一個(gè)類實(shí)現(xiàn)Converter接口
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.core.convert.converter.Converter; //需要實(shí)現(xiàn)Converter接口,這里是將String類型轉(zhuǎn)換成Date類型 public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { //實(shí)現(xiàn)將字符串轉(zhuǎn)成日期類型(格式是yyyy-MM-dd HH:mm:ss) SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return dateFormat.parse(source); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } //如果參數(shù)綁定失敗返回null return null; } }
2. 在配置文件中進(jìn)行配置
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <!-- 自定義轉(zhuǎn)換器的類名 --> <bean class="com.ys.util.DateConverter"></bean> </property> </bean>
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
文章標(biāo)題:SpringMVC中參數(shù)綁定的作用是什么
網(wǎng)站網(wǎng)址:http://chinadenli.net/article4/ihjdoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、營(yíng)銷型網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、定制網(wǎng)站、網(wǎng)站改版、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)