vit_sin
vit_sin

Reputation: 23

how to pass attribute by value to backend bean

if "f:attribute" is a simple string and pass like: <f:attribute name="selectedUserEmail" value="TADA" /> than all works fine, TADA is passed correctly. But when its passed like #{manageUserView.selectedUser.email} - than its null.

<ui:composition ...
    <ui:define name="metadata">
        <f:metadata>
            <f:viewAction action="#{manageUserView.init}" />
        </f:metadata>
    </ui:define>

    <ui:define name="content">
        <div class="col-12">
            <div class="card">

<!-- indentations are broken on purpose so code will fit the page -->
<h:form id="editUserForm">
    <p:accordionPanel> ...
        <p:tab>
            <div class="field grid">
                <p:outputLabel for="emailInput" value="Email" class="col-12 mb-2 md:col-2 md:mb-0"/>
                <div class="col">
                    <p:inputText id="emailInput" 
                        value="#{manageUserView.selectedUser.email}"
                        maxlength="100"
                        required="true"
                        requiredMessage="Please enter value"
                        validator="#{emailValidator.validate}"/>
                    <p:message for="emailInput"/>
                </div>
            </div>
        
            <div class="field grid">
                <p:outputLabel for="pwd1" value="Password" class="col-12 mb-2 md:col-2 md:mb-0"/>
                <div class="col">
                    <p:password id="pwd1" 
                        value="#{manageUserView.selectedUser.password}"
                        validator="#{passwordValidator.validate}"
                        required="true" 
                        requiredMessage="Please enter value">
                            <f:attribute name="selectedUserEmail" value="#{manageUserView.selectedUser.email}" />
                    </p:password>
                    <p:message for="pwd1"/>
                </div>
            </div>
        ...

            <p:commandButton id="saveButton"
                action="#{manageUserView.save()}"/>
        </p:tab>
    </p:accordionPanel> 
</h:form>
            </div>
        </div>
    </ui:define>
import org.omnifaces.cdi.ViewScoped;
@Component("manageUserView")
@ViewScoped
public class ManageUserView implements Serializable {

    ...
    @Getter @Setter private User selectedUser;

    public void init() {
        selectedUser = new User();
    }

    public void save() {
        // at this point selectedUser is not null, 
        // and values inserted by user, like email, have correct inserted values. 
        // All good.
    }
}
import jakarta.faces.application.FacesMessage;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.validator.Validator;
import jakarta.faces.validator.ValidatorException;
... 
@Service("passwordValidator")
public class PasswordValidatorImpl implements PasswordValidator, Validator<Object>, Serializable {

    ...
    
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
        String selectedUserEmail = (String)component.getAttributes().get("selectedUserEmail");
        
        //selectedUserEmail always null here, why? thanks!
    }
}
@Configuration
public class FacesExceptionFilterConfig {

    @Bean
    FilterRegistrationBean<FacesExceptionFilter> filterRegisBean()
    {
        final FilterRegistrationBean<FacesExceptionFilter> filterRegBean = new FilterRegistrationBean<>();

        filterRegBean.setFilter(new FacesExceptionFilter());
        filterRegBean.addUrlPatterns("/*");
        filterRegBean.setEnabled(Boolean.TRUE);
        return filterRegBean;
    }

    @Bean
    ErrorPageRegistrar errorPageRegistrar()
    {
        return new ErrorPageRegistrar() {
            @Override
            public void registerErrorPages(final ErrorPageRegistry registry) {
                registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/page1"));
                registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/bla-page2"));
                registry.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "... and so on"));
                registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "... and so on"));
                registry.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "... and so on"));
            }
        };
    }
}

public class FacesExceptionHandlerFactory extends ExceptionHandlerFactory {

    public FacesExceptionHandlerFactory(final ExceptionHandlerFactory parent) {
        super(parent);
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        final ExceptionHandler result = getWrapped().getExceptionHandler();
        return new FacesExceptionHandler(result);
    }

}


import org.omnifaces.cdi.ViewScoped;
import org.springframework.stereotype.Component;
@Component
@ViewScoped
public class ManageUserView implements Serializable {...}

import jakarta.faces.event.PhaseEvent;
import jakarta.faces.event.PhaseId;
import jakarta.faces.event.PhaseListener;

@Service
public class ViewLifeCycleListener implements PhaseListener {
    
    @Override
    public void beforePhase(PhaseEvent event) 
    {
        if (event.getPhaseId().getOrdinal() == 1) 
        {
            log.debug("PhaseEvent #1 called");
            ...
        }
    }

    @Override
    public void afterPhase(PhaseEvent event) 
    {
        // RENDER_RESPONSE - last phase of the JSF lifecycle
        if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) 
        {
            log.debug("Finished rendering view for last phase: {}",event.getPhaseId());
            ...
        }
    }

    @Override
    public PhaseId getPhaseId() 
    {
        return PhaseId.ANY_PHASE;
    }
    
    private Optional<Long> getPhaseTimeFromMap(String username) 
    {
        return Optional.ofNullable(userPhaseTimeMap.get(username));
    }   
}

PrimeFaces v12.x, MyFaces(latest), Faces(JSF) v4.x, Omnifaces v4.x

Highly appreciate the help --vs

Upvotes: 0

Views: 88

Answers (0)

Related Questions