處理器適配器在執行Handler之前需要把http請求的key/value數據綁定到Handler方法形參數上。
1.1.1 默認支持的參數類型
處理器形參中添加如下類型的參數處理適配器會默認識別並進行賦值。
1.1.1.1HttpServletRequest
通過request對象獲取請求信息
1.1.1.2HttpServletResponse
通過response處理響應信息
1.1.1.3HttpSession
通過session對象得到session中存放的對象
1.1.1.4Model/ModelMap
ModelMap是Model接口的實現類,通過Model或ModelMap向頁面傳遞數據,如下:
//調用service查詢商品信息
Items item = itemService.findItemById(id);
model.addAttribute("item",item);
頁面通過${item.XXXX}獲取item對象的屬性值。
使用Model和ModelMap的效果一樣,如果直接使用Model,springmvc會實例化ModelMap。
1.1.2 參數綁定介紹
註解適配器對RequestMapping標記的方法進行適配,對方法中的形參會進行參數綁定,早期springmvc採用PropertyEditor(屬性編輯器)進行參數綁定將request請求的參數綁定到方法形參上,3.X之後springmvc就開始使用Converter進行參數綁定。
1.1.3 簡單類型
當請求的參數名稱和處理器形參名稱一致時會將請求參數與形參進行綁定。
1.1.3.1 整型
public String editItem(Modelmodel,Integer id) throws Exception{
}
1.1.3.2 字符串
例子略
1.1.3.3 單精度/雙精度
例子略
1.1.3.4 布爾型
處理器方法:
public String editItem(Modelmodel,Integer id,Boolean status) throwsException
請求url:
http://localhost:8080/springmvc_mybatis/item/editItem.action?id=2&status=false
說明:對於布爾類型的參數,請求的參數值爲true或false。
1.1.3.5@RequestParam
使用@RequestParam常用於處理簡單類型的綁定。
value:參數名字,即入參的請求參數名字,如value=“item_id”表示請求的參數區中的名字爲item_id的參數的值將傳入;
required:是否必須,默認是true,表示請求中一定要有相應的參數,否則將報;
TTP Status 400 - Required Integer parameter'XXXX' is not present
defaultValue:默認值,表示如果請求中沒有同名參數時的默認值
定義如下:
public String editItem(@RequestParam(value="item_id",required=true)String id) {
}
形參名稱爲id,但是這裏使用value=" item_id"限定請求的參數名爲item_id,所以頁面傳遞參數的名必須爲item_id。
注意:如果請求參數中沒有item_id將跑出異常:
HTTP Status 500 - Required Integerparameter 'item_id' is not present
這裏通過required=true限定item_id參數爲必需傳遞,如果不傳遞則報400錯誤,可以使用defaultvalue設置默認值,即使required=true也可以不傳item_id參數值
1.1.4 pojo
1.1.4.1簡單pojo
將pojo對象中的屬性名於傳遞進來的屬性名對應,如果傳進來的參數名稱和對象中的屬性名稱一致則將參數值設置在pojo對象中
頁面定義如下;
<inputtype="text" name="name"/>
<inputtype="text" name="price"/>
Contrller方法定義如下:
@RequestMapping("/editItemSubmit")
public String editItemSubmit(Items items)throws Exception{
System.out.println(items);
請求的參數名稱和pojo的屬性名稱一致,會自動將請求參數賦值給pojo的屬性。
1.1.4.2包裝pojo
如果採用類似struts中對象.屬性的方式命名,需要將pojo對象作爲一個包裝對象的屬性,action中以該包裝對象作爲形參。
包裝對象定義如下:
Public class QueryVo {
private Items items;
}
頁面定義:
<inputtype="text" name="items.name" />
<input type="text"name="items.price" />
Controller方法定義如下:
public String useraddsubmit(Modelmodel,QueryVoqueryVo)throwsException{
System.out.println(queryVo.getItems());
1.1.5 自定義參數綁定
1.1.5.1需求
根據業務需求自定義日期格式進行參數綁定。
1.1.5.2Converter
1.1.5.2.1 自定義Converter
public class CustomDateConverter implements Converter<String,Date> {
@Override
public Date convert(String source) {
try {
SimpleDateFormat simpleDateFormat = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.parse(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
1.1.5.2.2 配置方式1
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
<!--conversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 轉換器 -->
<property name="converters">
<list>
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>
1.1.5.2.3 配置方式2(自學)
<!--註解適配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 自定義webBinder -->
<bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
</bean>
<!--conversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 轉換器 -->
<property name="converters">
<list>
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>
1.1.6 集合類
1.1.6.1 字符串數組
頁面定義如下:
頁面選中多個checkbox向controller方法傳遞
<inputtype="checkbox" name="item_id"value="001"/>
<inputtype="checkbox" name="item_id" value="002"/>
<inputtype="checkbox" name="item_id" value="002"/>
傳遞到controller方法中的格式是:001,002,003
Controller方法中可以用String[]接收,定義如下:
public String deleteitem(String[]item_id)throws Exception{
System.out.println(item_id);
}
1.1.6.2 List
List中存放對象,並將定義的List放在包裝類中,action使用包裝對象接收。
List中對象:
成績對象
Public class QueryVo {
Private List<Items> itemList;//商品列表
//get/set方法..
}
包裝類中定義List對象,並添加get/set方法如下:
頁面定義如下:
<tr>
<td>
<inputtype="text" name=" itemsList[0].id" value="${item.id}"/>
</td>
<td>
<inputtype="text" name=" itemsList[0].name" value="${item.name }"/>
</td>
<td>
<inputtype="text" name=" itemsList[0].price" value="${item.price}"/>
</td>
</tr>
<tr>
<td>
<inputtype="text" name=" itemsList[1].id" value="${item.id}"/>
</td>
<td>
<inputtype="text" name=" itemsList[1].name" value="${item.name }"/>
</td>
<td>
<inputtype="text" name=" itemsList[1].price" value="${item.price}"/>
</td>
</tr>
上邊的靜態代碼改爲動態jsp代碼如下:
<c:forEach items="${itemsList }" var="item"varStatus="s">
<tr>
<td><input type="text"name="itemsList[${s.index}].name" value="${item.name }"/></td>
<td><input type="text"name="itemsList[${s.index}].price" value="${item.price }"/></td>
.....
.....
</tr>
</c:forEach>
Contrller方法定義如下:
public String useraddsubmit(Modelmodel,QueryVoqueryVo)throwsException{
System.out.println(queryVo.getItemList());
}
1.1.6.3 Map
在包裝類中定義Map對象,並添加get/set方法,action使用包裝對象接收。
包裝類中定義Map對象如下:
Public class QueryVo {
private Map<String, Object> itemInfo= new HashMap<String, Object>();
//get/set方法..
}
頁面定義如下:
<tr>
<td>學生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年齡:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr>
Contrller方法定義如下:
public String useraddsubmit(Modelmodel,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getStudentinfo());
}