1

Bean values are not getting removed when I come back to same screen.

  1. I create user details and insert the data
  2. I view all the listed users available in database
  3. I search for particular user
  4. when I come back to Create user page, I see data of searched user

How do I remove old data from bean? Managed bean is in session scope.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
developer
  • 9,116
  • 29
  • 91
  • 150

3 Answers3

2

You need a good reason why you'd want to put a list of users into a session scoped bean. It may cause some trouble and in the end reflect outdated data in a multiuser environment. Make the bean @ViewScoped instead, and load all users, or a subset of users, in a @PostConstruct method.

As to the coherence when using a session scoped bean, my guess is that you didn't update a list of current users. Basically you need to double the operation in this case: the first operation is a database operation, and the second one is a list update operation. Like in the following code:

public void deleteUser() {
    yourEJB.remove(user);
    listOfUsers.remove(user);
}

Also, to have your UI updated, be sure to make a postback, by returning void/null from an action method, or update the needed component via AJAX, by specifying its client id in render attribute of <f:ajax> tag.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
0

I think you're thinking about @Remove annotation, which should be on specific method (or I understand your problem wrong).

Reference: http://docs.oracle.com/javaee/5/api/javax/ejb/Remove.html

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
0

A couple things here: first, post a simple example: it allows us to run it to help you. But here's what is (probably) happening. Your bean is session scoped which means it will live for the entire time the user has a session. Unless you specifically destroy the session change the value it is going to "exist" for that seesion.

Using the JSF2 annotations try this:

@ManagedBean(name="seesionScopedBean")
@SessionScoped
public class SessionScopedBean { private String data;//create getter/setter}

@ManagedBean(name="viewScopedBean")
@ViewScoped
public class ViewScopedBean { private String data;}  

@ManagedBean(name="requestScopedBean")
@RequestScoped
public class RequestScopedBean { private String data;}  

Assuming your three scopes make a page that is basically

<f:view>
<h:form>
<h:inputText value="#{sessionScopedBean.data}" />
<h:inputText value="#{viewScopedBean.data}" />
<h:inputText value="#{requestScopedBean.data}" />
<!-- whatever listener/action to set things -->
</h::form>
</f:view>

You'll notice that the "SessionScoped" data stays until you log out, the view will stay as long as you interact with this view (not totally correct, but that's the gist of it. And the request scope lives in each request.

Basically your create user bean feels like it should have a Request or View scope (as stated in a previous answer). Hopefully this helps. It takes a bit of time to wrap your head around the life-cycle but JSF is rewarding to work with once you get the hang of it.

Daniel B. Chapman
  • 4,647
  • 32
  • 42