Em Fri, 24 Oct 2008 14:21:05 -0300, Joel Halbert <[EMAIL PROTECTED]>
escreveu:
Is there a hook you can leverage to trap an event when the user
navigates to a new page? Specifically I want to be able to call
ComponentResources.discardPersistentFieldChanges() on the previous page
when this happens...
I've just implemented this solution as a RequestFilter. It could be
improved in some points (getting the page name and the session page
attribute clearing are a little hacky), but at least it works. :)
In AppModule or any other Tapestry-IoC module class:
/**
* RequestFilter that clears all persistent fields of a page when the user
* requests another page.
*
* @author Thiago H. de Paula Figueiredo (thiagohp at gmail dot com).
*/
public class ClearLastPageRequestFilter implements RequestFilter {
final private static String ATTRIBUTE =
ClearLastPageRequestFilter.class.getName();
private ComponentClassResolver componentClassResolver;
private HttpServletRequest httpServletRequest;
/**
* @param pagePool
* @param componentClassResolver
* @param request
*/
public ClearLastPageRequestFilter(ComponentClassResolver
componentClassResolver,
HttpServletRequest request) {
this.componentClassResolver = componentClassResolver;
this.httpServletRequest = request;
}
/**
* @see
org.apache.tapestry5.services.RequestFilter#service(org.apache.tapestry5.services.Request,
* org.apache.tapestry5.services.Response,
org.apache.tapestry5.services.RequestHandler)
*/
public boolean service(Request request, Response response, RequestHandler
handler)
throws IOException {
final Session session = request.getSession(true);
String lastPage = (String) session.getAttribute(ATTRIBUTE);
String thisPage = getPageName();
if (lastPage != null && thisPage != null &&
lastPage.equalsIgnoreCase(thisPage) == false) {
final List<String> attributeNames =
session.getAttributeNames();
for (String attributeName : attributeNames) {
if (attributeName.toLowerCase().startsWith("state:" +
thisPage.toLowerCase())) {
session.setAttribute(attributeName,
null);
}
}
}
if (thisPage != null) {
session.setAttribute(ATTRIBUTE, thisPage);
}
return handler.service(request, response);
}
private String getPageName() {
// copied from RequestImpl.getPath()
String path = httpServletRequest.getPathInfo();
if (path == null) {
path = httpServletRequest.getServletPath();
}
// Websphere 6.1 is a bit wonky (see TAPESTRY-1713), and tends to return
the empty string
// for the servlet path, and return the true path in pathInfo.
path = path.length() == 0 ? "/" : path;
path = path.trim();
if (path.equals("/")) {
return "Index";
}
else {
String extendedName = path.length() == 0 ? path :
path.substring(1);
// Copied and adapted from Tapestry's
PageRenderDispatcher
// Ignore trailing slashes in the path.
while (extendedName.endsWith("/")) {
extendedName = extendedName.substring(0,
extendedName.length() - 1);
}
int slashx = extendedName.length();
boolean atEnd = true;
while (slashx > 0) {
String pageName = extendedName.substring(0,
slashx);
if
(componentClassResolver.isPageName(pageName)) {
return pageName;
}
// Work backwards, splitting at the next slash.
slashx = extendedName.lastIndexOf('/', slashx -
1);
atEnd = false;
}
}
// this isn't a page.
return null;
}
}
public static void
contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
ComponentClassResolver componentClassResolver,
HttpServletRequest request) {
ClearLastPageRequestFilter filter = new ClearLastPageRequestFilter(
componentClassResolver, request);
configuration.add("clearlastpage", filter, "before:*");
}
--
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
Consultor, desenvolvedor e instrutor em Java
http://www.arsmachina.com.br/thiago
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]