我试图在我的表单中验证一些值,但是这个验证约束我希望来自类上的一些注释(javax.validation.constraints。*)。我尽我所能,花了几个小时搜索谷歌/ stackoverflow - 但我得到同样的事情,可能是我错过了一些非常简单的东西,但我仍然无法看到它
我使用的是Primefaces 5.3和webspehere8.5(java1.7)
下面是我的Bean,我将收到这些值。
请注意,我尝试了几种方法来实现这项工作,因为我不确定哪一种方法会让人感到震惊:
直接在豆中
@ManagedBean
@SessionScoped
public class MagicBean implements Serializable {
private static final long serialVersionUID = -8724952745982557284L;
SearchForm formValues = new SearchForm();
public void submitSearchForm() {
System.out.println("is Ok");
}
....
@NotNull
@Pattern(regexp = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", message = "This is not a valid email")
private String email;
...
}
这是搜索表单类
public class SearchForm {
...
@NotNull
@Size(min = 3, message = "Name should at least be 3 characters long")
private String name;
...
}
以下是我的网页的样子
<p:growl id="msgs" showDetail="true" />
<p:messages id="msgs2"/>
<h:form id="searchParamsForm">
<p:fieldset widgetVar="searchParamsFormToggle" legend="Search params" toggleable="true" toggleSpeed="500">
<h:panelGrid columns="3" cellpadding="5">
<h:outputText>Name</h:outputText>
<p:inputText id="name" required="true" >#{magicBean.formValues.name} <!-- notice here I use some other class -->
</p:inputText>
<p:message id="errorName" for="name" style="color:red"/>
<h:outputText>Email</h:outputText>
<p:inputText id="email">#{magicBean.email}</p:inputText> <!-- direct from the magic bean -->
<p:message id="errorMail" for="email" style="color:red"/>
<p:commandButton value="Save" ajax="false" icon="ui-icon-check" validateClient="true"/>
<p:commandButton value="Search" update="msgs msgs2 errorMail searchParamsForm" ajax="false" icon="ui-icon-check" validateClient="true" action="#{magicBean.submitSearchForm}" process="searchParamsForm" >
</h:panelGrid>
</p:fieldset>
</h:form>
当然,这里有一些我发现必须设置的参数
<context-param>
<param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.TRANSFORM_METADATA</param-name>
<param-value>true</param-value>
</context-param>