I had the same problem awhile back, see thread:
http://www.mail-archive.com/[email protected]/msg11419.html
I kept searching around and here is the solution I came up with. Implement
PageAttacheListener on your BasePage extending class and implement the
PageAttachListener like so:
---UserList.java---
public abstract class UserList extends BasePage implements
PageAttachListener {
...
public void pageAttached(PageEvent event) {
if (!getUserVisit().isLoggedIn()) {
throw new PageRedirectException("Login");
}
}
...
@InjectState("visit")
public abstract UserVisit getUserVisit();
}
---UserVisit.java---
import java.io.Serializable;
public class UserVisit implements Serializable {
boolean loggedIn = false;
public UserVisit() {
}
public boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}
}
---hivemodule.xml---
<?xml version="1.0"?>
<module id="app" version="1.0.0" package="services">
<contribution configuration-id="tapestry.url.ServiceEncoders">
<page-service-encoder id="page" extension="html"
service="page" />
</contribution>
<contribution configuration-id="tapestry.state.ApplicationObjects">
<state-object name="visit" scope="session">
<create-instance
class="com.mowyourlawn.session.UserVisit" />
</state-object>
</contribution>
</module>
At first I thought I wanted a catchall like I had with JSF
(http://mowyourlawn.com/blog/?p=6) but then I realized that method required
me to hard code the page names into the listener. So with this Tapestry
method it is still more or less hard coded, but you don't have to worry
necessarily about when your page names change. (i.e. UserList changes to
Users)
I am working on a Tapestry 4.0 CRUD (Create, Read, Update, Delete)
application that has a lot of these tidbits that people need but aren't
necessarily apparent how to do them based on the Tapestry documentation.
HTH,
Aaron Bartell
http://mowyourlawn.com/blog
-----Original Message-----
From: Cagan [mailto:[EMAIL PROTECTED]
Sent: Friday, January 27, 2006 10:07 AM
To: [email protected]
Subject: forcing Login
Hello all,
I am quite new to Tapestry, trying to leave Struts behind...
I'm using Tapestry 4.0 running on Tomcat 5.5.15.
I'm trying to setup a system where there is a common base class for all
Pages that require a user to login first.
This is going to be a bit lenghty as I want to give all the details of my
setup...
For that, I wrote a simple AAdminPage :
@InjectStateFlag("user")
public abstract boolean getUserExists();
@InjectPage("Login")
public abstract Login getLoginPage();
/**
* @see
org.apache.tapestry.event.PageValidateListener#pageValidate
(org.apache.tapestry.event.PageEvent)
*/
public void pageValidate(PageEvent pe) {
if (!getUserExists()){
Login login = getLoginPage();
login.setNextPage(this);
throw new PageRedirectException(login);
}
}
and Login:
@InjectPage("Home")
public abstract Home getHomePage();
@Persist("user")
public abstract User getUser();
public abstract void setUser(User usr);
public IPage doSubmit(){
log.fine("-->doSubmit()");
try{
setUser(UserManager.authenticate(getEmail(), getPassword ()));
return getNextPage() == null ? getHomePage() : getNextPage ();
}catch(AuthenticationException ae){
return null;
}
}
public abstract void setEmail(String email);
public abstract void setPassword(String password);
public abstract String getEmail();
public abstract String getPassword();
public abstract void setNextPage(IPage page);
public abstract IPage getNextPage();
and Home is an empty class that simply extends AAdminPage..
I have in META-INF/hivemodule.xml:
<?xml version="1.0"?>
<module id="com.teknolabs.teknotrip" version="1.0.0">
<contribution configuration-id="tapestry.state.ApplicationObjects">
<state-object name="user" scope="session">
<create-instance
class="com.teknolabs.teknotrip.model.company.User"/>
</state-object>
</contribution>
</module>
When I try to go to localhost:8080/app, I get the following error:
"'user' is not a declared application state object."
which is happening inside pageValidate method of AAdminPage at !
getUserExists() line...
t what is it that I'm doing wrong here?
Thanks in advance.
Cagan
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]