4 Replies Latest reply on Jan 30, 2013 10:26 AM by maschmid

    partial-response xml displayed in browser on seam redirect after ajax request

    canotech

      I have a webapp for photos where users are able to like, dislike and add photos to favorites. Naturally, the users have to be logged in before these functions can be called.

       

       

      The JSF code for the like function is as follows:

       

       

      <h:form style="border-bottom: 1px solid #E8E8E8; padding-bottom: 20px; text-align: center;">
      
                                                                  <br/>
                                                                  <p:outputPanel rendered="#{not likeDislikePhoto.userLikesPhoto( viewPhoto.photo.id )}">
      
      
                                                                                       <h:commandButton value="I Like" style="cursor: pointer; padding: 10px; min-width: 90px;" styleClass="button buttoncolor">
                                                                                                <f:ajax event="click" listener="#{likeDislikePhoto.like( viewPhoto.photo.id, true )}" render="@form :votestats :ratingstats"/>
                                                                                      </h:commandButton>
                                                                                      <h:commandButton value="I Dislike" style="cursor: pointer;padding: 10px; min-width: 90px; margin-left: 20px;" styleClass="button buttoncolor">
                                                                                                <f:ajax event="click" listener="#{likeDislikePhoto.like( viewPhoto.photo.id, false )}" render="@form :votestats :ratingstats"/>
                                                                                      </h:commandButton>
                                                                  </p:outputPanel>
      
                                                                  <p:outputPanel rendered="#{likeDislikePhoto.userLikesPhoto( viewPhoto.photo.id )}">
      
                                                                            <div class="like-fact">You rated this photo</div>
      
                                                                            <div style="display: none;" class="like-remove">
                                                                                      <h:commandLink value="Remove your like" styleClass="greenlink pointer">
                                                                                                <f:ajax event="click" listener="#{likeDislikePhoto.removeLike( viewPhoto.photo.id )}" render="@form :votestats :ratingstats"/>
                                                                                      </h:commandLink>
                                                                            </div>
                                                                  </p:outputPanel>
      
                                              </h:form>
      

       

       

       

       

       

      When the user clicks the like button, if they are not logged in, I would like to redirect them to the login page and after they log in, redirect them to the photo page they were on before clicking the like button. After logging in, I want the function to be executed.

       

       

      Below is the bean code for the like method:

       

       

       

       

       

          public String like( int photoId, boolean likeState ){
      
                              try{
                                        if ( loggedInUser != null ) {
                                                  if( everythingElseOk){
                                              // perform the like
      
                                                            return "success";
      
                                                  } else {
                                                            return "failed";
                                                  }
                                        }
                                        else{
      
                                                  Redirect.instance().setViewId( FacesContext.getCurrentInstance().getViewRoot().getViewId() );
                                                  Redirect.instance().setParameter("id", photoId );
      
                                                  Pages p = Pages.instance();
                                                  p.redirectToLoginView();
      
      
                                                  return "notLoggedIn";
                                        }
                              }
                              catch( Exception e ){
      
                                        e.printStackTrace();
                                        return "failed";
                              }
      
                    }
      
      

      The redirection and logging in all work fine but the problem is that due to the fact that I use the f:ajax for the like action, all sorts of jsf partial update params are added to the url of the redirected page. And worse, the inclusion of these parameters makes the page display <partial-response> xml as shown below:

       

       

         

      <partial-response>
          <changes>
          <update id="favestats">
          <![CDATA[
          <span id="favestats" style="float: right; text-align: center; width: 110px;"> <div style="font-size: 30px;"> 4 </div> <div style="font-size: 11px;"> Favorites </div></span>
          ]]>
          </update>
          <update id="j_idt114">
          <![CDATA[
          <form id="j_idt114" name="j_idt114" method="post" action="/photo/116" enctype="application/x-www-form-urlencoded" style="border-bottom: 1px solid #E8E8E8; padding-bottom: 20px; text-align: center;"> <input type="hidden" name="j_idt114" value="j_idt114" /> <br /><span id="j_idt114:j_idt118"> <div class="fave-fact"> <div style="background: url('/img/favorite.png') no-repeat 10px 100%; height: 16px; margin-left: 10px;" align="center">You added this to favorites</div> </div> <div style="display: none;" class="fave-remove"><a id="j_idt114:j_idt120" href="#" onclick="mojarra.ab(this,event,'click',0,'@form favestats');return false" class="greenlink pointer">Remove from favorites</a> </div></span> </form>
          ]]>
          </update>
          <update id="javax.faces.ViewState">
          <![CDATA[ -4743453278638707233:2218559108323062779 ]]>
          </update>
          </changes>
          </partial-response>
      

       

       

       

       

       

       

       

      The url redirected to is:

       

       

      http://www.example.com/photo/55?c=4&j_idt114=j_idt114&javax.faces.ViewState=-4743453278638707233%3A2218559108323062779&javax.faces.behavior.event=click&javax.faces.partial.ajax=true&javax.faces.partial.event=click&javax.faces.partial.execute=j_idt114%3Aj_idt117+j_idt114%3Aj_idt117&javax.faces.partial.render=j_idt114+favestats&javax.faces.source=j_idt114%3Aj_idt117

       

       

      but all I want is:

       

       

      http://www.example.com/photo/55?c=4 (the c=4 is just conversation id parameter in seam apps and is normal).

       

       

       

       

      In addition, I am using the seam redirect functionality and in my components.xml, I have:

       

       

          <event type="org.jboss.seam.security.notLoggedIn"> 
                <action execute="#{loginRedirector.captureCurrentView}"/>
             </event>
             <event type="org.jboss.seam.security.loginSuccessful">
                <action execute="#{loginRedirector.returnToCapturedView}"/>
             </event>
      
      

       

       

       

       

      My loginRedirector bean is shown below:

       

       

       

          @Name("loginRedirector")
          @Scope(ScopeType.SESSION)
          @AutoCreate
          @Startup
          public class LoginRedirector implements Serializable{
      
                    private static final long serialVersionUID = -3921511488811869974L;
      
                    @Logger
                    Log log;
      
                    @In("redirect")
              private Redirect _redirect;
      
              @In("identity")
              private Identity _identity;
      
              private Redirect _savedRedirect;
      
              public void captureCurrentView() {
                  if (_identity != null && !_identity.isLoggedIn()) {
                      _savedRedirect = _redirect;
                      _redirect.captureCurrentView();
                  }
              }
      
              public void returnToCapturedView()  {
                  if (_savedRedirect != null && _savedRedirect.getViewId() != null ) {
                      _savedRedirect.returnToCapturedView();
                  }
              }
          }
      
      

       

       

      What can I do to solve this problem?

       

      My project environment: Jboss seam 2.3.0 beta2, primefaces 3.4 RC1, JSF 2.1.11, JBoss AS 7.2

       

      Message was edited by: Charles Akalugwu