在JSF 2.0中注入Bean [英] Injecting Beans in JSF 2.0
问题描述
我有一个Session作用域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
和@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!
推荐答案
您不能将使用javax.faces.bean.SessionScoped
用于JSF和import javax.inject.Named
用于CDI的这两个软件包中的注释混合在一起.两者都反映了不同的注入机制,因此不能在同一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屋!