简体   繁体   English

从Servlet访问ViewScoped ManagedBean

[英]Access ViewScoped ManagedBean from Servlet

Background information: I have a file upload applet in my jsf page. 背景信息:我的jsf页面中有一个文件上传小程序。 This applet expects an adress where it can send it's POST request. 该applet期望可以发送POST请求的地址。 (I can't edit this post request to add more fields or something). (我无法编辑此发布请求以添加更多字段或其他内容)。 The post method of my servlet then stores the file. 然后,我的servlet的post方法存储文件。 This job can't be done by a managed bean because the servlet has to be annotated with @MultiPartConfig and I can't add this annotation to the jsf managed bean. 托管bean无法完成此工作,因为该servlet必须使用@MultiPartConfig进行注释,并且我无法将此注释添加到jsf托管bean中。 In order to force the upload applet to use the same session I added an URL attribute named jsessionId to the post request according to this post . 为了强制上载小程序使用相同的会话,我根据这篇文章在发布请求中添加了一个名为jsessionId的URL属性。 In my servlet I am now able to access session scoped beans. 现在,在我的servlet中,我可以访问会话作用域的bean。

Now I have a ViewScoped bean where I store some form input data which I want to use in the servlet, since adding those inputs to the post request doesn't work (Applet is a third party project (JUploadApplet) and for some reason it doesn't work to add additional form data). 现在,我有了一个ViewScoped bean,在其中存储了一些我想在servlet中使用的表单输入数据,因为将这些输入添加到post请求中不起作用(Applet是第三方项目(JUploadApplet),由于某种原因,它不能无法添加其他表单数据)。 Now is it possible to access the ViewScoped bean from within the servlet ? 现在可以从servlet中访问ViewScoped bean了吗? If I change the scope into SessionScope I am able to process the input but with ViewScoped I get a NullPointerException if I try to access the bean like this : UploadBean uploadBean = (UploadBean)request.getSession().getAttribute("uploadBean"); 如果将范围更改为SessionScope,则可以处理输入,但是如果尝试像这样访问Bean,则使用ViewScoped会收到NullPointerException:UploadBean uploadBean =(UploadBean)request.getSession()。getAttribute(“ uploadBean”);

This is not possible. 这是不可能的。 Your best bet is to let the view scoped bean generate an unique key, store itself in the session scope by that key and pass that key as additional parameter to the applet and finally let the servlet access the session attribute by that key. 最好的选择是让视图作用域的bean生成一个唯一的键,通过该键将自身存储在会话范围中,然后将该键作为附加参数传递给applet,最后让servlet通过该键访问会话属性。

Eg 例如

private String sessionKey;

@PostConstruct
public void init() {
    sessionKey = UUID.randomUUID().toString();
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(sessionKey, this);
}

@PreDestroy
public void destroy() {
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(sessionKey);
}

Let the applet pass the sessionKey as request parameter to the servlet, so that the servlet can do 让小程序将sessionKey作为请求参数传递给servlet,以便servlet可以执行

String sessionKey = request.getParameter("sessionKey");
Bean bean = (Bean) request.getSession().getAttribute(sessionKey);
// ...

Note that instead of the bean itself, you can also just store an arbitrary bean/valueobject/etc. 注意,除了bean本身,您还可以只存储任意bean / valueobject / etc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM