Image not getting updated

橙三吉。 提交于 2019-12-02 06:52:08
BalusC

Your problem is caused by a combination of those 2 causes:

  1. The bean is request scoped.
  2. The <p:fileUpload> is running in "advanced mode" (with browse, upload and cancel buttons).

A request scoped bean has a lifetime of exactly one HTTP request. Uploading a file using <p:fileUpload> button accounts as one HTTP request. Submitting the form accounts as another HTTP request, with its own brand new instance of the bean. You should have noticed it by user.getImage() being null/empty during em.persist() while doing a simple debug.

In order to get it to work, you need to put the bean in the view scope. This way the bean will live as long as you're interacting with the same view.

@ManagedBean
@ViewScoped
public class UserUpdateBean {}

(or with <managed-bean-scope>view when using the old fashioned XML config approach)

An alternative is to set <p:fileUpload> to "simple mode" so that it comes with only a browse button and get uploaded only when you actually submit the form. This way the uploaded file will be set during the same HTTP request as when the form is submitted.

<p:fileUpload ... mode="simple" />

See also:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!