0

Below is the except from JSF official documentation - The immediate Attribute

Suppose that you have a page with a button and a field for entering the quantity of a book in a shopping cart. If the immediate attributes of both the button and the field are set to true, the new value entered in the field will be available for any processing associated with the event that is generated when the button is clicked. The event associated with the button as well as the events, validation, and conversion associated with the field are all handled when request parameter values are applied.

If the button's immediate attribute is set to true but the field's immediate attribute is set to false, the event associated with the button is processed without updating the field's local value to the model layer. The reason is that any events, conversion, and validation associated with the field occur after request parameter values are applied.

This suggests that, if immediate attribute is set TRUE for input component like textbox and command component then submit button then textbox value will be available for processing, however I think this isn't true, if immediate attribute of the submit button is true, then any data entered by the user on the form is not available for processing because event handling of the submit button will occur during the "apply request values" phase and will result in form submission, so it means that "update data model" phase will not be executed and hence managed bean properties will not be updated with the data entered by the user.

I even tried out this with code and my understanding looks true. However since the excerpt is from official Oracle documentation so I would like to know if I am missing something.

Below is my code sample:

greeting.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>Guess Number Facelets Application</title>
</h:head>
<h:body>
    <h:form>
        <h:graphicImage value="#{resource['images:OracleLogo.png']}"
            alt="Duke waving his hand" />
        <h2>
            Hi, my name is #{userNameBean.lName}, #{userNameBean.fName}. I am thinking of a number from
            #{userNumberBean.minimum} to #{userNumberBean.maximum}. Can you guess it?
        </h2>
        <p>
            User number: <h:inputText id="userNo" title="Enter a number from 0 to 10:" value="#{userNumberBean.userNumber}">
                <f:validateLongRange minimum="#{userNumberBean.minimum}" maximum="#{userNumberBean.maximum}" />
            </h:inputText><br></br>
            User fName: <h:inputText id="userFName" value="#{userNameBean.fName}"  immediate="true" required="true"/><br></br>
            User lName: <h:inputText id="userLName" value="#{userNameBean.lName}"  immediate="true"/><br></br>
            <h:commandButton id="submit" value="Submit" action="#{userNameBean.submitRequest}" immediate="true"/>
        </p>
        <h:message showSummary="true" showDetail="false"
            style="color: #d20005;
font-family: 'New Century Schoolbook', serif;
font-style: oblique;
text-decoration: overline"
            id="errors1" for="userNo" />
    </h:form>
</h:body>
</html>

response.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title>Guess Number Facelets Application</title>
</h:head>
<h:body>
    <h:form>
        <h:graphicImage value="#{resource['images:OracleLogo.png']}"
            alt="Duke waving his hand" />
        <h2>
            <h:outputText id="result" value="${'hip' lt 'hit'}" /><br></br>
            <h:outputText id="result2" value="#{userNumberBean.response}" /><br></br>
            <h:outputText id="result3" value="#{userNameBean.lName}" />, <h:outputText id="result4" value="#{userNameBean.fName}" />
        </h2>
        <h:commandButton id="back" value="Back" action="greeting" />
    </h:form>
</h:body>
</html>

UserNumberBean:

import java.io.Serializable;
import java.util.Random;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class UserNumberBean implements Serializable {
    private static final long serialVersionUID = 5443351151396868724L;
    Integer randomInt = null;
    Integer userNumber = null;
    String response = null;
    private int maximum = 10;
    private int minimum = 0;

    public UserNumberBean() {
        Random randomGR = new Random();
        randomInt = new Integer(randomGR.nextInt(maximum + 1));
        System.out.println("Duke's number: " + randomInt);
    }

    public void setUserNumber(Integer user_number) {
        userNumber = user_number;
    }

    public Integer getUserNumber() {
        return userNumber;
    }

    public String getResponse() {
        if ((userNumber == null) || (userNumber.compareTo(randomInt) != 0)) {
            return "Sorry, " + userNumber + " is incorrect.";
        } else {
            return "Yay! You got it!";
        }
    }

    public int getMaximum() {
        return (this.maximum);
    }

    public void setMaximum(int maximum) {
        this.maximum = maximum;
    }

    public int getMinimum() {
        return (this.minimum);
    }

    public void setMinimum(int minimum) {
        this.minimum = minimum;
    }
}

UserNameBean:

package com.db.nsw.izt;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class UserNameBean {
    private String fName = "";
    private String lName = "";

    public String getfName() {
        return fName;
    }
    public void setfName(String fName) {
        this.fName = fName;
    }
    public String getlName() {
        return lName;
    }
    public void setlName(String lName) {
        this.lName = lName;
    }

    public String submitRequest(){
        System.out.println("@@@ " + fName + " | " + lName);
        return "response";
    }
}


UPDATE 1:

My environment details: JEE6, JSF 2.0, Mojara, WL 12.1.2 (glassfish.jsf_1.0.0.0_2-1-20.jar)

hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • And you are using jsf x.y? Mojarra? MyFaces? – Kukeltje May 24 '18 at 10:19
  • And can you make a real [mcve] (remove all code not related to this issue) I'd like to check. And you are btw using old namespaces https://stackoverflow.com/questions/31068678/which-xml-namespace-to-use-with-jsf-2-2 (I assume you are using jsf 2.2 since you refer to javaee 7 docs) – Kukeltje May 24 '18 at 10:23
  • @Kukeltje Please refer my update, I have cleaned up some code jargon. Old namepsace because I am using JSF 2.0, and this thing remains same in documentation, this from JEE 6 docs - https://docs.oracle.com/javaee/6/tutorial/doc/bnarf.html#bnari – hagrawal7777 May 24 '18 at 10:33
  • Then edit the Q and add the right link there. You see how things can confuse people (and indirectly waste my time, sort of ;-)) Oh and JSF 2.0 is an API version... What impl? What specific version? Tried a later version of your implementation since JSF 2.1 and 2.2 have been released (so it might have been fixed in the mean time)... – Kukeltje May 24 '18 at 10:37
  • @Kukeltje I have updated my question. I don't think that implementation version would matter a lot because this is not something which will change across implementation or API version, I think this is problem with the way it is put up in documentation, so I wanted to confirm. – hagrawal7777 May 24 '18 at 10:47
  • Ok, good luck then... Maybe reading the full specs helps) (and keep in mind that implementations CAN have bugs so behavipur might chamge) and assumptions (thinking instead of knowing) are the mother of all... ;-0 – Kukeltje May 24 '18 at 10:51

0 Answers0