@RequestParam(value="id",defaultValue="1",required = true)用於映射路徑的參數java
Value:表明參數名web
defaultValue:用於設置參數的默認值,若是參數值爲空,就會傳入這個默認值spring
required:表示規定這個參數必須有這個參數tomcat
HttpServletRequest、 HttpServletResponse 、HttpSession、Model/ModelMapmvc
支持的數據類型:app
參數類型推薦使用包裝數據類型,由於基礎數據類型不能夠爲nullide
整形:Integer、int工具
字符串:Stringpost
單精度:Float、floatui
雙精度: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"; } |
也能夠直接在參數列表中直接寫,只要參數的名字和請求中傳遞的參數名一致就能接收
代碼:
@RequestMapping("/updateitem") public String updateItem(Items items) { itemService.updateItem(items); //返回成功頁面 return "success"; } |
注意:提交的表單中不要有日期類型的數據,不然會報400錯誤。若是想提交日期類型的數據須要用到後面的自定義參數綁定的內容
分析:
由於日期傳遞過來的是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"/> |
在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編碼
定義一個包裝的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; } |
在頁面中傳遞參數須要在屬性.屬性的方式進行傳遞