2015-03-03 65 views
2

我正在使用spring MVC創建一個項目。 我正在使用jsr303驗證字段。 它工作正常它顯示控制檯中的字段的錯誤,但不是在jsp頁面 任何人都可以讓我知道我在這裏做什麼錯誤?jsp不顯示彈簧驗證程序錯誤

我控制器的方法是

@RequestMapping(value="/addCampaign", method = RequestMethod.POST) 
    public String processForm(@ModelAttribute(value="Campaign") @Valid CampaignEntity campaignObj,BindingResult result, ModelMap model, Principal principal) { 

     validator.validate(campaignObj, result); 

     if(result.hasErrors()){   

      System.out.println(result.getErrorCount()); 
      System.out.println(result.toString()); 

      CampaignEntity campaignBean = new CampaignEntity(); 

      Map<String,String> agencies = new LinkedHashMap<String,String>(); 
      agencies.put("1", "United Stated"); 
      agencies.put("2", "China"); 
      agencies.put("3", "Singapore"); 
      agencies.put("4", "Malaysia"); 

      model.addAttribute("agencies", agencies); 
      model.addAttribute("publishers", agencies); 
      model.addAttribute("Campaign", campaignBean); 

      return "addCampaign"; 
     }else{ 
      return campaign(model, principal); 
     } 
    } 

在JSP中我使用的標籤

<form:errors path="*" cssClass="error" /> 

我的實體類

package com.nativeunlock.dto; 

import java.io.Serializable; 
import java.util.Date; 

import javax.persistence.*; 
import javax.validation.constraints.Digits; 
import javax.validation.constraints.NotNull; 

import org.hibernate.validator.constraints.NotEmpty; 

import lombok.Data; 
import lombok.Getter; 
import lombok.Setter; 


@SuppressWarnings("serial") 
@NamedQueries({ 
    @NamedQuery(
      name = CampaignEntity.GET_CAMPAIGNS_QUERY, 
      query = "from CampaignEntity campaign" 
    ), 
    @NamedQuery(
      name = CampaignEntity.DELETE_CAMPAIGNS_QUERY, 
      query = "DELETE FROM CampaignEntity campaign WHERE campaign.campaign_id = :campaign_id" 
    )  
}) 

@Entity 
@Table(name = "campaign") 
public class CampaignEntity implements Serializable { 

    public static final String GET_CAMPAIGNS_QUERY ="getCampaignList"; 
    public static final String DELETE_CAMPAIGNS_QUERY ="deleteCampaignList"; 

    @Id 
    @Getter 
    @Setter 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "campaign_id", unique = true, nullable = false) 
    private int campaign_id; 

    @Getter 
    @Setter 
    @Column(name = "name", unique = true) 
    @NotEmpty(message="Name field is mandatory.") 
    private String name; 


    @Getter 
    @Setter 
    @Column(name = "no_of_views") 
    @NotNull(message="No. of Views field is mandatory.") 
    private int no_of_views; 

    @Getter 
    @Setter 
    @Column(name = "video_url") 
    @NotEmpty(message="Video URL field is mandatory.") 
    private String video_url; 

    @Getter 
    @Setter 
    @Column(name = "start_date")  
    @NotEmpty(message="Start Date field is mandatory.") 
    private String start_date; 

    @Getter 
    @Setter 
    @Column(name = "end_date") 
    @NotEmpty(message="End Date field is mandatory.") 
    private String end_date; 

    @Getter 
    @Setter 
    @Column(name = "click_to_play") 
    @NotNull(message="Click to play field is mandatory.") 
    private int click_to_play; 

    @Getter 
    @Setter 
    @Column(name = "frequency") 
    @NotNull(message="Frequency field is mandatory.") 
    private int frequency; 

    @Getter 
    @Setter 
    @Column(name = "priority") 
    @Digits(fraction = 0, integer = 100) 
    @NotNull(message="Priority field is mandatory.") 
    private int priority; 

    @Getter 
    @Setter 
    @Column(name = "divice") 
    @NotNull(message="Divice field is mandatory.") 
    private int divice; 

    @Getter 
    @Setter 
    @Column(name = "operating_system") 
    @NotNull(message="Operating system field is mandatory.") 
    private int operating_system; 

    @Getter 
    @Setter 
    @OneToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name = "publisher_id") 
    @NotEmpty(message="Publisher field is mandatory.") 
    private PublisherEntity publishers; 

    @Getter 
    @Setter 
    @OneToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name = "agency_id") 
    @NotEmpty(message="Agency field is mandatory.") 
    private AgencyEntity agencies; 

} 

我的驗證器類是

示值誤差10
+0

上ErrorsTag設置斷點(其方式和方法從基類繼承。)可能有助於 – 2015-03-03 08:57:13

+0

@JohnDonn :你能告訴我如何設置斷點,控制器出現錯誤,如果你正在使用Eclipse,我已經在控制檯 – 2015-03-03 08:58:50

+0

中打印它們,你應該按下Ctrl + Shift + T並鍵入類的名稱ErrorsTag;如果你有源,你會看到類源打開。如果沒有,你必須找到你的spring-mvc jar,並將它與你從網上下載的源代碼聯繫起來。只要源代碼打開,您可以按照與自己的代碼完全相同的方式設置斷點。 – 2015-03-03 09:02:39

回答

1

問題是,通過添加模型屬性您正在從模型中刪除綁定錯誤。

綁定錯誤綁定到驗證的實例。如果您在方法內的第一行設置了斷點並檢查模型,您將看到一個帶有密鑰Campaign的驗證對象,但也會看到一個帶有密鑰的綁定錯誤對象org.springframework.validation.BindingResult.Campaign

當您進一步移動並達到在'Campaign'鍵下添加一個新實例到模型中,注意綁定錯誤將從模型中移除,因爲它們綁定的實例不再存在,這就是爲什麼你不'不要在你的JSP中看到它們。

刪除或更改密鑰,你應該看到你的綁定錯誤。

而且並不是說你不需要手動調用validator.validate(campaignObj, result);@Valid將調用驗證你