我正在使用
验证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;
(...)
}
相关的小脸提取物在下面
<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>
支持bean位于
之下@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,可以查看“could not initialize proxy - no session” with an open session available
我的问题在于如何(或可能在何时)验证验证约束。更具体地说,@NotNull
属性上的String libelle
约束正在按预期工作(消息显示在潜在的其他属性上,不会发生保存,...)
但是,与@Size
themes属性的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为null)。
这一切都让我认为集合上的@Size
约束只能在持续阶段验证,而不是验证阶段。但我不明白为什么会这样。
提前感谢您对该问题的任何领导或帮助。