티스토리 뷰
Programming/JSF
Solution for getValue() fails with NullPointerException when calling FacesContext.getViewRoot().findComponent()
Jared 2008. 6. 19. 22:07In case of validator for multiple fields, you have to use a f:attribute facet(s) to attach the validator to the last component of the group (components are rendered, validated, converted and updated in the same order as you define them in the JSF view) and pass the client ID of the other component(s).
In some of cases, you may confused to use an attribute field(s) in f:attribute value. For instance, the case that you have a form object in rich:modalPanel from RichFaces, you have to navigate the component tree from rich:modalPanel not form. In this case, it can be passing over easily. So you have to check the conceptual component tree or path to the component before you use it. And I'll introduce the methods for this.
JSF components are managed using a conceptual tree data structure which is one of the core elements of the framework. Some third party frameworks(e.g. Sun RI, myFaces) for JSF are support the printstream to print the tree. But if you use a framwork that does not support a print method for tree, you can use getClientId method from UIComponent to get the component name or consider using the Faces Trace to find a component tree from JSF pages.
For the first case, the getClientId method return a client-side identifier for the component, generating one if necessary.
To get the client-side identifier:
PasswordValidator.java code:
For the second case, you can get the Faces Trace library from here or check an attached facestrace-0.9.0.jar file.
In some of cases, you may confused to use an attribute field(s) in f:attribute value. For instance, the case that you have a form object in rich:modalPanel from RichFaces, you have to navigate the component tree from rich:modalPanel not form. In this case, it can be passing over easily. So you have to check the conceptual component tree or path to the component before you use it. And I'll introduce the methods for this.
JSF components are managed using a conceptual tree data structure which is one of the core elements of the framework. Some third party frameworks(e.g. Sun RI, myFaces) for JSF are support the printstream to print the tree. But if you use a framwork that does not support a print method for tree, you can use getClientId method from UIComponent to get the component name or consider using the Faces Trace to find a component tree from JSF pages.
For the first case, the getClientId method return a client-side identifier for the component, generating one if necessary.
To get the client-side identifier:
- Just set f:validator for authuserpassword as follows.
<f:validator validatorId="realm.PasswordValidator"/> - Print out the client-side identifier in PasswordValidator by followed code.
System.out.println("uiComponent.getClientId(facesContext)" + uiComponent.getClientId(facesContext)); - Remove f:validator from authuserpassword and add f:validator and f:attribute for authuserpassword in confirmpassword as follows.
<f:validator validatorId="realm.PasswordValidator"/>
<f:attribute name="passwordId" value="CLIENT_ID_FROM_2th_STEP"/> - Change the PasswordValidator.java as follows.
<h:inputSecret id="authuserpassword" label="Auth user password"
value="#{AddInfoBean.authUserPassword}"
styleClass="rsInput" redisplay="true" required="true">
<f:validateLength minimum="6" maximum="250"/>
<f:validator validatorId="realm.NoSpaceValidator"/>
</h:inputSecret>
<h:outputText value="Confirm password:" styleClass="rsLabel"/>
<h:inputSecret id="confirmpassword" label="Confirm password" value="#{AddInfoBean.confirmPassword}"
styleClass="rsRealmInput" redisplay="true" required="true">
<f:validateLength minimum="6" maximum="250"/>
<f:validator validatorId="realm.NoSpaceValidator"/>
<f:validator validatorId="realm.PasswordValidator"/>
<f:attribute name="passwordId" value="addModal:addFrm2:authuserpassword"/>
</h:inputSecret>
value="#{AddInfoBean.authUserPassword}"
styleClass="rsInput" redisplay="true" required="true">
<f:validateLength minimum="6" maximum="250"/>
<f:validator validatorId="realm.NoSpaceValidator"/>
</h:inputSecret>
<h:outputText value="Confirm password:" styleClass="rsLabel"/>
<h:inputSecret id="confirmpassword" label="Confirm password" value="#{AddInfoBean.confirmPassword}"
styleClass="rsRealmInput" redisplay="true" required="true">
<f:validateLength minimum="6" maximum="250"/>
<f:validator validatorId="realm.NoSpaceValidator"/>
<f:validator validatorId="realm.PasswordValidator"/>
<f:attribute name="passwordId" value="addModal:addFrm2:authuserpassword"/>
</h:inputSecret>
PasswordValidator.java code:
/**
* File Name: PasswordValidator.java
* Version : 1.00
* Date : 2008. 6. 19
* Author : Jared
* Purpose :
*----------------------------------------------------------------------
* Copyright: Copyright 2001-2008 RealNetworks, Inc.
2601 Elliott Ave, Seattle WA 98121. USA.
This software is the confidential and proprietary
information of RealNetworks, Inc.
You shall not disclose such Confidential Information
and shall use it only in accordance with the terms of
the license agreement you entered into with RealNetworks.
*/
package com.real.mms.realmeditor.validator;
import com.sun.faces.util.MessageFactory;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.application.FacesMessage;
/**
* PasswordValidator CLASS
* <p/>
* This represent PasswordValidator object.
*
* @author Jared Bark (jbark@real.com)
* @version 1.0
* @since JDK1.5
*/
public class PasswordValidator implements Validator {
public PasswordValidator() {
}
public void validate(FacesContext facesContext, UIComponent uiComponent, Object val) throws ValidatorException {
String passwordId;
String password = null;
String label;
String summary;
String message;
if(val != null) {
label = (String) MessageFactory.getLabel(facesContext, uiComponent);
if (!(val instanceof String)) {
throw new IllegalArgumentException(label + ": must be a String.");
}
passwordId = (String) uiComponent.getAttributes().get("passwordId");
password = (String) ((UIInput) facesContext.getViewRoot().findComponent(passwordId)).getValue();
if(val == null || password == null || !val.equals(password)) {
summary = label + ": confirmation password does not match.";
message = label + ": confirmation password does not match.";
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, message));
}
}
}
}
* File Name: PasswordValidator.java
* Version : 1.00
* Date : 2008. 6. 19
* Author : Jared
* Purpose :
*----------------------------------------------------------------------
* Copyright: Copyright 2001-2008 RealNetworks, Inc.
2601 Elliott Ave, Seattle WA 98121. USA.
This software is the confidential and proprietary
information of RealNetworks, Inc.
You shall not disclose such Confidential Information
and shall use it only in accordance with the terms of
the license agreement you entered into with RealNetworks.
*/
package com.real.mms.realmeditor.validator;
import com.sun.faces.util.MessageFactory;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.application.FacesMessage;
/**
* PasswordValidator CLASS
* <p/>
* This represent PasswordValidator object.
*
* @author Jared Bark (jbark@real.com)
* @version 1.0
* @since JDK1.5
*/
public class PasswordValidator implements Validator {
public PasswordValidator() {
}
public void validate(FacesContext facesContext, UIComponent uiComponent, Object val) throws ValidatorException {
String passwordId;
String password = null;
String label;
String summary;
String message;
if(val != null) {
label = (String) MessageFactory.getLabel(facesContext, uiComponent);
if (!(val instanceof String)) {
throw new IllegalArgumentException(label + ": must be a String.");
}
passwordId = (String) uiComponent.getAttributes().get("passwordId");
password = (String) ((UIInput) facesContext.getViewRoot().findComponent(passwordId)).getValue();
if(val == null || password == null || !val.equals(password)) {
summary = label + ": confirmation password does not match.";
message = label + ": confirmation password does not match.";
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, message));
}
}
}
}
For the second case, you can get the Faces Trace library from here or check an attached facestrace-0.9.0.jar file.
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- spring
- SBM
- Shell
- Dependency
- Oracle
- bouncycastle
- bash
- spring boot
- log4j
- install
- Java
- Maven
- jboss
- Kubernetes
- JSF
- monitoring
- Tomcat
- EMV
- ubuntu
- dump
- Jose
- OOP
- Guava
- svn
- ssh
- docker
- Heap
- zookeeper
- SMPP
- nodejs
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함