Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are lot of misconceptions with your current approach. I would recommend to first learn and grasp the basic concepts of JSF to accomplish this.</p> <p>In order to make this work, you need a single bean that needs to be alive only for each request to the server. This bean will have the responsibility to get the data from the JSF form and execute the validation action. Then, you just have to bind the fields and login method to your JSF form.</p> <p>Java code</p> <pre><code>//Looks like you use CDI (or probably you just think this is what you need) @Named //The bean just needs to be alive on each request //Since you're using CDI, make sure this annotation comes from //javax.enterprise.context package @RequestScoped //Usually, the managed beans used in JSF are named with "Bean" suffix //Since it is for learning purposes, I'll name this bean UserBean public class UserBean { //fields to hold username and password //they both are Strings private String username; private String password; //getters and setters methods //I won't add them since it is boilerplate code for this sample //... //Method to handle your login action //If successful login, then returns the name of the next view //If unsuccessful, returns null to stay in the current view public String login() { //Sample implementation, remember to change it for A REAL ONE if ("admin".equals(username) &amp;&amp; "adminPass".equals(password)) { //Successful login //Make sure to have a file index.xhtml in the same folder //where the login JSF page resides (for sample purposes) return "index"; } //Unsuccessful login //Also, it is a good idea to show a message for end user FacesMessage message = new FacesMessage("Invalid login."); FacesContext.getCurrentInstance().addMessage(null, message); return null; } } </code></pre> <p>JSF code</p> <pre class="lang-html prettyprint-override"><code>&lt;h:form&gt; &lt;h3&gt;Login Title&lt;/h3&gt; &lt;h:panelGrid columns="2"&gt; &lt;h:outputText value="User Id" /&gt; &lt;h:inputText value="#{userBean.username}" /&gt; &lt;h:outputText value="Password" /&gt; &lt;h:inputSecret value="#{userBean.password}" /&gt; &lt;/h:panelGrid&gt; &lt;h:commandButton value="Login" action="#{userBean.login}" /&gt; &lt;br /&gt; &lt;h:messages /&gt; &lt;/h:form&gt; </code></pre> <p>Some things you <strong>need</strong> to know before going on:</p> <ul> <li>JSF beans <strong>are not the same</strong> as CDI beans. This is explained better here: <a href="https://stackoverflow.com/q/4347374/1065197">Backing beans (@ManagedBean) or CDI Beans (@Named)?</a>. I recommend reading both Bozho and BalusC's answers.</li> <li>Define the scope of your managed beans. One of the main problems JSF beginners confront is to choose the right scope for the bean. For example, you started marking the bean as <code>@ApplicationScoped</code> which means there's a single User bean for <strong>the entire application</strong>, which means two different end users will have the same user object (now you start worring about). You can learn more about this here: <a href="https://stackoverflow.com/q/7031885/1065197">How to choose the right bean scope?</a></li> <li>As you may see from link above, there's a view scope. If you're working with Java EE 7, then you will find this scope available for your CDI beans as well. If you're working with Java EE 6, then you'll find that there's no annotation for this. But don't worry, <a href="http://omnifaces.org" rel="nofollow noreferrer">OmniFaces</a> solves this since its version 1.6 with a custom <a href="http://showcase.omnifaces.org/cdi/ViewScoped" rel="nofollow noreferrer"><code>@ViewScoped</code></a> annotation from <code>org.omnifaces.cdi</code> package.</li> <li>Since you're starting with JSF/CDI, I recommend using a single bean per view. When you have more experience, you may use two or more beans in the same view. Here's good example: <a href="https://stackoverflow.com/q/18919545/1065197">Can I use multiple managed bean in the same xhtml page?</a></li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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