views:

89

answers:

1

Here is how my commandLink work

 <p:dataTable value="#{myBean.users}" var="item">
     <p:column>
         <h:commandLink value="#{item.name}" action="#{myBean.setSelectedUser(item)}" />     
     </p:column>
 </p:dataTable>

then in myBean.java

 public String setSelectedUser(User user){
     this.selectedUser = user;
     return "Profile";
 }

Let assume the user name is Peter. Then if I click on Peter, I will set the selectedUser to be Peter's User Object, then redirect to the profile page, which now render information from selectedUser. I want to create that same effect only using <h:outputText>, so GET request come to mind. So I do this

 <h:outputText value="{myBean.text(item.name,item.id)}" />

then the text(String name, Long id) method just return

"<a href=\"someURL?userId=\"" + id + ">" + name + "</a>"

all that left is creating a servlet, catch that id, query the database to get the user object, set to selectedUser, the redirect. So here is my servlet

public class myServlet extends HttpServlet { 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Long userId = Long.parseLong(request.getParameter("userId"));
    }
}

Now I have the id, how do I access my session bean to query the database for the user, then access managed bean to set the user to selectedUser, then redirect to profile.jsf?

+1  A: 

JSF stores session scoped managed beans as session attribute using managed bean name as key. So the following should work (assuming that JSF has already created the bean before in the session):

MyBean myBean = (MyBean) request.getSession().getAttribute("myBean");

That said, I have the feeling that you're looking in the wrong direction for the solution. You could also just do like follows:

<a href="profile.jsf?userId=123">

with the following in a request scoped bean associated with profile.jsf

@ManagedProperty(value="#{param.userId}")
private Long userId;

@ManagedProperty(value="#{sessionBean}")
private SessionBean sessionBean;

@PostConstruct
public void init() {
    sessionBean.setUser(em.find(User.class, userId));
    // ...
}
BalusC
For the `SessionBean`, dont u have to inject @EJB annotation?
Harry Pham
Can also. That was not clear from the question :) It's just a kickoff example how to make use of `@ManagedProperty` and `@PostConstruct` to apply and process request parameters.
BalusC
I am sorry, but this might be a stupid question. The way I associate the `jsf` file with the managed bean is basically just calling the method of the bean via `EL`. The bean for `profile.jsf` is session scoped. I know I need a request/view scoped bean, but now I dont know how to associate that bean to `profile.jsf`. `init()` method never get call.
Harry Pham
Just reference it somewhere in the view. E.g. `Welcome, #{bean.sessionBean.user.name}`.
BalusC
Thank you. It work with the requestScoped, but with viewScoped, it generate the error, `The scope of the object referenced by expression #{param.userId}, request, is shorter than the referring managed beans (Profile) scope of view`. Thank you BalusC
Harry Pham
JSF is right, I removed the viewscope suggestion :)
BalusC
Is there a way to get the parameter from the URL in the ViewScoped, or you must do it in RequestScoped like what you suggest above? It seems like it a bit too restricted if JSF only allow to do it in RequestScoped bean.
Harry Pham
never mind, I can get it via getRequestParameterMap().
Harry Pham