4.19.2013

In order to describe the picture in detail we need at least two .xhtml pages and one backend bean.

Let's consider that we have two pages with such a content (use any code you wish instead of <...>. I will describe only navigation effect)
 <html>
<h:head>...</h:head>
<h:body>
... 
<a href="index.xhtml">Homepage</a>
<a href="publications.xhtml">Publications</a>
...
</h:body>
<html>

Now we need to transform these lines in something that will show us on wich of the pages we are: Homepage or Publications (e.g.).

Let's create the managed bean, that will contain the following:

@ManagedBean (name="mainMB")
@SessionScoped
public class MainManagedBean {
 
    private String url;
 
    public void MainManagedBean(){}
    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }
    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getCurrentPage(){
        url = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRequestURI();
        if (url.contains("index.xhtml")){
            return "index";
        } else
return "publs";
}
What actions does the getCurrentPage do? It is simple, in this method we are getting our current URI and checking its content. If its content is like index.xhtml, we need to return "index" string. In another case we need to return "publs" string.
Let's edit the pages:
<html>
<h:head>...</h:head>
<h:body>
... 

 <c:if test="#{mainMB.currentPage!='index'}">
   <a href="index.xhtml">Homepage</a>
</c:if>
<c:if test="#{mainMB.currentPage=='index'}">
   <a style="color:red" href="index.xhtml">Homepage</a> 
</c:if>
<c:if test="#{mainMB.currentPage!='publs'}">
   <a href="publications.xhtml">Publications</a>
</c:if>
<c:if test="#{mainMB.currentPage=='publs'}">
 
   <a style="color:red" href="publications.xhtml">Publications</a> 
</c:if> 
...
</h:body>
<html>
So, that is all. For now you will see the indication of the current page in red color.


0 коммент.:

Subscribe to RSS Feed Follow me on Twitter!