SpringMvc作爲表現層框架最重要的功能莫過於接收數據和返回數據!
接收數據:參數綁定
返回數據:ModelAndView
1. 默認支持的參數類型
1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
4. Model和ModelMap
2. 簡單數據類型
當請求的參數名稱和處理器形參名稱一致時會將請求參數與形參進行綁定。從Request取參數的方法可以進一步簡化。
參數類型推薦使用包裝數據類型,因爲基礎數據類型不可以爲null
整形:Integer、int
字符串:String
單精度:Float、float
雙精度:Double、double
布爾型:Boolean、boolean
說明:對於布爾類型的參數,請求的參數值爲true或false。
3. pojo(entity)類型
如果提交的參數很多,或者提交的表單中的內容很多的時候可以使用pojo接收數據。要求pojo對象中的屬性名和表單中input的name屬性一致。SpringMvc是根據頁面name的值進行封裝進pojo類型
4. pojo包裝類型(vo)
pojo包裝類型(vo)
Public classQueryVo {
private Itemsitems;
getter and setter;
}
頁面這麼定義
//如果Controller中接收的是Vo,那麼頁面上input框的name屬性值要等於vo的屬性.屬性.屬性.....
<input type="text" name="items.name" />
<input type="text" name="items.price" />
controller方法這麼定義
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo) {
System.out.println(queryVo.getItems().getName());
System.out.println(queryVo.getItems().getPrice());
retur nnull;
}
5. 高級參數綁定(數組和集合)
1.接收數組(例如在做批量刪除的時候要接受一個id數組)
<!--頁面input框的name值要和接受的數組名稱相同,SpirngMvc會自動String轉換成Integer-->
public string delAll(Integer[]ids) throws Exception{
System.out.println(ids.toString());
return "";
}
1.1可擴展性地接受數組(我們不直接用Integer[]ids接受,而是將這個數組放進pojo包裝類中,頁面input框的name值要和pojo包裝類屬性數組名稱相同,然後在controller中直接接收vo就可以直接封裝)
public class QueryVo {
//商品對象
private Items items;
//訂單對象...
//批量刪除使用
private Integer[] ids;
getter and setter;
}
2.接收集合的數據(比如我們在批量修改的時候同樣可以將每個id的對象放進List集合中我們直接在後面獲取List集合就行,爲了可擴展性,我們同樣放進pojo包裝類中(vo)中;
vo的代碼如下!
public class QueryVo {
//商品對象
private Items items;
//訂單對象...
//用戶對象....
//批量刪除使用
private Integer[] ids;
//批量修改使用----這個
private List<Items> itemsList;
}
前臺頁面將數據封裝進list集合中(重點)—如何將複選框的數據封裝成pojo對象然後放入List集合中
<!--forEach標籤-->
<c:forEach items="${itemList }" var="item" varStatus="status">
<tr>
<!-- 如果批量刪除,可以用List<pojo>來接收,頁面上input框的name屬性值= vo中接收的集合屬性名稱+[list的下標]+.+list泛型的屬性名稱 -->
<td>
<input type="checkbox" name="ids" value="${item.id }"/>
<input type="hidden" name="itemsList[${status.index }].id" value="${item.id }"/>
</td>
<td><input type="text" name="itemsList[${status.index }].name" value="${item.name }"/></td>
<td><input type="text" name="itemsList[${status.index }].price" value="${item.price }"/></td>
<td><input type="text" name="itemsList[${status.index }].createtime"
value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
<td><input type="text" name="itemsList[${status.index }].detail" value="${item.detail }"/></td>
<td><a href="${pageContext.request.contextPath }/items/itemEdit/${item.id}">修改</a></td>
</tr>
</c:forEach>
問題1. 解決Post請求亂碼
<!--在web.xml中配置過濾器,爲Spring提供-->
<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>
問題2. 頁面String date 的字符串無法封裝到java.util.Date對象
解決步驟1:自定義轉換器converter
/**
* S - source:源
* T - target:目標
* @author zj
*
*/
public class CustomGlobalStrToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(source);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
解決步驟2,在SpringMvc.xml核心配置文件中配置自定義轉換器
<!-- 加載註解驅動 -->
<mvc:annotation-drivenconversion-service="conversionService"/>
<!-- 轉換器配置 -->
<beanid="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<propertyname="converters">
<set>
<bean class="cn.redrat.controller.converter"/>
</set>
</property>
</bean>
解決步驟3,將自定義轉換器掛載到註解驅動上
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>