參數(shù)接收
Springmvc中,接收頁(yè)面提交的數(shù)據(jù)是通過(guò)方法形參來(lái)接收:
-
處理器適配器調(diào)用springmvc使用反射將前端提交的參數(shù)傳遞給controller方法的形參
-
springmvc接收的參數(shù)都是String類型,所以spirngmvc提供了很多converter(轉(zhuǎn)換器)在特殊情況下需要自定義converter,如對(duì)日期數(shù)據(jù)
基本數(shù)據(jù)類型
-
編寫controller
@Controller @RequestMapping("/account") public class AccountController { ? ? ?@RequestMapping("/findAccount5") ? ?public String findAccount5(String username,Model model){ ? ? ? ?model.addAttribute("msg", username); ? ? ? ?return "success"; ? } ? ? ? ?@RequestMapping("/findAccount6") ? ?public String findAccount6(String username,Integer age,Model model){ ? ? ? ?model.addAttribute("msg", username+" "+age); ? ? ? ?return "success"; ? } }
-
在index.jsp里面定義超鏈接
<a href="/account/findAccount5?username=eric">參數(shù)接收-基本數(shù)據(jù)類型</a> <a href="/account/findAccount6?username=eric&age=22">參數(shù)接收-多個(gè)基本數(shù)據(jù)類型</a>
POJO類型參數(shù)綁定
-
編寫pojo
public class Account implements Serializable { ? ?private Integer id; ? ?private String name; ? ?private Float money; ? ?private Address address; ? //省略get set toString方法 }
public class Address implements Serializable { ? ?private String provinceName; ? ?private String cityName; ? ? //省略get set toString方法 }
-
編寫controller
package com.by.controller; ? import com.by.pojo.Account; 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 org.springframework.web.servlet.ModelAndView; ? @Controller @RequestMapping("/account") public class AccountController { ? ? ?@RequestMapping("/saveAccount") ? ?public String saveAccount(Account account, Model model){ ? ? ? ?model.addAttribute("msg", account); ? ? ? ?return "success"; ? } }
-
在index.jsp里面添加表單
<form action="account/saveAccount" method="post"> ? 賬戶名稱:<input type="text" name="name"><br/> ? 賬戶金額:<input type="text" name="money"><br/> ? 賬戶省份:<input type="text" name="address.provinceName"><br/> ? 賬戶城市:<input type="text" name="address.cityName"><br/> ? ?<input type="submit" value="保存"> </form>
restful風(fēng)格
-
restful概述:
(Representational State Transfer,表現(xiàn)層狀態(tài)轉(zhuǎn)移):URL定位資源時(shí),用HTTP動(dòng)詞(GET,POST,DELETE,PUT)描述操作
restful風(fēng)格URL
-
在Restful之前的操作: http://127.0.0.1/user/query?id=1 根據(jù)用戶id查詢用戶數(shù)據(jù) http://127.0.0.1/user/save 新增用戶 http://127.0.0.1/user/update?id=1 修改用戶信息 http://127.0.0.1/user/delete?id=1 刪除用戶信息
-
RESTful用法: http://127.0.0.1/user/1 GET 根據(jù)用戶id查詢用戶數(shù)據(jù) http://127.0.0.1/user POST 新增用戶 http://127.0.0.1/user PUT 修改用戶信息 http://127.0.0.1/user/1 DELETE 刪除用戶信息
-
RESTful總結(jié):
Restful風(fēng)格就是請(qǐng)求url統(tǒng)一,根據(jù)不同的請(qǐng)求方式,請(qǐng)求不同的后臺(tái)方法。如果需要攜帶參數(shù),在url上使用/{}占位符。
@PathVaribale
-
作用
用于綁定url中的占位符。例如:/account/{id},這個(gè){id}就是url占位符
url支持占位符是spring3.0之后加入的,是springmvc支持rest風(fēng)格url的重要標(biāo)志。
-
描述需要使用指定的請(qǐng)求方式來(lái)請(qǐng)求該方法
@Controller @RequestMapping("/account") public class AccountController { ? ? @RequestMapping(value="/findAccount7/{id}") ? ?public String findAccount11(@PathVariable Integer id, Model model){ ? ? ? ?model.addAttribute("msg", id); ? ? ? ?return "success"; ? } }
-
測(cè)試:在index.jsp里面定義超鏈接
<a href="/account/findAccount7/123">restful傳參</a><br>
請(qǐng)求參數(shù)亂碼問(wèn)題
POST請(qǐng)求方式解決亂碼問(wèn)題
-
在web.xml里面設(shè)置編碼過(guò)濾器
<filter> ?<filter-name>CharacterEncodingFilter</filter-name> ?<filter-class> ? org.springframework.web.filter.CharacterEncodingFilter ?</filter-class> ?<!-- 設(shè)置過(guò)濾器中的屬性值 --> ?<init-param> ? ?<param-name>encoding</param-name> ? ?<param-value>UTF-8</param-value> ?</init-param> </filter> <!-- 過(guò)濾所有請(qǐng)求 --> <filter-mapping> ?<filter-name>CharacterEncodingFilter</filter-name> ?<url-pattern>/*</url-pattern> </filter-mapping>
-
測(cè)試
GET請(qǐng)求方式解決亂碼問(wèn)題
-
tomcat對(duì)GET和POST請(qǐng)求處理方式是不同的,GET請(qǐng)求的編碼問(wèn)題,要改tomcat的 配置信息,如下:
<plugin> ? ?<groupId>org.apache.tomcat.maven</groupId> ? ?<artifactId>tomcat7-maven-plugin</artifactId> ? ?<version>2.2</version> ? ?<configuration> ? ? ? ?<port>8080</port> ? ? ? ?<path>/</path> ? ? ? ?<!--按UTF-8進(jìn)行編碼--> ? ? ? ?<uriEncoding>UTF-8</uriEncoding> ? ?</configuration> </plugin>
自定義類型轉(zhuǎn)換器
使用場(chǎng)景
-
在index.jsp里面添加日期類型
? ?<form action="account/saveAccount" method="post"> ? ? 賬戶名稱:<input type="text" name="name"><br/> ? ? 賬戶金額:<input type="text" name="money"><br/> ? ? 賬戶省份:<input type="text" name="address.provinceName"><br/> ? ? 賬戶城市:<input type="text" name="address.cityName"><br/> ? ? 開戶日期:<input type="text" name="date"><br/> ? ? ?<input type="submit" value="保存"> ? ?</form>
-
在pojo里面添加日期類型
public class Account implements Serializable { ? ?private Integer id; ? ?private String name; ? ?private Float money; ? ?private Address address; ? ?//添加日期類型 ? ?private Date date; ? ?//省略get set toString方法 } ? ?
-
測(cè)試
-
原因
我們前臺(tái)傳遞的是字符串類型的參數(shù),但是后臺(tái)使用的是Date類型接收的。我們期望springmvc可以幫我們做數(shù)據(jù)類型的自動(dòng)轉(zhuǎn)換,顯然沒有做,所以我們需要自己自定義類型轉(zhuǎn)換器。
自定義類型轉(zhuǎn)換器
-
Converter接口說(shuō)明:
-
定義一個(gè)類,實(shí)現(xiàn)Converter接口
public class DateConverter implements Converter<String, Date> { ? ?@Override ? ?public Date convert(String source) { ? ? ? ?try { ? ? ? ? ? ?DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); ? ? ? ? ? ?return format.parse(source); ? ? ? } catch (Exception e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? } ? ? ? ?return null; ? } }
-
在 springmvc.xml配置文件中配置類型轉(zhuǎn)換器
<!--開啟springmvc注解支持--> ? ?<mvc:annotation-driven conversion-service="cs"></mvc:annotation-driven> ? ?<!-- 配置類型轉(zhuǎn)換器工廠 --> ? ?<bean id="cs" ? ? ? ? ?class="org.springframework.context.support.ConversionServiceFactoryBean"> ? ? ? ?<!-- 給工廠注入一個(gè)新的類型轉(zhuǎn)換器 --> ? ? ? ?<property name="converters"> ? ? ? ? ? ?<set> ? ? ? ? ? ? ? ?<!-- 配置自定義類型轉(zhuǎn)換器 --> ? ? ? ? ? ? ? ?<bean class="com.by.converter.DateConverter"></bean> ? ? ? ? ? ?</set> ? ? ? ?</property> ? ?</bean>
使用ServletAPI接收參數(shù)
-
編寫controller文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-819397.html
@Controller @RequestMapping("/account") public class AccountController { ? ? ?@RequestMapping("/findAccount8") ? ?public String findAccount8(HttpServletRequest request, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpServletResponse response){ ? ? ? ?String username = request.getParameter("name"); ? ? ? ?String age = request.getParameter("age"); ? ? ? ?request.setAttribute("msg",username+" "+age); ? ? ? ?return "success"; ? } }
-
在index.jsp里面定義超鏈接文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-819397.html
<a href="/account/findAccount8?username=eric&age=19">Servlet接收參數(shù)</a>
到了這里,關(guān)于Spring MVC 參數(shù)接收的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!