Tuesday, June 9, 2015

JSF2 navigation ruin my mood

Hohoho~ I'm just having some fun with JSF2 navigation, it was a real interesting thing. Not at all! Say I have the source page, that could allow me navigate to the target page. Somehow the page doesn't navigate and I were so frustrate what I have been done? In a regular programming practice in JSF2, this is what I have done on the front end site:
<h:body>
 <h:form>
  <p:commandButton action="#{messageListController.link}" value="link">
  </p:commandButton>
 </h:form>
</h:body>
And the messageListController bean will take up the responsibility on the navigation task:
@ManagedBean
@RequestScoped
public class MessageListController {
 public String link() throws IOException {
  
  return "messagePost.html?faces-redirect=true";
 }
}
Guess what will happen? The target page, which is messagePost, will never show. This is because I was mistakenly making messagePost.html instead of messagePost.xhtml. This is what ruin my mood at the end of the day. But fortunately, I have discovered an alternate way to navigate a page. On the front end, I use actionListener like this:
<h:body>
 <h:form>
  <p:commandButton actionListener="#{messageListController.link}" value="link">
  </p:commandButton>
 </h:form>
</h:body>
Then on managed bean site, I would have this:
@ManagedBean
@RequestScoped
public class MessageListController {
 public void link(ActionEvent event) throws IOException {
   FacesContext.getCurrentInstance().getExternalContext().redirect("messagePost.html?faces-redirect=true");
 }
}
Tada! This will still achieve the same result. Thanks to my mistake pushing me to the max power to try every effort I could do.

No comments: