1 2 Previous Next 15 Replies Latest reply on Jan 23, 2008 2:25 AM by wachtda

    PhaseListener in Seam 2.0

      Hello

      I tried to implement my own PhaseListener in Seam 2.0.
      I made a class for trim all the ui-inputs and added it to the faces-config.xml, but nothing happens...

      Is it possible to write a custom PhaseListener?
      If not, how can I trim every ui-input?

      It should be possible to do this in a central-point and not in every-action where I get ui-inputs...

      Thanks
      Daniel

        • 1. Re: PhaseListener in Seam 2.0
          mail.micke

          Are you sure the PhaseListener is implemented correctly, because it should work.

          Works for me with seam 1.2.1.

          Did you specify its phase correctly?

          cheers

          • 2. Re: PhaseListener in Seam 2.0

            I think I implemented correctly:

            faces-config.xml

             <application>
             <locale-config>
             <default-locale>de</default-locale>
             <supported-locale>de</supported-locale>
             <supported-locale>fr</supported-locale>
             <supported-locale>it</supported-locale>
             <supported-locale>en</supported-locale>
             <supported-locale>es</supported-locale>
             </locale-config>
            
             <lifecycle>
             <phase-listener>com.solarmax.scratchbook.action.TrimUIInputStrings</phase-listener>
             </lifecycle>
             </application>
            


            It seems that isn't possible in Seam 2.0...
            But there must be any solution to do that! (hopefully)

            • 3. Re: PhaseListener in Seam 2.0

              I would recommend instead writing a Seam component that observes the phase events that Seam raises (i.e. org.jboss.seam.beforePhase, org.jboss.seam.afterPhase). This is very simple to do and provides you bijection as well.

              • 4. Re: PhaseListener in Seam 2.0
                mail.micke

                Hi

                I was thinkig about this method in your phase listener:

                public PhaseId getPhaseId() {
                 // The phase you want the listener to execute for
                 return PhaseId.ANY_PHASE;
                }
                


                Joacobs suggestion is probably a better idea though, especially if you want to access other jsf manged beans.



                • 5. Re: PhaseListener in Seam 2.0

                  Thank you for the hint with the observer.
                  I'm created a Seam-Component with a observer for org.jboss.seam.beforePhase.

                  It works, but I get always an error:

                  08:13:04,936 ERROR [STDERR] java.lang.NullPointerException
                  08:13:04,937 ERROR [STDERR] at com.solarmax.scratchbook.action.TrimUIInputStrings.beforePhase(TrimUIInputStrings.java:24 )
                  08:13:04,937 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  08:13:04,937 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                  08:13:04,938 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  08:13:04,938 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:4 2)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:106)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java :155)
                  08:13:04,938 ERROR [STDERR] at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:91)
                  08:13:04,938 ERROR [STDERR] at com.solarmax.scratchbook.action.TrimUIInputStrings_$$_javassist_2.beforePhase(TrimUIInput Strings_$$_javassist_2.java)
                  08:13:04,938 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  08:13:04,938 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                  08:13:04,939 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  08:13:04,939 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.Component.callComponentMethod(Component.java:2078)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.core.Events.raiseEvent(Events.java:85)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.jsf.SeamPhaseListener.raiseEventsBeforePhase(SeamPhaseListener.java:381)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:118)
                  08:13:04,939 ERROR [STDERR] at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:222)
                  08:13:04,939 ERROR [STDERR] at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                  08:13:04,939 ERROR [STDERR] at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                  08:13:04,939 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j ava:290)
                  08:13:04,939 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
                  08:13:04,939 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  08:13:04,940 ERROR [STDERR] at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
                  08:13:04,940 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  08:13:04,940 ERROR [STDERR] at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                  08:13:04,940 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  08:13:04,940 ERROR [STDERR] at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:44)
                  08:13:04,940 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  08:13:04,940 ERROR [STDERR] at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
                  08:13:04,940 ERROR [STDERR] at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
                  08:13:04,941 ERROR [STDERR] at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
                  08:13:04,941 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  08:13:04,941 ERROR [STDERR] at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
                  08:13:04,941 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                  08:13:04,941 ERROR [STDERR] at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j ava:235)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  08:13:04,941 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j ava:235)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                  08:13:04,941 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.ja va:179)
                  08:13:04,941 ERROR [STDERR] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                  08:13:04,942 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
                  08:13:04,942 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                  08:13:04,942 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                  08:13:04,942 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java: 157)
                  08:13:04,942 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                  08:13:04,942 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
                  08:13:04,942 ERROR [STDERR] at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:856)
                  08:13:04,942 ERROR [STDERR] at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProto col.java:566)
                  08:13:04,942 ERROR [STDERR] at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1508)
                  08:13:04,942 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
                  



                  My Seam-Component:

                  @Name("trimUIINputStrings")
                  public class TrimUIInputStrings {
                  
                   @Observer("org.jboss.seam.beforePhase")
                   public void beforePhase(PhaseEvent event) {
                   try {
                   FacesContext context = event.getFacesContext();
                   HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
                  
                   //Only handle POST
                   if ("POST".equalsIgnoreCase(request.getMethod())) {
                   trimUIInputStrings(context.getViewRoot().getChildren());
                   }
                   } catch (Exception e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                   }
                   }
                  
                  
                   private void trimUIInputStrings(List<UIComponent> children) {
                  
                   for (UIComponent child : children) {
                   //Only handle UIInput components
                   if(child instanceof UIInput) {
                   UIInput input = (UIInput) child;
                   Object value = input.getValue();
                  
                   //Only handle Strings
                   if(value != null && value instanceof String) {
                   input.setValue(((String) value).trim());
                   }
                   }
                  
                   if(child.getChildCount() > 0) {
                  
                   //Loop through it's children
                   trimUIInputStrings(child.getChildren());
                   }
                   }
                   }
                  
                  }
                  


                  Any hints for me?
                  Thank you

                  • 6. Re: PhaseListener in Seam 2.0
                    nickarls

                    I don't think the signature of the event listener should be like that.

                    It's not really a part of the JSF lifecycle so event is going to be null always in your case. Access the faces context from FacesContext.getInstance() (or whatever the real syntax was).

                    • 7. Re: PhaseListener in Seam 2.0

                      If I get the context via FacesContext I have exactly the same issues:


                       @Observer("org.jboss.seam.beforePhase")
                       public void beforePhase() {
                       try {
                       //FacesContext context = event.getFacesContext();
                       FacesContext context = FacesContext.getCurrentInstance();
                       HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
                      
                       //Only handle POST
                       if ("POST".equalsIgnoreCase(request.getMethod())) {
                       trimUIInputStrings(context.getViewRoot().getChildren());
                       }
                       } catch (Exception e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                       }
                       }
                      


                      • 8. Re: PhaseListener in Seam 2.0
                        nickarls

                        Which line does the NPE come from? Is it the faces context or external context that is null? Not that I'm that familiar with the JSF lifecycle so that I could say what should be available at what phase...

                        • 9. Re: PhaseListener in Seam 2.0

                          The NPE comes from the FacesContext self.
                          It's always the first beforePhase which throws the NPE!

                          As a workaround I surround the call of the trim-method with a if-check!

                          //Check if the Context has no ViewRoot
                          if (context.getViewRoot() != null) {
                           trimUIInputStrings(context.getViewRoot().getChildren());
                          }
                          


                          Now It works without throwing a NPE...
                          It seems, that the ViewRoot of the context is null in the first beforePhase call.
                          Maybe you have a propper solution
                          Cheers

                          • 10. Re: PhaseListener in Seam 2.0
                            nickarls

                            Go back to square one and implement the normal PhaseListener and override the getPhaseId() to only react to phases in which the view root exists ;-)

                            • 11. Re: PhaseListener in Seam 2.0

                              It seems, that seam 2.0 doesnt allow to implement a "normal" phaseListener.
                              If I implent the phaseListener the "normal" way, nothing happens!

                              But now I have the problem, that if I make it with the observer it work's but not if i have ajax elements on my page. (Aren't they in the ViewRoot?)

                              • 12. Re: PhaseListener in Seam 2.0
                                pmuir

                                Seam doesn't alter phase listeners registration with JSF at all.

                                • 13. Re: PhaseListener in Seam 2.0

                                  Peter, that means that it should possible to make a PhaseListener as in JSF?

                                  • 14. Re: PhaseListener in Seam 2.0

                                     

                                    "wachtda" wrote:
                                    Peter, that means that it should possible to make a PhaseListener as in JSF?


                                    Yes, this is quite possible, there is just no guarantee on ordering so you should be aware of that.

                                    With the observer, why not base your execution on the phases you are interested in?
                                    @Observer("org.jboss.seam.beforePhase")
                                    public void beforePhase(PhaseEvent event)
                                    {
                                     if(event.getPhaseId() == PhaseId.PHASE_IM_INTERESTED_IN)
                                     {
                                     ... ...
                                    

                                    If you are not interested in the beforePhase of RESTORE_VIEW because the view is not yet restored, just don't process during that phase.

                                    Hope that helps.

                                    1 2 Previous Next