Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3421596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:07:16+00:00 2026-05-18T06:07:16+00:00

I need to change the context of my site by using parameter sended by

  • 0

I need to change the context of my site by using parameter sended by client.

For example, if I call http://localhost:8084/JSF/ I load the usual index.xhtml with the “Homepage” page on the content template (as default).
But, if I call http://localhost:8084/JSF/index.xhtml?page=profile, I need a sort of switch in the index.xhtml, and include/insert the profile template (or a page that define profile) in my content area.

I think i need to manage a servlet to do it, because i don’t think i can create a sort of swith in my index.xhtml. So i think i need to load some template instead of another.

Which servlet i need to use? Or i need to create my own Servlet to do this?

Cheers

UPDATE (added after BalusC’s suggestion)

package Beans;

import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ManagedBean;

@ManagedBean(name="selector")
@ManagedProperty(value="#{param.page}")
public class Selector {
    private String page;

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

}

template.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title><ui:insert name="title">Facelets Template</ui:insert></title>
    </h:head>

    <h:body>
        <ui:insert name="login_homepage">Box Content Here</ui:insert>

        <ui:insert name="content_homepage">Box Content Here</ui:insert>
    </h:body>
</html>

index.xhtml

 <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition template="./template.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <ui:define name="title">
        // title
    </ui:define>

    <ui:define name="login_homepage">
        // login
    </ui:define>

    <ui:include src="#{selector.page}.xhtml" />

    <ui:define name="content_homepage">
        // content
    </ui:define>
</ui:composition>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

profile.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h2>PROFILE</h2>
</ui:composition>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:07:16+00:00Added an answer on May 18, 2026 at 6:07 am

    Request parameters are settable in JSF bean by @ManagedProperty.

    @ManagedProperty(value="#{param.page}")
    private String page;
    

    (this does basically a bean.setPage(request.getParameter("page")) directly after bean’s construction)

    You can use EL in Facelets <ui:include>.

    <ui:include src="#{bean.page}.xhtml" />
    

    (if bean.getPage() returns profile, the value would end up as profile.xhtml and included accordingly)

    No need for legacy servlets 🙂


    Update: you’ve set the annotation at the wrong place. It should look like this, exactly as in my original answer:

    package beans;
    
    import javax.faces.bean.ManagedProperty;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    
    @ManagedBean
    @RequestScoped
    public class Selector {
    
        @ManagedProperty(value="#{param.page}")
        private String page;
    
        public String getPage() {
            return page;
        }
    
        public void setPage(String page) {
            this.page = page;
        }
    
    }
    

    Note that I omitted the @ManagedBean name since the default value is already the classname with 1st character lowercased (which is exactly the same as you specified manually). I also added the @RequestScoped annotation to specify the bean scope. I also lowercased the packagename since uppercases are disallowed in package name as per standard Java Naming Conventions.

    The whole <managed-bean> in faces-config.xml is entirely superfluous with the new JSF 2.0 annotations. You’re basically duplicating it. Remove it.


    Update 2: the index.xhtml should look like this

    <!DOCTYPE html>
    <html lang="en"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:c="http://java.sun.com/jsp/jstl/core">
        <h:head>
            <title>Include demo</title>
        </h:head>
        <h:body>
            <h1>This is the index page</h1>
            <c:catch>
                <ui:include src="#{selector.page}.xhtml" />
            </c:catch>
        </h:body>
    </html>
    

    (the <c:catch> is there to suppress the FileNotFoundException whenever there’s no such file)

    The include.xhtml should look like this:

    <!DOCTYPE html>
    <ui:composition 
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets">
        <h2>Include page content</h2>
    </ui:composition>
    

    Assuming that FacesServlet is listening on url-pattern of *.xhtml and the both files are in the same folder, open it by index.xhtml?page=include.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use Pjax with tutorial from http://railscasts.com/episodes/294-playing-with-pjax?view=comments I don't need change url and this
I need to change table name from lowercase to uppercase but using this statement
After load content to page using div.load('page.php'); I need get all of links from
I need to go around a site using java programmatically but the site doesn't
I have a wx.Frame . I need to change the content from time to
I need change first image in Galleria fullscreen theme ( link ) after Galleria.run().
I need change background of all text that have two spaces from the start
i need change color of SystemTray.ProgressIndicator color, how i can do it? I tried
I have database, and in one the tables I need change the values of
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.