我正在使用spring MVC创建一个项目。 我正在使用jsr303验证字段。 它工作正常它显示控制台中的字段错误,但不显示在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;
}
我的验证器类是
package com.nativeunlock.Validator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.nativeunlock.dto.CampaignEntity;
@Component
public class CampaignValidator implements Validator{
@Override
public boolean supports(Class c) {
return CampaignEntity.class.isAssignableFrom(c);
}
@Override
public void validate(Object command, Errors errors) {
CampaignEntity campaignBean = (CampaignEntity)command;
/*if(!regBean.getPassword().equals(regBean.getRePassword()))
errors.rejectValue("rePassword","password.notmatch");*/
}
}
答案 0 :(得分:1)
问题在于,通过添加模型属性model.addAttribute("Campaign", campaignBean);
,您可以从模型中删除绑定错误。
绑定错误绑定到经过验证的实例。如果将断点设置为方法内的第一行并检查模型,则会看到带有键Campaign
的已验证对象,但也会看到带有键org.springframework.validation.BindingResult.Campaign
的绑定错误对象
当您进一步移动时,并且在“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;键,请注意绑定错误将从模型中删除,因为它们绑定的实例不再存在,这就是您在JSP中看不到它们的原因。
删除model.addAttribute("Campaign", campaignBean);
或更改密钥,您应该会看到绑定错误。
也不是说您不需要手动调用validator.validate(campaignObj, result);
@Valid 会为您调用验证