1. 程式人生 > >Controller引數繫結

Controller引數繫結

1.1引數繫結

         處理器介面卡在執行Handler之前需要把http請求的key/value資料繫結到Handler方法形引數上。

1.1.1  預設支援的引數型別

處理器形參中新增如下型別的引數處理介面卡會預設識別並進行賦值。

1.1.1.1 HttpServletRequest

通過request物件獲取請求資訊

1.1.1.2 HttpServletResponse

通過response處理響應資訊

1.1.1.3 HttpSession

通過session物件得到session中存放的物件

1.1.1.4 Model/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(Model model,Integer id) throws Exception{

1.1.3.2字串

例子略

1.1.3.3單精度/雙精度

例子略

1.1.3.4布林型

處理器方法:

public String editItem(Model model,Integer id,Boolean status) throws Exception

請求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 Integer parameter '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物件中

頁面定義如下;

<input type="text" name="name"/>

<input type="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;

}

頁面定義:

<input type="text" name="items.name" />

<input type="text" name="items.price" />

Controller方法定義如下:

public String useraddsubmit(Model model,QueryVoqueryVo)throws Exception{

System.out.println(queryVo.getItems());

1.1.5  自定義引數繫結

1.1.5.1 需求

根據業務需求自定義日期格式進行引數繫結。

1.1.5.2 Converter

1.1.5.2.1自定義Converter

public class CustomDateConverter implements Converter<String, Date> {

    @Override

    public Date convert(String source) {

       try {

           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("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"

       <!-- 轉換器 -->

       <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方法傳遞

<input type="checkbox" name="item_id" value="001"/>

<input type="checkbox" name="item_id" value="002"/>

<input type="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.2List

List中存放物件,並將定義的List放在包裝類中,action使用包裝物件接收。

List中物件:

成績物件

Public class QueryVo {

Private List<Items> itemList;//商品列表

  //get/set方法..

}

包裝類中定義List物件,並新增get/set方法如下:

頁面定義如下:

<tr>

<td>

<input type="text" name=" itemsList[0].id" value="${item.id}"/>

</td>

<td>

<input type="text" name=" itemsList[0].name" value="${item.name }"/>

</td>

<td>

<input type="text" name=" itemsList[0].price" value="${item.price}"/>

</td>

</tr>

<tr>

<td>

<input type="text" name=" itemsList[1].id" value="${item.id}"/>

</td>

<td>

<input type="text" name=" itemsList[1].name" value="${item.name }"/>

</td>

<td>

<input type="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(Model model,QueryVoqueryVo)throws Exception{

System.out.println(queryVo.getItemList());

}

1.1.6.3Map

在包裝類中定義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(Model model,QueryVo queryVo)throws Exception{

System.out.println(queryVo.getStudentinfo());

}