本文介绍了具有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 .看到如今,无所不包是一种事实上的标准,自己动手做可能已经过时了.如果当时有无所不包的人,我可能不会写这篇文章

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/zh-CN/html/injectablerefs.html

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

它可能也将包含在Deltaspike 0.4孵化或类似产品中.

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的设置器,它将查找一个名为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.

通常的用例是使用一个String变量,该变量的名称应与所需的参数完全相同.

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包装注入.您改为注入该豆.

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的去污注入请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:14