仅在第一个周期评估验证器的 JSF2 禁用属性?
我发现,如果我的托管 bean 是 ViewScoped,那么我在 JSF2 中使用的任何验证器的禁用属性仅在第一个周期中进行评估。
但我想根据我从第四个更新阶段获得的数据为我的验证器使用 disabled 属性。因此,我希望它在同一视图上执行的所有周期中重新评估。
示例xhtml页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
<h:form>
<h:panelGrid id="pnlGrid">
<h:inputText id="someValueId" value="#{testPageBean.someValue}">
<f:validateLength minimum="2" maximum="4"
disabled="#{testPageBean.disableValidateLength}"/>
</h:inputText>
<h:messages for="someValueId"/>
<h:commandButton action="#{testPageBean.doSomething}"
value="Do something" />
</h:panelGrid>
</h:form>
</h:body>
</html>
及其视图范围托管bean
package cz.kamosh;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TestPageBean implements Serializable {
boolean disableValidateLength = false;
private String someValue;
public TestPageBean() {
System.out.println("Constructor TestPageBean");
}
public String getSomeValue() {
return someValue;
}
public void setSomeValue(String someValue) {
this.someValue = someValue;
}
/**
* Some action without any navigation as a result
*/
public void doSomething() {
disableValidateLength = !disableValidateLength;
System.out.printf(
"DoSomething, someValue: %1$s, disableValidateLength: %2$b\n",
someValue, disableValidateLength);
}
public boolean isDisableValidateLength() {
System.out.printf("IsValidateLength, disableValidateLength: %1$b\n",
disableValidateLength);
return disableValidateLength;
}
}
我知道我应该责怪实现com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl:
private void applyNested(FaceletContext ctx,
UIComponent parent) {
// only process if it's been created
if (!ComponentHandler.isNew(parent)) {
return;
}
...
}
这三行导致验证器当我执行操作时,没有重新评估其 disabled 属性:-(
请给我一个提示,JSF2 人员实现这种方式的动机是什么,或者更好地如何解决我的问题问题?
编辑:
JSF2 版本:2.0.3-SNAPSHOT
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JSF2Testing</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
</web-app>
EDIT2尝试遵循 BalusC'建议
页面更改:
<h:inputText id="someValueId" value="#{testPageBean.someValue}">
<f:validator validatorId="myLengthValidator"/>
<f:attribute name="disableMyLengthValidator" value="#{testPageBean.disableValidateLength}"/>
<f:attribute name="minimum" value="2" />
<f:attribute name="maximum" value="4" />
</h:inputText>
使用自己的验证器而不是标准:
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.LengthValidator;
import javax.faces.validator.ValidatorException;
@FacesValidator("myLengthValidator")
public class MyLengthValidator extends LengthValidator implements Serializable {
public MyLengthValidator() {
System.out.println("MyLengthValidator constructor");
}
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if((Boolean)component.getAttributes().get("disableMyLengthValidator")) {
return;
}
setMinimum(Integer.valueOf((String)component.getAttributes().get("minimum")));
setMaximum(Integer.valueOf((String)component.getAttributes().get("maximum")));
super.validate(context, component, value);
}
}
我对此解决方案的注释:
- 需要自己的实现,即使标准验证器
- 依赖于组件上的属性(inputText) 级别
- 验证器被触发,即使它不应该被触发
- 我很确定当为验证器提供客户端验证时它不会像我们预期的那样运行
- 我认为它非常丑陋并且不遵循 JSF2 :-(
I found that disabled attribute for any validator I use in JSF2 is evaluated only in the first cycle if my managed bean is ViewScoped.
But I would like to make use of disabled attribute for my validators based on data I get from 4th update phase. So I would expect it reevaluated in all cycles performed on the same view.
Example xhtml page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head></h:head>
<h:body>
<h:form>
<h:panelGrid id="pnlGrid">
<h:inputText id="someValueId" value="#{testPageBean.someValue}">
<f:validateLength minimum="2" maximum="4"
disabled="#{testPageBean.disableValidateLength}"/>
</h:inputText>
<h:messages for="someValueId"/>
<h:commandButton action="#{testPageBean.doSomething}"
value="Do something" />
</h:panelGrid>
</h:form>
</h:body>
</html>
and its view scoped managed bean
package cz.kamosh;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TestPageBean implements Serializable {
boolean disableValidateLength = false;
private String someValue;
public TestPageBean() {
System.out.println("Constructor TestPageBean");
}
public String getSomeValue() {
return someValue;
}
public void setSomeValue(String someValue) {
this.someValue = someValue;
}
/**
* Some action without any navigation as a result
*/
public void doSomething() {
disableValidateLength = !disableValidateLength;
System.out.printf(
"DoSomething, someValue: %1$s, disableValidateLength: %2$b\n",
someValue, disableValidateLength);
}
public boolean isDisableValidateLength() {
System.out.printf("IsValidateLength, disableValidateLength: %1$b\n",
disableValidateLength);
return disableValidateLength;
}
}
I know that I should blame implementation com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl:
private void applyNested(FaceletContext ctx,
UIComponent parent) {
// only process if it's been created
if (!ComponentHandler.isNew(parent)) {
return;
}
...
}
These three rows cause validator not being reevaluated its disabled attribute when I perform action:-(
Could some please give me a hint, what was the motivation for JSF2 guys to implement this way or even better how to solve my problem?
EDIT:
Version of JSF2: 2.0.3-SNAPSHOT
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JSF2Testing</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
</web-app>
EDIT2 Attempt to follow BalusC' suggestion
Changes on page:
<h:inputText id="someValueId" value="#{testPageBean.someValue}">
<f:validator validatorId="myLengthValidator"/>
<f:attribute name="disableMyLengthValidator" value="#{testPageBean.disableValidateLength}"/>
<f:attribute name="minimum" value="2" />
<f:attribute name="maximum" value="4" />
</h:inputText>
Own validator used instead of standard :
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.LengthValidator;
import javax.faces.validator.ValidatorException;
@FacesValidator("myLengthValidator")
public class MyLengthValidator extends LengthValidator implements Serializable {
public MyLengthValidator() {
System.out.println("MyLengthValidator constructor");
}
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if((Boolean)component.getAttributes().get("disableMyLengthValidator")) {
return;
}
setMinimum(Integer.valueOf((String)component.getAttributes().get("minimum")));
setMaximum(Integer.valueOf((String)component.getAttributes().get("maximum")));
super.validate(context, component, value);
}
}
My notes about this solution:
- requires own implementation even if for standard validators
- is dependent on attributes on component (inputText) level
- validator is triggered even if it should not be
- I am pretty sure it will not behave as we expect when client side validation is provided for validator
- I consider is as very ugly and not following JSF2 :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已成为先有鸡还是先有蛋问题的另一个受害者,部分状态保存和查看作用域 bean,如 问题 1492 将在即将发布的 Mojarra 2.2 中修复。
当您看到视图作用域 bean 的构造函数在每个请求上调用而不是在视图的第一个请求时调用一次时,您还应该立即意识到有些事情并不完全正确。
解决此问题的方法之一是完全禁用部分状态保存:
另请参阅:
@ViewScoped
在标记处理程序中失败You've become another victim of the chicken-egg issue with partial state saving and view scoped beans as described in issue 1492 which is to be fixed in the upcoming Mojarra 2.2.
You should also immediately have realized that something's not entirely right when you saw the constructor of a view scoped bean being invoked on every request instead of only once at the first request of a view.
One of the ways to fix it is to disable the partial state saving altogether:
See also:
@ViewScoped
fails in tag handlers