-
15. Re: Producer method
gavin.king Jan 5, 2010 9:59 PM (in response to mcalpay)
Before login the session @Produces user produce a instance. It's Anonymous copyAh yes, that sounds like the problem. The error is that the @Produces method is marked @RequestScoped, when it should be @Dependent.
-
-
17. Re: Producer method
mcalpay Jan 6, 2010 9:29 AM (in response to mcalpay)Thanks all for the answers.
Since this worked in my case :@Produces @RequestScoped @Principal @Named("principal") private User getPrincipal(Current current) { return current.getUser(); }
instead of :
@Produces @RequestScoped @Principal @Named("principal") private User getPrincipal() { return user; }
I thought there was a problem with the instances. But what made it work was current.getUser() method call instead of the field access. So calling this.getUser() returns me the expected value but this.user field access remains uninjected. It simply has thte value of the field (I set it Anonymous so its anonymous, if you assigned null it returns null).
-
18. Re: Producer method
aliebrahimi Jan 6, 2010 12:45 PM (in response to mcalpay)I think this issue related to following three part of code:
part 1:
private User user = ANONYMOUS;
...
public String checkLogin(User user) { if("testus".equals(user.getName())) { this.user = user; return "/index"; } return ""; } @Produces @RequestScoped @Principal @Named("principal") private User getPrincipal() { return user; }
part 2:
<h:commandButton value="Login" action="#{current.checkLogin(login)}"/>
you passed request scope bean(proxy object) to checkLogin method and assign that to user field that returned by producer method. and so that in each request this proxy bean' s target object refreshed.
-
19. Re: Producer method
mcalpay Jan 6, 2010 2:08 PM (in response to mcalpay)Thanks for the reply.
this.user = user;
Part was problematic and I changed that to bean I got from the entity manager.
${login} is not the 'principal' btw, its just to check users input. This is what It looks now :@SessionScoped @Named("current") public class Current implements Serializable { @Inject @Anonymous private User user; @Inject private EntityManager entityManager; public User getUser() { return user; } public String checkLogin(User user) { try { this.user = (User) entityManager.createNamedQuery(User.Queries.GETUSERBYNAME) .setParameter(1,user.getName()) .getSingleResult(); return "/index"; } catch(NoResultException nre) { return ""; } finally { entityManager.close(); } } @Produces @RequestScoped @Principal @Named("principal") private User getPrincipal() { return this.getUser(); } }
Complete code is here : http://code.google.com/p/teknoatolye/source/browse/#svn/trunk/iwall%3Fstate%3Dclosed
if anyone wants to check out...
thanks -
20. Re: Producer method
gavin.king Jan 6, 2010 6:54 PM (in response to mcalpay)
Ali Ebrahimi wrote on Jan 06, 2010 12:45:
you passed request scope bean(proxy object) to checkLogin method and assign that to user field that returned by producer method. and so that in each request this proxy bean' s target object refreshed.Yes, you're right. That's the correct explanation.