Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8784483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:06:08+00:00 2026-06-13T21:06:08+00:00

When using CDI and JSF2 How can a HTTP request parameter be injected into

  • 0

When using CDI and JSF2 How can a HTTP request parameter be injected into a bean?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:06:10+00:00Added an answer on June 13, 2026 at 9:06 pm

    HINT: before reading any further have a look at http://showcase.omnifaces.org/cdi/Param.
    Do it yourself is probably obsolete seeing how omnifaces is a de facto standard today. I would probably not have written this if omnifaces had this at the time

    CDI does not solve specialized problems like injecting a request parameter. That’s supposed to be solved by extensions.

    This is already provided by solder. http://docs.jboss.org/seam/3/solder/latest/reference/en-US/html/injectablerefs.html

    It will probably be included in Deltaspike 0.4-incubating or similar as well.

    That said the code required is rather simple to implement yourself. Example below:

    Annotation to use for the injection point (For example private String myParam;)

    import javax.enterprise.util.Nonbinding;
    import javax.inject.Qualifier;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    
    import static java.lang.annotation.ElementType.*;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;
    
    
    @Qualifier
    @Retention(RUNTIME)
    @Target({METHOD, FIELD, PARAMETER })
    public @interface RequestParam {
        @Nonbinding
        public String value() default "";
    }
    

    Now we have the annotation but we can’t just ask the container to dependency inject a @RequestParam – we obviously need an implementation.

    import javax.enterprise.inject.Produces;
    import javax.enterprise.inject.spi.InjectionPoint;
    import javax.faces.context.FacesContext;
    import javax.inject.Inject;
    
    public class RequestParamProducer implements Serializable {
    
        private static final long serialVersionUID = -4260202951977249652L;
        @Inject
        FacesContext facesContext;
    
        // Producer for @RequestParam
        @Produces
        @RequestParam
        String getRequestParameter(InjectionPoint ip) {
            String name = ip.getAnnotated().getAnnotation(RequestParam.class)
                    .value();
    
            if ("".equals(name))
                name = ip.getMember().getName();
    
            return facesContext.getExternalContext().getRequestParameterMap()
                    .get(name);
        }
    }
    

    So how does it work? Well quite simply it first checks if you did specify what parameter you wanted as in @Requestparam("longAndTerribleFieldNameBestToSpecify");

    If you didn’t it will use the fieldName. So if you annoted a setter called setMyInstance it will look for a parameter called setMyInstance.

    The normal use case would be to have a String variable that is named exactly like the parameter you want.

    Note that we inject FacesContext, that must also be produced. A FacesContext producer could look like this:

    class FacesContextProducer {
    
       @Produces @RequestScoped FacesContext getFacesContext() {
    
          return FacesContext.getCurrentInstance();
    
       }
    
    }
    

    End usage:

    @Inject
    @RequestParam
    private String session_secret;
    

    Note that this will not work for Servlet or similar as it requires access to FacesContext. In those cases one need to wrap the injection with for example a bean that is @RequesScoped. You inject that bean instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I have written a jsf2 (cdi) web-application, using jQuery, a syntaxhighlighting script lib. On
Is it possible to inject EJB 3.1 beans into POJO using CDI on Glassfish
I'm trying to inject a bean defined in a Spring context into a CDI
I'm using CDI (Weld) and I've faced a problem, hope you can help. I
I have a request scoped CDI manage bean(Also tried with jsf managed bean but
I've written a JEE6 application using CDI and JPA. My tests are written in
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
[My Setup: Java EE 6 application, with EJB3.1, CDI/Weld, JSF2 running on Glassfish 3.0.1]
I've been using Seam 2 (also started looking into Java EE 6) in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.