我在用
- Myfaces 2.1.12(与 Primefaces 3.5)
- Bean Validation Hibernate Validator 4.3.0.Final
- 龙目岛 1.12.2
- Omnifaces 1.6.2
- 漂亮面孔 3.3.3
- 焊接 2.1.0.CR1
验证 bean(下面的相关摘录)
@Entity
@Data
public class Controle implements Serializable {
private static final String FOREIGN_KEY_ID = "CON_ID";
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Size(max = 256, message = "trop long.")
@Column(nme = "LIB", length = 256)
@NotNull
private String libelle;
@ManyToMany
@JoinTable(
name = "THEME_CONTROLE",
joinColumns = @JoinColumn(name = FOREIGN_KEY_ID),
inverseJoinColumns = @JoinColumn(name = "THECLE")
)
@NotNull(message = "vous devez définir au moins un thème")
@Size(min = 1, message = "vous devez définir au moins un thème")
private List<Theme> themes;
(...)
}
相关的facelet摘录如下
<h:form id="controle">
(...)
<p:messages />
<p:panel>
(...)
<h:panelGrid columns="2" columnClasses="label,">
<p:outputLabel for="libelle" value="Libellé" />
<p:inputText id="libelle" value="#{controle.controle.libelle}" size="60" />
(...)
<p:outputLabel for="themes" value="Thèmes" />
<p:selectManyButton id="themes" value="#{controle.controle.themes}"
collectionType="java.util.ArrayList"
converter="omnifaces.SelectItemsConverter">
<f:selectItems value="#{controle.themes}" var="t" itemLabel="#{t.libelle}" itemValue="#{t}" />
</p:selectManyButton>
(...)
</h:panelGrid>
(...)
<f:facet name="footer">
(...)
<p:commandButton id="saveSubmit" value="#{controle.saveLabel}"
action="#{controle.save}" icon="ui-icon-disk"
styleClass="ui-priority-primary" update=":controle" />
</f:facet>
</p:panel>
(...)
</h:form>
后备豆在下面
@Named(value = "controle")
@ViewScoped
@Data
@Slf4j
@URLMappings(mappings = {
@URLMapping(id = "controle_create", pattern = ControleController.URL_MAPPING_CONTROLE_CREATE, viewId = ControleController.VIEW_ID),
@URLMapping(id = "controle", pattern = ControleController.URL_MAPPING_CONTROLE, viewId = ControleController.VIEW_ID)})
public class ControleController implements Serializable {
(...)
@URLAction
public void load() {
FacesContext.getCurrentInstance().getMessages();
if (controle == null) {
if (id != null) {
controle = controleService.findById(id);
} else {
controle = new Controle();
controle.setAnneeCreation(LocalDate.now().getYear());
}
themes = themeService.getAllThemes();
}
(...)
public String save() {
String destination;
if (controle.getId() == null) {
destination = "pretty:controle_create";
} else {
destination = "pretty:programme";
}
controleService.save(controle);
return destination;
}
(...)
}
如果您对 collectionType 感到疑惑,您可以查看“无法初始化代理 - 无会话”以及可用的打开会话
我的问题在于如何(或何时)验证验证约束。更具体地说,@NotNull
对属性的约束String libelle
按预期工作(消息与潜在的其他人一起显示,不会发生保存,...)
但是,它与主题属性的@Size
约束不同。List<Theme>
没有选择主题(大小为 0)并且所有其他都验证,当我点击保存时似乎什么都没有发生。如果我检查 HTTP 响应 (ajax),我会发现验证异常
<?xml version="1.0" encoding="utf-8"?>
<partial-response>
<error>
<error-name>org.apache.myfaces.view.facelets.el.ContextAwareELException</error-name>
<error-message><![CDATA[
javax.el.ELException: javax.validation.ConstraintViolationException: Validation failed for classes [fr.senat.model.dosleg.Controle] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{
interpolatedMessage='vous devez définir au moins un thème',
propertyPath=themes,
rootBeanClass=class fr.senat.model.dosleg.Controle,
messageTemplate='vous devez définir au moins un thème'
}
]]]></error-message>
</error>
</partial-response>
此外,如果两个约束都未验证,则仅显示第一个(libelle 为空)。
这一切都让我认为@Size
对 Collection 的约束仅在持久阶段而不是验证阶段进行验证。但我不明白为什么会这样。
提前感谢您提供解决该问题的任何线索或帮助。