日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring mvc参数类型转换

發(fā)布時(shí)間:2024/9/30 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring mvc参数类型转换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1,需求

有時(shí)候我們接收到的參數(shù)為String類型的,但是我們需要將它們轉(zhuǎn)化為其他類型的如:date類型,枚舉類型等等,spring mvc為我們提供了這樣的功能。


2,配置文件

在springmvc.xml配置文件中添加如下代碼:

<mvc:annotation-driven conversion-service="conversionService" /> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><bean class="com.cmc.core.converters.StringToESportConverter" /></list></property> </bean> 別忘記上面那句,是注冊(cè)轉(zhuǎn)換器的。


3,添加StringToESportConverter類

package com.cmc.core.converters;import org.springframework.core.convert.converter.Converter;import com.gionee.xo.healthy.enums.ESport;/*** 配置spring mvc自動(dòng)接收ESport* * @author chenmc*/ public class StringToESportConverter implements Converter<String, ESport> {@Overridepublic ESport convert(String source) {String value = source.trim();if ("".equals(value)) {return null;}return ESport.get(Integer.parseInt(source));}}
4,添加ESport枚舉類

package com.cmc.xo.healthy.enums;import java.util.Locale;import com.cmc.core.base.utils.I18N;/*** 運(yùn)動(dòng)枚舉* * @author chenmc* @date 2017年4月18日 下午8:32:33*/ public enum ESport {run("0"),//跑步cycling("1");//騎行private final String value;private ESport(String v) {this.value = v;}public String toString() {return this.value;}public static ESport get(int v) {String str = String.valueOf(v);return get(str);}public static ESport get(String value) {for (ESport e : values()) {if (e.toString().equals(value)) {return e;}}return null;}public String getName() {return I18N.getEnumName(this, Locale.CHINA);} }
轉(zhuǎn)換主要用到了get(String value)這個(gè)方法


5,controller中代碼

@ApiOperation(value="獲取某用戶單種運(yùn)動(dòng)的總信息", notes="返回某用戶的運(yùn)動(dòng)總次數(shù)和總耗時(shí)總消耗") @RequestMapping( value = {"/sports/{useruid:.{32}}/{type:\\d{1}}/sum"}, method = RequestMethod.GET, produces = "application/json;charset=UTF-8") @ResponseBody public String get_count(HttpServletRequest request, @PathVariable String useruid, @PathVariable ESport type) {return BaseResultHP.jsonResultSuccess(so.getSum(useruid, type)); }url中傳入的type為String類型的數(shù)字,而我接收參數(shù)@PathVariable ESport type

總結(jié)

以上是生活随笔為你收集整理的Spring mvc参数类型转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。