Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF - How does the JSF implementation identify the action?
    primarykey
    data
    text
    <p>I am trying to understand how JSF implementation identifies various possible actions from the user. In the simple application I've put together, I have the following fields configured in the <code>login.xhtml</code> page.</p> <p><code>User name - input field</code></p> <p><code>Password - password field</code></p> <p><code>Login button</code></p> <p><code>Cancel button</code></p> <p><strong>login.xhtml</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html 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"&gt; &lt;h:head id="head_id" &gt;&lt;/h:head&gt; &lt;h:body id="body_id"&gt; &lt;h:form id="loginForm_id"&gt; &lt;h:panelGrid id="loginPanelGrid_id"&gt; &lt;h:outputText id="nameLabel_id" value="Name"&gt;&lt;/h:outputText&gt; &lt;h:inputText id="nameInput_id" value="#{loginBean.name}"&gt;&lt;/h:inputText&gt; &lt;h:outputText id="passwordLabel_id" value="Password"&gt;&lt;/h:outputText&gt; &lt;h:inputSecret id="passwordInput_id" value="#{loginBean.password}"&gt;&lt;/h:inputSecret&gt; &lt;/h:panelGrid&gt; &lt;h:commandButton id="loginBtn_id" value="Do Login" action="#{loginBean.login}"&gt;&lt;/h:commandButton&gt; &lt;h:commandButton id="CancelBtn_id" value="Cancel Login" action="#{loginBean.cancel}"&gt;&lt;/h:commandButton&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><strong>LoginBean.java</strong></p> <pre><code>package com.ila; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class LoginBean { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login() { if ("a".equals(name)) return "success"; else return "failure"; } public String cancel() { return "cancel"; } } </code></pre> <p><strong>Generated html code from login.xhtml</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;form id="loginForm_id" name="loginForm_id" method="post" action="/JSF2TestProject/faces/simple.xhtml" enctype="application/x-www-form-urlencoded"&gt; &lt;input type="hidden" name="loginForm_id" value="loginForm_id" /&gt; &lt;table id="loginForm_id:loginPanelGrid_id"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span id="loginForm_id:nameLabel_id"&gt;Name&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input id="loginForm_id:nameInput_id" type="text" name="loginForm_id:nameInput_id" value="a" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;span id="loginForm_id:passwordLabel_id"&gt;Password&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input id="loginForm_id:passwordInput_id" type="password" name="loginForm_id:passwordInput_id" value="" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;input id="loginForm_id:loginBtn_id" type="submit" name="loginForm_id:loginBtn_id" value="Do Login" /&gt; &lt;input id="loginForm_id:CancelBtn_id" type="submit" name="loginForm_id:CancelBtn_id" value="Cancel Login" /&gt; &lt;input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAAI1QTUoDMRj9Ou1QW2ztDwouBBduVJgD6MIiWCwqFUQRXWicie2UTBKTzHS6KR7BAwhewEuIawUXbryDHsCVSR2nuij4Qd6XfCHvvbz7d7C5FFDtoQg5ofKJs41kdw9xO//28Dh3/pIFqwlFwpDXRK5iogUF1RVYdhnxYr7RAFPT/SmNFb0sBQuEdXzaZCI48701jqTsM+G1KA+VHoQC6qe7IzmCaMdpX/Swq9Zvno/vKnKZWAAx1zy58AqGkDWM6c7mutJTfihg1fDEziVysXRcFnBGMVXOYasdKi23tC8Yx0INdvBAQlI1rSCgPHawRcPg9yVXYEeIhDgSkIuY78G4Yq5VVyapjv44UdQYL3xz+16a+cjCJmMEI/q0KK5fbz8/LMicJB5inhmah2UFJcJcRI7M9AAraaZV4Nz0Wa5Nz//JnaIAp5n/ZPaPVM2maKBkYMZAzUA90Ula/AXEIFMaOgIAAA==" autocomplete="off" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>faces-config.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;faces-config 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-facesconfig_2_1.xsd" version="2.1"&gt; &lt;navigation-rule&gt; &lt;display-name&gt;login.xhtml&lt;/display-name&gt; &lt;from-view-id&gt;/login.xhtml&lt;/from-view-id&gt; &lt;navigation-case&gt; &lt;from-outcome&gt;success&lt;/from-outcome&gt; &lt;to-view-id&gt;/welcome.xhtml&lt;/to-view-id&gt; &lt;/navigation-case&gt; &lt;/navigation-rule&gt; &lt;navigation-rule&gt; &lt;display-name&gt;login.xhtml&lt;/display-name&gt; &lt;from-view-id&gt;/login.xhtml&lt;/from-view-id&gt; &lt;navigation-case&gt; &lt;from-outcome&gt;failure&lt;/from-outcome&gt; &lt;to-view-id&gt;/login.xhtml&lt;/to-view-id&gt; &lt;/navigation-case&gt; &lt;/navigation-rule&gt; &lt;navigation-rule&gt; &lt;display-name&gt;login.xhtml&lt;/display-name&gt; &lt;from-view-id&gt;/login.xhtml&lt;/from-view-id&gt; &lt;navigation-case&gt; &lt;from-outcome&gt;cancel&lt;/from-outcome&gt; &lt;to-view-id&gt;/login.xhtml&lt;/to-view-id&gt; &lt;/navigation-case&gt; &lt;/navigation-rule&gt; &lt;/faces-config&gt; </code></pre> <p><strong>UI rendered (screen shot)</strong></p> <p><img src="https://i.stack.imgur.com/eA81y.jpg" alt="enter image description here"></p> <p><strong>Question 1</strong></p> <p>Given that the generated html does NOT have any parameter sent that identifies which button is clicked (atleast I can't see), how does the JSF implementation determine if the 'Do Login' button is clicked or the 'Cancel Login' button is clicked?</p> <p><strong>Question 2</strong></p> <p>What is the purpose of the two hidden (generated by JSF implementation) fields (shown below)? Are these related to the question 1?</p> <pre><code>&lt;input type="hidden" name="loginForm_id" value="loginForm_id" /&gt; &lt;input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="H4sIAAAAAAAAAI1QTUoDMRj9Ou1QW2ztDwouBBduVJgD6MIiWCwqFUQRXWicie2UTBKTzHS6KR7BAwhewEuIawUXbryDHsCVSR2nuij4Qd6XfCHvvbz7d7C5FFDtoQg5ofKJs41kdw9xO//28Dh3/pIFqwlFwpDXRK5iogUF1RVYdhnxYr7RAFPT/SmNFb0sBQuEdXzaZCI48701jqTsM+G1KA+VHoQC6qe7IzmCaMdpX/Swq9Zvno/vKnKZWAAx1zy58AqGkDWM6c7mutJTfihg1fDEziVysXRcFnBGMVXOYasdKi23tC8Yx0INdvBAQlI1rSCgPHawRcPg9yVXYEeIhDgSkIuY78G4Yq5VVyapjv44UdQYL3xz+16a+cjCJmMEI/q0KK5fbz8/LMicJB5inhmah2UFJcJcRI7M9AAraaZV4Nz0Wa5Nz//JnaIAp5n/ZPaPVM2maKBkYMZAzUA90Ula/AXEIFMaOgIAAA==" autocomplete="off" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload