[jboss-user] [JBoss Seam] - Re: @Redirect

chane do-not-reply at jboss.com
Thu Jul 27 00:01:58 EDT 2006


Thanks!

I'll give your 1st option a shot tomorrow to see what I can come up with.  I have not used the redirect filter explicetly before, so it'll be something new to learn about.

In the meantime below is something that I have been playing with.  Basically I am adding a new interceptor.  I believe I have acheived what I want for handling the exception and returning a navigation rule that is an error page.  I have yet to use a FacesMessage on it; but I don't see why it shouldn't work.

I have not tested this extensively yet and would appreciate thoughts/concerns that anyone might see with my approach.

Thanks,
Chris....

The annotation I use on the BusinessException and as a marker on the SFSB:

  | @Target(TYPE)
  | @Retention(RUNTIME)
  | @Interceptors(ExceptionInterceptor.class)
  | public @interface Redisplay {
  |    String viewId() default "";
  | }
  | 

The Interceptor I am using (for now I have named it the same as the Seam Interceptor - except it is in a different package).  In fact I do most of the same stuff, except I had to add the rollback stuff which I grabbed from the SeamExceptionFilter.

I'm not sure if I need to add the SeamExceptionFilter.endWebRequestAfterException(request) functionality.

  | @Around( { ConversationalInterceptor.class, RemoveInterceptor.class, BijectionInterceptor.class })
  | public class ExceptionInterceptor{
  | 
  |    private static final Log log = LogFactory.getLog(ExceptionInterceptor.class);
  | 
  |    @AroundInvoke
  |    public Object handleExceptions(InvocationContext invocation) throws Exception{
  |       boolean outermost = invocation.getContextData().get("custom.outermost.ExceptionInterceptor") == null;
  |       invocation.getContextData().put("custom.outermost.ExceptionInterceptor", true);
  |       try {
  |          log.fatal("===== Execute Exception Handler ==========");
  |          return invocation.proceed();
  |       } catch(Exception e) {
  |          log.fatal("====== Process Exception =======");
  | 
  |          if(outermost && FacesContext.getCurrentInstance() != null) {
  |             if(e.getClass().isAnnotationPresent(Redisplay.class)) {
  |                rollbackTransactionIfNecessary();
  |                handled(e);
  |                return e.getClass().getAnnotation(Redisplay.class).viewId();
  |             }
  |          }
  |          throw e;
  |       }
  |    }
  | 
  |    private void rollbackTransactionIfNecessary(){
  |       try {
  |          if(Transactions.isTransactionActiveOrMarkedRollback()) {
  |             log.info("killing transaction");
  |             Transactions.getUserTransaction().rollback();
  |          }
  |       } catch(Exception te) {
  |          log.error("could not roll back transaction", te);
  |       }
  |    }
  |    
  |    private static void handled(Exception e){
  |       getRequest().put("org.jboss.seam.exceptionHandled", e);
  |    }
  | 
  |    private static Map getRequest(){
  |       return FacesContext.getCurrentInstance().getExternalContext()
  |             .getRequestMap();
  |    }
  | 
  | }
  | 
  | 

Usage would be:

  | @Name("editor")
  | @Stateful
  | @Redisplay
  | public class Editor implements IEditor{
  |    
  |    public Fulfillment fulfill;
  | 
  |    @Begin
  |    public String save(){
  |       //persist logic  //throws BusinessException
  |       return "save";
  |    }
  | }
  | 

Simple exception class

  | @Redisplay(viewId="exceptionErrorMessage")
  | public class BusinessException extends RuntimeException{
  |    private static final long serialVersionUID = -4422447214761623238L;
  | 
  |    public BusinessException(){
  |       super("Generic Business Exception");
  |    }
  |    
  |    public BusinessException(String msg){
  |       super(msg);
  |    }
  |    
  | }
  | 

simple navigation rule

  |     <navigation-rule>
  |         <navigation-case>  
  |             <from-outcome>exceptionErrorMessage</from-outcome> 
  |             <to-view-id>/error.xhtml</to-view-id>
  |         </navigation-case> 
  |     </navigation-rule>
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3961178#3961178

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3961178



More information about the jboss-user mailing list