...

How to use ExternalContext.redirect () in JSF2.0?

Consider the webapp / myPage.xhtml page :

...
<h:form id="myForm">
    ...
    <h:selectOneMenu id="..." required="true" value="#{myController.aValue}">
       <f:selectItems value="#{...}" var="..." itemValue="#{...}" itemLabel="#{...}"/>
    </h:selectOneMenu>
    ...
    <h:commandButton value="Go for it!" action="#{myController.goForIt(...)}"/>
    ...
</h:form>
...

The button action is bound to the controller method MyController.goForIt () :

@ManagedBean(name = "myController")
@RequestScoped
public class MyController {
   public String goForIt(...){
      if (myCondition){
         try {
            FacesContext.getCurrentInstance().getExternalContext()
                        .redirect("http://www.myTarget.com/thePage.html");
         } catch (IOException e) {
           ...
         }
      }
      return "myPage.xhtml"   
   }
}

My first question is: does this make sense? Is this the correct way to use redirect ()?

I want to redirect the user in http://www.myTarget.com/thePage.htmlcase myCondition- true. If myConditionfalse, it must remain on myPage.xhtml.

If so, I would like to better understand what is happening ... When using Live HTTP Headers in Firefox, I notice that when I click the button

  • POST webapp / myPage.xhtml message appears
  • 302 Moved Temporarily - Location: www.myTarget.com/thePage.html
  • GET www.myTarget.com/thePage.html

, ?

, webapp/myPage.xhtml. , preRenderView -event. ( preRenderView-listener, .)

? - ?

! J.

+3
1

, ?

HTTP- new URL-, Location. , , , .

URL- . URL- . URL-, , http://example.com/context/page.jsf, http://example.com/context/www.myTarget.com/thePage.html, , , . URL- , URL-. , http://.

, webapp/myPage.xhtml.

, . return null try.

+8

Source: https://habr.com/ru/post/1765102/


All Articles