springmvc使用校驗框架validation校驗
1 springmvc校驗(服務端 )
b/s系統中對http請求資料的校驗多數在客戶端進行,這也是出於簡單及使用者體驗性上考慮,但是在一些安全性要求高的系統中服務端校驗是不可缺少的,本節主要學習springmvc實現控制層新增校驗。
Spring3支援JSR-303驗證框架,JSR-303 是JAVA EE 6 中的一項子規範,叫做BeanValidation,官方參考實現是Hibernate Validator(與Hibernate ORM 沒有關係),JSR 303 用於對Java Bean 中的欄位的值進行驗證。
1.1 校驗理解
專案中,通常使用較多是前端的校驗,比如頁面中
服務端校驗:(面試)
控制層conroller:校驗頁面請求的引數的合法性。在服務端控制層conroller校驗,不區分客戶端型別(瀏覽器、手機客戶端、遠端呼叫)
業務層service(使用較多):主要校驗關鍵業務引數,僅限於service介面中使用的引數。
持久層dao:一般是不校驗的。
1.2 springmvc校驗需求
springmvc使用hibernate的校驗框架validation(和hibernate沒有任何關係)。
校驗思路:
頁面提交請求的引數,請求到controller方法中,使用validation進行校驗。如果校驗出錯,將錯誤資訊展示到頁面。
具體需求:
商品修改,新增校驗(校驗商品名稱長度,生產日期的非空校驗),如果校驗出錯,在商品修改頁面顯示錯誤資訊。
1. 配置校驗資訊倒入Jar配置springmvc和注入容器
2. 配置檔案proterties
3. 在pojo類中寫校驗
4. 在controller中接收
5. 傳遞給頁面
1.3 環境準備
hibernate的校驗框架validation所需要jar包:
1.4 配置校驗器
介面卡執行handel 所以將校驗器注入到處理器介面卡中
在sprigmvc.Xml中配置
<--校驗器 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" >
<--校驗器-->
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<--指定校驗使用的資原始檔,如果不指定則預設使用classpath下的ValidationMessages.properties -->
<property name="validationMessageSource" ref="messageSource" />
</bean>
<--校驗錯誤資訊配置文 -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<--資原始檔名-->
<property name="basenames">
<list>
<value>classpath:CustomValidationMessages <value>
</list>
</property>
<--資原始檔編碼格式 -->
<property name="fileEncodings"value="utf-8" />
<!-- 對資原始檔內容快取時間,單位秒 -->
<property name="cacheSeconds"value="120" />
</bean>
1.5 校驗器注入到處理器介面卡中
<mvc:annotation-driven conversion-service="conversionService"
validator="validator"></mvc:annotation-driven>
配置方式2(自學)
<!-- 自定義webBinder -->
<bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator"ref="validator" />
</bean>
<!-- 註解介面卡 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer"ref="customBinder"></property>
</bean>
1.6 在pojo中新增校驗規則
在ItemsCustom.java中新增校驗規則:
public classItems {
privateInteger id;
//校驗名稱在1-30字元中間
//message是錯誤提示驗證出錯的資訊
@Size(min=1,max=30,message="{items.name.length.error}")
private String name;
private Float price;
private String pic;
//非空校驗
@NotNull(message="{items.createtime.isNull}")
private Date createtime;
private String detail;
1.7 CustomValidationMessages.properties
在CustomValidationMessages.properties配置校驗錯誤資訊:
items.name.length.error=請輸入1到30個字元的商品名稱
items.createtime.isNull=請輸入商品的生產日期
1.8 捕獲校驗錯誤資訊
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request,Integer id,@Validated ItemsCustom itemsCustom,BindingResult bindingResult)throws Exception{
//在需要校驗的pojo前邊新增@Validated,在需要校驗的pojo後邊新增BindingResult bindingResult接收校驗出錯資訊
//注意:@Validated和BindingResultbindingResult是配對出現,並且形參順序是固定的(一前一後)。
1.9 在頁面顯示校驗錯誤資訊
在controller中將錯誤資訊傳到頁面即可。
@RequestMapping("/editItemsSubmit")
//如果需要同時穿商品的name和使用者的name 則需要用包裝的pojo
//在需要校驗的pojo前邊新增@Validated在需要校驗的pojo後邊新增BindingResult來接收校驗出錯的資訊
//注意:@Validated和@Validated是配對出現,並且形參是順序是固定的(一前一後)
public String editItemsSubmit(Model model
,HttpServletRequest request,Integer id,@Validated ItemsCustomitemsCustom,BindingResult bindingResult)throws Exception{
//獲取驗證錯誤資訊
if(bindingResult.hasErrors())
{
//輸出錯誤資訊
List<ObjectError> allerrors=bindingResult.getAllErrors();
for(ObjectError error: allerrors)
{
System.out.println(error.getDefaultMessage());
}
//錯誤資訊傳遞到頁面
model.addAttribute("allErrors", allerrors);
return "items/editItems";
}
//呼叫service更新商品資訊,頁面需要將商品資訊傳到此方法
itemsService.updateItems(id, itemsCustom);
//重定向不用加跟路徑
//return"redirect:queryItems.action";
//頁面轉發
return "forward:queryItems.action";
}
頁面顯示錯誤資訊:
<!-- 顯示錯誤資訊 -->
<div style="color:red">
<c:if test="${allErrors!=null}">
<c:forEach items="${allErrors}"var="error">
${error.defaultMessage}</br>
</c:forEach>
</c:if>
</div>
1.10 分組校驗
1.10.1 需求
在pojo中定義校驗規則,而pojo是被多個 controller所共用,當不同的controller方法對同一個pojo進行校驗,但是每個controller方法需要不同的校驗。
解決方法:
定義多個校驗分組(其實是一個java介面),分組中定義有哪些規則
每個controller方法使用不同的校驗分組
1. 定義校驗分組介面
2. 在校驗規則中新增分組
3. 在controller 中使用分組校驗
1.10.2 校驗分組
/**
* 校驗分組
* @author Administrator
*
*/
public interface ValidGrouop1 {
//介面中需要定義任何方法,僅是對不同的校驗規則進行分組
//此分組只校驗上商品名稱長度
}
1.10.3 在校驗規則中新增分組
//校驗名稱在1-30字元中間
//message是錯誤提示驗證出錯的資訊
//groups校驗是屬於哪個分組,group可以定義多個分組
@Size(min=1,max=30,message="{items.name.length.error}",groups={ValidGrouop1.class})
private String name;
1.10.4 在controller方法使用指定分組的校驗
@RequestMapping("/editItemsSubmit")
//如果需要同時穿商品的name和使用者的name 則需要用包裝的pojo
//在需要校驗的pojo前邊新增@Validated在需要校驗的pojo後邊新增BindingResult來接收校驗出錯的資訊
//注意:@Validated和@Validated是配對出現,並且形參是順序是固定的(一前一後)
//ValidGrouop1.class指定使用分組的校驗
public String editItemsSubmit(Model model
,HttpServletRequest request,Integer id,
@Validated(value={ValidGrouop1.class}) ItemsCustomitemsCustom,BindingResult bindingResult)throws Exception{
摘自傳智部落格燕青老師的視訊