Search in sources :

Example 1 with FacesContext

use of jakarta.faces.context.FacesContext in project guide-jpa-intro by OpenLiberty.

the class EventBean method addErrorMessage.

/**
 * Adds "Choose a valid time" message after selectOptions in user interface.
 */
private void addErrorMessage(ComponentSystemEvent event, String errorMessage) {
    FacesContext context = FacesContext.getCurrentInstance();
    FacesMessage message = new FacesMessage(errorMessage);
    HtmlPanelGroup divEventTime = (HtmlPanelGroup) event.getComponent().findComponent("eventform:eventTime");
    context.addMessage(divEventTime.getClientId(context), message);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) HtmlPanelGroup(jakarta.faces.component.html.HtmlPanelGroup) FacesMessage(jakarta.faces.application.FacesMessage)

Example 2 with FacesContext

use of jakarta.faces.context.FacesContext in project rubia-forums by flashboss.

the class JSFUtil method getBundleMessage.

/**
 * @param bundleName the name of the bundle
 * @param messageKey the key of the bundle to find
 *
 * @return the value for the requested id
 */
public static String getBundleMessage(String bundleName, String messageKey) {
    String bundleMessage = null;
    // Getting ResourceBundle with current Locale
    FacesContext ctx = getCurrentInstance();
    UIViewRoot uiRoot = ctx.getViewRoot();
    Locale locale = uiRoot.getLocale();
    ClassLoader ldr = currentThread().getContextClassLoader();
    ResourceBundle bundle = getBundle(bundleName, locale, ldr);
    bundleMessage = bundle.getString(messageKey);
    return bundleMessage;
}
Also used : Locale(java.util.Locale) FacesContext(jakarta.faces.context.FacesContext) ResourceBundle(java.util.ResourceBundle) UIViewRoot(jakarta.faces.component.UIViewRoot)

Example 3 with FacesContext

use of jakarta.faces.context.FacesContext in project rubia-forums by flashboss.

the class PollValidator method validate.

public void validate(FacesContext context, UIComponent component, String value) throws ValidatorException {
    // A check whether it is post submition action. If not validators are
    // not executed.
    FacesContext fc = FacesContext.getCurrentInstance();
    Map<String, String> reqParams = fc.getExternalContext().getRequestParameterMap();
    if (!reqParams.keySet().contains("post:Submit")) {
        return;
    }
    // Collecting all poll data from the form.
    UIComponent formComp = component.getParent();
    UIComponent questionComp = formComp.findComponent("question");
    UIComponent pollDurationComp = formComp.findComponent("pollDuration");
    List<UIComponent> options = new ArrayList<UIComponent>();
    for (int i = 1; ; i++) {
        UIComponent temp = formComp.findComponent("option_" + i);
        if (temp != null) {
            options.add(temp);
        } else {
            break;
        }
    }
    // validate.
    if (!((questionComp.getAttributes().get("value") != null && questionComp.getAttributes().get("value").toString().trim().length() != 0) || (options.size() > 0))) {
        return;
    }
    // Checks
    if (options.size() > 10) {
        throwValidationException(TOO_MANY_OPTIONS_MSG);
    }
    if (options.size() < 2) {
        throwValidationException(TOO_FEW_OPTIONS_MSG);
    }
    if (questionComp.getAttributes().get("value") == null || questionComp.getAttributes().get("value").toString().trim().length() == 0) {
        throwValidationException(EMPTY_POLL_QUESTION_MSG);
    }
    for (UIComponent option : options) {
        if (option.getAttributes().get("value") == null || option.getAttributes().get("value").toString().trim().length() == 0) {
            throwValidationException(EMPTY_POLL_OPTION_MSG);
        }
    }
    int duration = 0;
    if (pollDurationComp.getAttributes().get("value") != null && pollDurationComp.getAttributes().get("value").toString().trim().length() != 0) {
        try {
            duration = parseInt(pollDurationComp.getAttributes().get("value").toString());
        } catch (NumberFormatException e) {
            throwValidationException(POLL_DURATION_MSG);
        }
        if (duration < 0) {
            throwValidationException(POLL_DURATION_MSG);
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList)

Example 4 with FacesContext

use of jakarta.faces.context.FacesContext in project core by weld.

the class WeldTestPhaseListener method testELResolver.

private void testELResolver(String name) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELResolver resolver = facesContext.getApplication().getELResolver();
    ELContext elContext = facesContext.getELContext();
    Object object = resolver.getValue(elContext, null, name);
    if (object == null)
        throw new NullPointerException("ELResolver returned null");
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ELResolver(jakarta.el.ELResolver) ELContext(jakarta.el.ELContext)

Example 5 with FacesContext

use of jakarta.faces.context.FacesContext in project core by weld.

the class ConversationPropagationFilter method wrapResponse.

private ServletResponse wrapResponse(HttpServletResponse response, final String requestPath) {
    return new HttpServletResponseWrapper(response) {

        @Override
        public void sendRedirect(String path) throws IOException {
            FacesContext context = FacesContext.getCurrentInstance();
            if (context != null) {
                // this is a JSF request
                ConversationContext conversationContext = instance(contextId).select(HttpConversationContext.class).get();
                if (conversationContext.isActive()) {
                    Conversation conversation = conversationContext.getCurrentConversation();
                    if (!conversation.isTransient()) {
                        path = new FacesUrlTransformer(path, context).toRedirectViewId().toActionUrl().appendConversationIdIfNecessary(conversationContext.getParameterName(), conversation.getId()).encode();
                    }
                }
            }
            super.sendRedirect(path);
        }
    };
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FacesUrlTransformer(org.jboss.weld.module.jsf.FacesUrlTransformer) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Conversation(jakarta.enterprise.context.Conversation) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext) ConversationContext(org.jboss.weld.context.ConversationContext) HttpConversationContext(org.jboss.weld.context.http.HttpConversationContext)

Aggregations

FacesContext (jakarta.faces.context.FacesContext)821 PrintWriter (java.io.PrintWriter)236 UIComponent (jakarta.faces.component.UIComponent)137 UIViewRoot (jakarta.faces.component.UIViewRoot)135 IOException (java.io.IOException)91 Application (jakarta.faces.application.Application)86 ExternalContext (jakarta.faces.context.ExternalContext)72 UIInput (jakarta.faces.component.UIInput)69 ServletException (jakarta.servlet.ServletException)69 Test (org.junit.Test)67 HashMap (java.util.HashMap)65 ArrayList (java.util.ArrayList)54 FacesException (jakarta.faces.FacesException)50 ValueExpression (jakarta.el.ValueExpression)48 ViewDeclarationLanguage (jakarta.faces.view.ViewDeclarationLanguage)43 Map (java.util.Map)43 UIPanel (jakarta.faces.component.UIPanel)41 ELContext (jakarta.el.ELContext)36 Locale (java.util.Locale)32 List (java.util.List)31