第1章 Springmvc參數綁定
1.1 概述
1.1.1 注解介紹:
@RequestParam(value="id",defaultValue="1",required = true)用於映射路徑的參數
Value:代表參數名
defaultValue:用於設置參數的默認值,如果參數值為空,就會傳入這個默認值
required:表示規定這個參數必須有這個參數
1.1.2 Handler(Controller中的方法)參數列表中默認能接收的參數類型
HttpServletRequest、 HttpServletResponse 、HttpSession、Model/ModelMap
1.2 綁定簡單的數據類型參數
支持的數據類型:
參數類型推薦使用包裝數據類型,因為基礎數據類型不可以為null
整形:Integer、int
字符串:String
單精度:Float、float
雙精度:Double、double
布爾型:Boolean、boolean
說明:對於布爾類型的參數,請求的參數值為true或false。
處理器方法:
public String editItem(Model model,Integer id,Boolean status) throws Exception
請求url:
http://localhost:8080/xxx.action?id=2&status=false
@RequestMapping("/itemEdit") public String editItem(@RequestParam(value="id",defaultValue="1",required = true)Integer ids, Model model) { Items items = itemService.getItemById(ids); //把數據傳遞給頁面 model.addAttribute("item", items); //返回邏輯視圖 return "editItem"; } |
也可以直接在參數列表中直接寫,只要參數的名字和請求中傳遞的參數名一致就能接收
1.3 綁定pojo數據類型的參數
代碼:
@RequestMapping("/updateitem") public String updateItem(Items items) { itemService.updateItem(items); //返回成功頁面 return "success"; } |
注意:提交的表單中不要有日期類型的數據,否則會報400錯誤。如果想提交日期類型的數據需要用到后面的自定義參數綁定的內容
1.4 (自定義參數綁定)解決提交表單時日期轉換類型
分析:
因為日期傳遞過來的是String類型,所以會出現類型轉換錯誤
需要編寫一個工具類,將字符串轉換Date方法
package cn.peihua.springmvc.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;
public class DateConverter implements Converter<String,Date> {
@Override public Date convert(String source) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return simpleDateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; } } |
然后在springmvc.xml中進行配置
配置日期轉換器的bean
<!--配置日期轉換器--> <bean id = "formattingConversionService" class = "org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class = "cn.peihua.springmvc.utils.DateConverter"></bean> </set> </property> </bean> |
然后在注解驅動注冊中加入
<!-- 配置注解驅動,如果配置此標簽相當於配置了處理器映射器和適配器 --> <mvc:annotation-driven conversion-service="formattingConversionService"/> |
1.5 解決post亂碼問題
在web.xml中加入
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
以上可以解決post請求亂碼問題。
對於get請求中文參數出現亂碼解決方法有兩個:
修改tomcat配置文件添加編碼與工程編碼一致,如下:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
另外一種方法對參數進行重新編碼:
String userName new
String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8")
ISO8859-1是tomcat默認編碼,需要將tomcat編碼后的內容按utf-8編碼
1.6 綁定pojo包裝類
定義一個包裝的pojo類
package cn.peihua.springmvc.pojo;
public class QueryVo {
private Items items;
public Items getItems() { return items; }
public void setItems(Items items) { this.items = items; }
}
|
編寫controlle層handler,直接將QueryVo 寫在參數列表上即可
@RequestMapping("/queryitem") public String queryItem(QueryVo queryVo) { //打印綁定結果 System.out.println(queryVo.getItems().getName()); System.out.println(queryVo.getItems().getPrice());
return null; } |
在頁面中傳遞參數需要在屬性.屬性的方式進行傳遞