[英]Inject @ViewScope cdi bean into JAX-RS resource
I'm using MyFaces Apache 2.0.3 JSF, WAS 8.0.0.10 我正在使用MyFaces Apache 2.0.3 JSF,WAS 8.0.0.10
Currently I'm trying to inject an @ViewScope
cdi bean, @ViewScope
in JSF 2.0 provided by OmniFaces 's library. 当前,我正在尝试注入
@ViewScope
bean,即@ViewScope
库提供的JSF 2.0中的@ViewScope 。 But I get an error: WebBeans context with scope type annotation @ViewScoped does not exist within current thread
. 但是我收到一个错误:
WebBeans context with scope type annotation @ViewScoped does not exist within current thread
。 When I try to inject an @SessionScope
cdi bean, everything works fine. 当我尝试注入
@SessionScope
cdi bean时,一切正常。
Code of my JAX-RS resource: 我的JAX-RS资源的代码:
@RequestScoped
@Path("/events")
public class CalendarResource implements Serializable {
@Inject
private CalendarBean calendarBean;
@Inject
private PropertiesBean propertiesBean;
@GET
@Produces("text/plain; charset=utf-8")
public Response getEvents(@QueryParam("calendarId") String calendarId,
@QueryParam("start") String start,
@QueryParam("end") String end,
@Context SecurityContext securityContext,
@Context HttpServletRequest req
) {
FullCalendar selectedCalendar = calendarBean.getFullCalendar();
System.out.println(calendarId + " " + start + " " + end + " " + propertiesBean.getUser().getName());
return null;
}
Code of my CDI bean: 我的CDI bean的代码:
@Named
@ViewScoped
public class CalendarBean implements Serializable {
@EJB
private CalendarEJB calendarEJB;
@Inject
private PropertiesBean propertiesBean;
@PostConstruct
public void init(){
...
}
...
}
What am I doing wrong? 我究竟做错了什么? As soon as I know, I can inject broader scope in a narrow scope.
据我所知,我可以在狭窄的范围内注入更大的范围。 Thank you.
谢谢。
UPDATE : when I changed @ViewScope
to @SessionScoped
, everything began to work. 更新 :当我将
@ViewScope
更改为@SessionScoped
,一切开始起作用。 May it be issue with OmniFaces ? OmniFaces可能有问题吗?
@ViewScoped
is tied to a JSF view, however durig a JAX-RS request, there's no means of a JSF view anywhere. @ViewScoped
绑定到JSF视图,但是,由于拖延了JAX-RS请求,因此无法在任何地方使用JSF视图。 There's no JSF view being restored during a JAX-RS request as the JAX-RS request is not initiated by submit of a JSF <h:form>
which holds information about the JSF view state and would trigger the FacesServlet
to restore the view. 在JAX-RS请求期间没有还原JSF视图,因为JAX-RS请求不是通过提交JSF
<h:form>
来发起的,该JSF <h:form>
保存有关JSF视图状态的信息,并会触发FacesServlet
还原视图。
@SessionScoped
will indeed work if the JAX-RS request is fired in the same HTTP session as the JSF page. 如果在与JSF页面相同的HTTP会话中触发JAX-RS请求,则
@SessionScoped
确实可以工作。 However I can understand that this scope is too broad. 但是我可以理解,这个范围太广了。 You might want to pass an additional request parameter identifying the current JSF view and get hold a mapping of it in the session scoped bean (which is basically how the JSF view scope works under the covers!).
您可能想要传递一个标识当前JSF视图的附加请求参数,并在会话范围的bean中保留它的映射(这基本上就是JSF视图范围在幕后的工作方式!)。
@ConversationScoped
should work if you pass along the cid
parameter to the JAX-RS request. 如果将
cid
参数传递给JAX-RS请求,则@ConversationScoped
应该可以工作。
This is not an OmniFaces problem. 这不是OmniFaces问题。 You'd face exactly the same problem with JSF 2.2
@ViewScoped
due to the nature of it being tied to a JSF view. 由于JSF 2.2
@ViewScoped
的性质与JSF视图绑定在一起,因此您将面临完全相同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.