Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Pass parameter from JSF page to backing bean

  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    188
    Rep Power
    7

    Default Pass parameter from JSF page to backing bean

    Hello,

    I need to pass paramter but using outputText tag to java class (backing bean)


    XML Code:
    <?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:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
        <h:body>
            <h1>JSF 2 param example</h1>
            <h:form id="form">
    
                <h:outputText value="#{user.parameter}">
                    <f:param name="parameter" value="Hi JSF 2.0" />    
                </h:outputText>
    
            </h:form>
        </h:body>
    </html>

    Java Code:
    import java.util.Map;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.ExternalContext;
    import javax.faces.context.FacesContext;
    
    @ManagedBean(name = "user")
    @SessionScoped
    public class UserBean {
     public  String getParameter() {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext externalContext = facesContext.getExternalContext();
    
            Map<String, String> params = externalContext.getRequestParameterMap();
            String parameter = params.get("parameter");
            System.out.println("the parameter is: " + parameter);
            return parameter;
        }
    }
    All what i get is:
    XML Code:
    the parameter is: null
    So why it doesn’t work with outputText tag? And how to fix it?
    Last edited by vector_ever; 06-30-2014 at 03:06 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Pass parameter from JSF page to backing bean

    Where's the value of "parameter" coming from?

    Your getter is pulling it from the request.
    Where is the form (or whatever) that submitted it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    188
    Rep Power
    7

    Default Re: Pass parameter from JSF page to backing bean

    I set the the value of the parameter manually
    XML Code:
    <f:param name="parameter" value="Here the value of the parameter" />
    Where is the form (or whatever) that submitted it?
    There is no another form, just the code above..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Pass parameter from JSF page to backing bean

    No it's not.
    That param tag does not do what you think it does.
    It's not setting a request parameter, it's setting a parameter associated with the enclosing tag.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    188
    Rep Power
    7

    Default Re: Pass parameter from JSF page to backing bean

    It's not setting a request parameter, it's setting a parameter associated with the enclosing tag.
    What do you mean?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Pass parameter from JSF page to backing bean

    Well, if outputText had a value of "Hello {}", then a param tag of:
    Java Code:
    <f:param value="Foo" />
    would result in "Hello Foo".

    For a link (can't remember the tag name offhand) a param tag of:
    Java Code:
    <f:param name="firstname" value="Bob" />
    would result in a parameter being attached to the link called "firstname", which would appear in the request.

    In your case, as far as I can make out, that tag is having no effect.

    Of course, you need to read the documentation to check what I'm saying. My JSF is ropey at best, but that's the impression I got from a brief reading of the tag.

    And use the proper (Oracle) documentation...not some random website.
    vector_ever likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Join Date
    Apr 2014
    Location
    http://www.roytuts.com
    Posts
    51
    Rep Power
    0

    Default Re: Pass parameter from JSF page to backing bean

    You can use <h:outputFormat> instead of <h:outputText> e.g.

    Java Code:
    <h:outputFormat value="#{user.parameter}">
                    <f:param name="parameter" value="Hi JSF 2.0" />   
    </h:outputFormat>

  8. #8
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    188
    Rep Power
    7

    Default Re: Pass parameter from JSF page to backing bean

    Quote Originally Posted by itswebin@gmail.com View Post
    You can use <h:outputFormat> instead of <h:outputText> e.g.

    Java Code:
    <h:outputFormat value="#{user.parameter}">
                    <f:param name="parameter" value="Hi JSF 2.0" />   
    </h:outputFormat>
    It make no difference still no parameter be passed

Similar Threads

  1. Pass a superclass as a parameter?
    By kkid in forum New To Java
    Replies: 4
    Last Post: 11-29-2013, 02:55 PM
  2. pass a parameter from sql table to jsp page.
    By kirti bansal in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 05-31-2012, 09:32 AM
  3. JSF reference to backing bean
    By tronied in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 03-22-2011, 07:33 AM
  4. how to pass parameter from one jsp to another jsp
    By kader_sit in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-10-2010, 02:04 PM
  5. Replies: 1
    Last Post: 08-22-2008, 06:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •