使用 CDI 和 JSF2 依赖注入请求参数 [英] Depedency inject request parameter with CDI and JSF2

查看:15
本文介绍了使用 CDI 和 JSF2 依赖注入请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 CDI 和 JSF2 时如何将 HTTP 请求参数注入 bean 中?

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

推荐答案

提示: 在进一步阅读之前,请先查看 http://showcase.omnifaces.org/cdi/Param.自己动手可能已经过时了,因为现在omnifaces 已成为事实上的标准.如果当时omnifaces有这个,我可能不会写这个

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 不能解决像注入请求参数这样的特殊问题.这应该通过扩展来解决.

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

这已经由焊料提供了.http://docs.jboss.org/seam/3/solder/latest/reference/en-US/html/injectablerefs.html

它可能也会包含在 Deltaspike 0.4-incubating 或类似版本中.

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:

注解用于注入点(例如private String myParam;)

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 "";
}

现在我们有了注解,但我们不能只要求容器依赖注入 @RequestParam - 我们显然需要一个实现.

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);
    }
}

那么它是如何工作的呢?那么很简单,它首先检查您是否确实指定了您想要的参数,如 @Requestparam("longAndTerribleFieldNameBestToSpecify");

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

如果没有,它将使用 fieldName.因此,如果您注释了一个名为 setMyInstance 的 setter,它将查找名为 setMyInstance 的参数.

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.

请注意,我们注入了 FacesContext,它也必须生成.FacesContext 生成器可能如下所示:

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();

   }

}

结束使用:

@Inject
@RequestParam
private String session_secret;

请注意,这不适用于 Servlet 或类似产品,因为它需要访问 FacesContext.在这些情况下,需要使用例如 @RequesScoped 的 bean 来包装注入.您改为注入那个 bean.

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.

这篇关于使用 CDI 和 JSF2 依赖注入请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆