在 JSF 2.0 中注入 Bean [英] Injecting Beans in JSF 2.0
问题描述
我有一个会话范围的 bean
I have a Session scoped bean
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class SessionBean implements Serializable{
我在一个过滤器中输入对象...
I inyect the object in one Filter...
public class FiltroSeguridad implements Filter{
@Inject
private SessionBean sessionBean;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
sessionBean.setRutaRedirect(httpRequest.getRequestURI());
}
}
但是,我在下一次交互中输入了 SessionBean...
But, I inyect SessionBean in the next interaction...
@Model
public class CuentaUsuarioWebBean implements Serializable{
@Inject
private SessionBean sessionBean;
public void loginUsuario() throws IOException{
sessionBean.getRutaRedirect();
}
}
但是属性 getRutaRedirect()
返回 null
But the property getRutaRedirect()
returns null
我通过 CDI 注释更改了导入,它仍然不起作用(javax.enterprise.context.SessionScoped
),与 JSF 注释(javax.faces.bean.ManagedBean
)相同code> 和 @ManagedProperty
).
I change the import by CDI annotations it still doesn't work (javax.enterprise.context.SessionScoped
), same with JSF annotation (javax.faces.bean.ManagedBean
and @ManagedProperty
).
谢谢.
PD:对不起我的英语!
PD: Sorry for my English!
推荐答案
对于 JSF,您不能混合使用 javax.faces.bean.SessionScoped
和 的这两个包中的注释代码>为 CDI 导入 javax.inject.Named
.两者都反映了不同的注入机制,不能在同一个 bean 上混合使用.您必须从同一个包中选择两个注释(用于注入和 Bean 范围).使用相应包中的以下集合
You can't mix annotations from those two packages you're using javax.faces.bean.SessionScoped
for JSF, and import javax.inject.Named
for CDI. Both reflect different injection mechanisms and cannot be mixed on the same bean. You have to pick both annotations(for Injection and Bean Scoping) from the same package. Use the following sets from their corresponding packages
对于基于 CDI 的 bean 定义
For CDI-based bean definitions
javax.enterprise.context.SessionScoped //for bean scoping
javax.inject.Named //for bean declaration
javax.inject.Inject //for injection
对于基于 JSF 的 bean 定义
For JSF-based bean definitions
javax.faces.bean.SessionScoped //for bean scoping
javax.faces.bean.ManagedBean //for bean declaration
javax.faces.bean.ManagedProperty //for bean injection
现在我更了解您的要求,使用以下构造注入 JSF 托管 bean
Now I understand your requirements better, use the following construct to inject a JSF managed bean
@ManagedProperty(value="#{yourBeanName}")
SessionBean yourSessionBean;
另请注意,在 JSF 中,您只能注入比目标范围更广的 bean,例如您可以将 @SessionScoped
bean 注入 @RequestScoped
bean 而不是相反
Also note that within JSF, you can inject only beans of a wider scope than their targets e.g. you can inject a @SessionScoped
bean into a @RequestScoped
bean and not the other way around
但由于 JSF 托管 bean 已被弃用,最好使用 CDI 托管 bean.在这种情况下,您可以在范围更广的 bean 中注入更短范围的 bean
But since JSF managed beans are deprecated, better to use the CDI managed ones. In that case you can inject shorter scoped beans in wider scoped ones
这篇关于在 JSF 2.0 中注入 Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!