How to do POST-REDIRECT-GET after action with PrettyFaces ?

Splash Forums PrettyFaces Users How to do POST-REDIRECT-GET after action with PrettyFaces ?

This topic contains 16 replies, has 3 voices, and was last updated by  Christian Kaltepoth 5 years, 1 month ago.

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #18125

    ceefour
    Participant

    Hi OCPsoft PrettyFaces,

    With regular JSF + f:viewParam this is doable using:

    public String tellSkill() {

    // do something

    return “show?faces-redirect=true&includeViewParams=true”;

    }

    However, with PrettyFaces I cannot make this work: (f:viewParam is removed from xhtml template)

    public String tellSkill() {

    // do something

    return “pretty:admin_users_show?faces-redirect=true”;

    }

    pretty-config.xml is :

    <url-mapping id=”admin_users_show”>

    <pattern value=”/admin/users/#{ id : userShowView.userId }” />

    <view-id value=”/faces/admin/users/show.xhtml” />

    </url-mapping>

    Whatever I do, I still get a normal postback. The URL is correct but I don’t get a redirect->get :(

    Help… :)

    #21930

    You don’t need to append faces-redirect to the action outcome. PrettyFaces always navigates with POST-REDIRECT-GET. So just use:

    public String tellSkill() {
    // ...
    return "pretty:admin_users_show";
    }

    This should do the trick.

    #21931

    ceefour
    Participant

    @Christian:

    Unfortunately, I’m not getting the behavior you described. I always get a standard postback (the URL is prettyfied, so URL rewriting is working). I’m using PrettyFaces 3.3.2 on JBoss AS 7.0.2 with Servlet 3.0 web.xml.

    The template code is:

    <h2>Relate</h2>

    <p>

    <h:selectOneMenu value=”#{userShowView.relatedSkillId}”>

    <f:selectItems value=”#{skillsView.skills}” var=”skill” itemLabel=”#{skill.name}” itemValue=”#{skill.nodeId}”/>

    </h:selectOneMenu>

    <h:commandButton action=”#{userShowView.tellSkill}” value=”Tell” />

    <h:commandButton action=”#{userShowView.seekSkill}” value=”Seek” />

    </p>

    #21932

    Are you sure that your action method gets invoked correctly? Did you add <h:messages> to the page? Perhaps there is a validation error on the page and so the action method doesn’t get invoked. In this case you would only see a postback without a redirect.

    #21933

    ceefour
    Participant

    @Christian : yes the action method is invoked.

    I also try using a4j:commandButton and it works (After clicking, I can refresh the page and Firefox will not “complain”) with a nice ajax effect.

    But I am still curious how to do it with normal h:commandButton with PrettyFaces.

    The only way I can manage it to work is by using JSF-style viewParam, returning “/admin/users/show?faces-redirect=true&includeViewParams=true” and PrettyFaces will rewrite the URL. This results in proper P-R-G and correct PrettyFaces URL. However it’s quite some work just to get a basic functionality.

    #21934

    It is really very weird that you don’t see PRG behavior with PrettyFaces. Actually I never heard from somebody having problems with this.

    Could you tell us about what stack you are using: Container, JSF-Implementation, additional libraries. Do you use a custom ViewHandler or NavigationHandler?

    #21935

    appatogma
    Member

    Hi,

    I want to commend the great work the PrettyFaces Team is doing. It’s indeed fabulous.

    I have been using PrettyFaces for a while now. The experience has been interesting. I however observed very recently that page navigations do not REDIRECT rather FORWARD. Is the situation? If it is, how can I redirect between pages.

    I will appreciate your kind assistance.

    Mojarra 2.1.7, Primefaces 3.4, Tomcat 7.0.27, PrettyFaces 3.3.2

    #21936

    No, PrettyFaces uses redirects for navigation. Could you show us an example for which the redirect doesn’t work correct?

    #21937

    appatogma
    Member

    This portion of code is in web.xml

    ===================================

    <filter>

    <filter-name>Pretty Filter</filter-name>

    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>

    <async-supported>true</async-supported>

    </filter>

    <filter-mapping>

    <filter-name>Pretty Filter</filter-name>

    <url-pattern>/*</url-pattern>

    <dispatcher>FORWARD</dispatcher>

    <dispatcher>REQUEST</dispatcher>

    <dispatcher>ASYNC</dispatcher>

    <dispatcher>ERROR</dispatcher>

    </filter-mapping>


    pretty-config.xml

    ==================================

    <?xml version=’1.0′ encoding=’UTF-8′?>

    <!– =========== FULL CONFIGURATION FILE ================================== –>

    <pretty-config xmlns=”http://ocpsoft.org/prettyfaces/3.3.3&#8243;

    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

    xsi:schemaLocation=”http://ocpsoft.org/prettyfaces/3.3.3 http://ocpsoft.org/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.3.xsd“>

    <url-mapping id=”setup”>

    <pattern value=”/setup/” />

    <view-id value=”/faces/setup/scheme-list.xhtml”/>

    </url-mapping>

    <url-mapping id=”category”>

    <pattern value=”/category/” />

    <view-id value=”/faces/faq/categoryList.xhtml”/>

    </url-mapping>

    <url-mapping id=”version”>

    <pattern value=”/version” />

    <view-id value=”/faces/faq/createVersion.xhtml”/>

    </url-mapping>

    <url-mapping id=”user”>

    <pattern value=”/user/” />

    <view-id value=”/faces/administrator/viewAdminUser.xhtml”/>

    </url-mapping>

    <url-mapping id=”mobileUser”>

    <pattern value=”/mobileUser/” />

    <view-id value=”/faces/usersession/viewUserSession.xhtml”/>

    </url-mapping>

    <url-mapping id=”home”>

    <pattern value=”/home/”/>

    <view-id value=”/faces/process/generate-returns.xhtml”/>

    </url-mapping>

    <url-mapping id=”login”>

    <pattern value=”/login/” />

    <view-id value=”/faces/index.xhtml”/>

    </url-mapping>

    </pretty-config>


    LoginMBean.java

    =========================

    public String homePage() {

    return “pretty:home”;

    }

    public String userInitiatelogout() {

    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

    FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, “Logout Request”, “User successfully logout”);

    FacesContext.getCurrentInstance().addMessage(null, facesMessage);

    return “pretty:login”;

    }

    …….

    …….

    …….

    The pages navigate correctly. But the challenge experienced here is that when I log out and log in again the pages I have prior visited holds the old values. Investigation shows that this is possible only when pages forward to other pages. But this would not have been if pages redirect. PrettyFaces controls the navigations on my entire application.

    Did I miss anything in my configuration above?

    Kindly advise.

    Regards

    #21938

    You can very easily check whether PrettyFaces redirects correctly by using the development tools of your browser (like Firebug). If it is behaving correctly, you should see a 302 followed by a 200 for a request. If not, you will only see a 200 OK response. Could you check that?

    In case of an action outcome like “pretty:mappingId”, PrettyFaces will always perform a redirect. If not, there seems to be some issue in you application.

    #21939

    Perhaps the “old values” are cause by some sort of scoping problem? Perhaps you beans have a scope which isn’t short enough?

    #21940

    appatogma
    Member

    Hi @Christian,

    Thank you for your feedback. I have attempted your suggestions as requested above. I however can not lay my eyes on either of the status messages above (200 or 300) on firebug. I have attached the server log for your review.

    Another point of note is that all the managed beans are @RequestScoped. Does this have any impact the behaviour in any way? This behaviour is also maintained even on the login page. The pages tends to retain the user name.

    Any assistance is highly appreciated.

    Regards.

    #21941

    I don’t think that the logs will help here. You must have a close look at the requests and response your browser sends and receives. Look at the following screenshot for an example for a typical POST-REDIRECT-GET like it happens if you return something like “pretty:myId” from an action method:

    View post on imgur.com

    The first row is the postback to the server. It executes the action method which returns “pretty:myId”. PrettyFaces therefore sends a 302 response (=redirect) to the client telling the client to load the target page with a GET request. The browser does so in the second row. It is a normal GET request for the target page (which is in this example the same as the page the postback was send to).

    #21942

    appatogma
    Member

    Hi @Christian,

    Please what version of firebug do you have. I can not see this output on mine.

    Regards.

    #21943

    It’s version 1.10.2 which is the most recent version available. But this view already exists a long time in Firebug. You can find it in the “net” tab on the right.

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic.

Comments are closed.