public final class UserSession extends WebSession
{
private Usuario user;
/**
* Constructor.
* @param app
*/
public UserSession(Application app)
{
super(app);
}
/**
*
* @return true si existe un usuario logeado.
*/
public boolean isSignedIn()
{
if (user == null)
return false;
return true;
}
/**
*
* @return el nombre del usuario logeado.
*/
public Usuario getUser()
{
return user;
}
/**
* @param user
* New user
*/
public void setUser(Usuario user)
{
this.user = user;
}
}
I have the sessionFactory on the application
and then on the submit method i do:
UserSession us = (UserSession)getSession();
us.setUser(usuario);
setResponsePage(new Bienvenido());
where usuario is an model object i get from database.
Is this right?
On 11/21/05, Igor Vaynberg <[EMAIL PROTECTED]> wrote:
On 11/21/05, Manuel Corrales < [EMAIL PROTECTED]> wrote:Hi, i've asked this question once but dont get a solution. So here i go again:
Sorry, must've missed it.
I have some clues about stateful beans and stateless beans but not too much. What i need is that when a user logins, create an object and that that object exists during the user session (to logout). Now i think this is easy to do with a app server, but all i have is tomcat and wicket. Is there a way of achieve this?
This is easy to do w/out an application server. all you have to do is put the object into user's servlet-container session. It will remain there until you close the session on user's behalf (users clicks log out link) or the session times out. This is a general solution that will work in any servlet-container environment. A problem with this approach is that the session is just a map, so you always have to cast the object when you pull it out. Wicket builds on top of the servlet container's session by allowing you to use type-safe semantics. look at WebSession object in wicket and ISessionFactory. By providing your own ISessionFactory you can create your own subclass of WebSession which can have a getter/setter method for your object. Whenever the object is there, simply call the setter in the websession and the object will remain there across requests. Use the getter to pull it back out.
Here is an example implementation:
public class PhonebookApplication extends HibernateApplication {
private final ISessionFactory sessionFactory=new ISessionFactory() {
public wicket.Session newSession() {
return new PhonebookSession(PhonebookApplication.this);
}
};
protected ISessionFactory getSessionFactory() {
return sessionFactory;
}
}
Here PhonebookSession is a custom session subclass public class PhonebookSession extends WebSession
You can get to session by creating a utility class in your page subclass that looks like this:
public class BasePage extends WebPage { public PhonebookSession getMySession() { return (PhonebookSession)getSession()); }Another related question is: How can i forbid access to a non loged user?
If you are using 1.1 check out the Page.checkAccess() method. This method is called before rendering of any page. Here you do the check and if the user is not logged in redirect them to a login page.
See Sign-in example for both solutions. Let me know if you need more help.
-Igor