Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><sup><em>(this was originally posted on <a href="https://stackoverflow.com/questions/10691324/working-with-3-java-beans-controller-backing-model">https://stackoverflow.com/questions/10691324/working-with-3-java-beans-controller-backing-model</a>, but the OP deleted the question and I didn't want to throw away the answer, I think a repost on this question is suitable as well)</em></sup></p> <p>You were probably focusing too much on this <a href="http://java.dzone.com/articles/making-distinctions-between" rel="nofollow noreferrer">ICEfaces blog</a> which contains <a href="https://stackoverflow.com/questions/7223055/distinction-between-different-types-of-managed-beans/7223910#7223910">mainly nonsense</a>.</p> <p>You should have a single JSF managed bean which acts as a controller, you already have it: <code>UserController</code>. You should have a simple entity bean which represents the model, you already have it: <code>UserVO</code>. You should have an enterprise bean which represents the service, you already have it: <code>UserBO</code>. Those last two doesn't need to be JSF managed beans.</p> <p>Based on your question history you're using Glassfish, thus you can make use of JPA and EJB. So the model class should be a JPA <code>@Entity</code> and the service class should be a <code>@Stateless</code> EJB.</p> <p>The use case of a "login user" is however special. You don't want to have the entity as a session scoped managed bean, or it would be implicitly created by JSF. You'd better put it straight in the session scope yourself, so that you can check on <code>#{not empty user}</code> if the user is logged in or not.</p> <p>All with all it should look something like this:</p> <pre class="lang-java prettyprint-override"><code>@Entity public class User { private String username; private String password; // ... } </code></pre> <pre class="lang-java prettyprint-override"><code>@Stateless public class UserService { @PersistenceContext private EntityManager em; public User find(String username, String password) { return em.createQuery("SELECT u FROM User u WHERE username = :username AND password = MD5(:password)", User.class) .setParameter("username", username) .setParameter("password", password) .getSingleResult(); } } </code></pre> <pre class="lang-java prettyprint-override"><code>@ManagedBean @ViewScoped public class UserController { private String username; private String password; @EJB private UserService service; public String login() { FacesContext context = FacesContext.getCurrentInstance(); try { User user = userService.find(username, password); context.getExternalContext().getSessionMap().put("user", user); return "/views/commons/home.html?faces-redirect=true"; } catch (NoResultException) { context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unknown login, please try again", null)); return null; } } public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "/views/commons/login.html?faces-redirect=true"; } // ... } </code></pre> <p>with</p> <pre><code>&lt;h:form&gt; &lt;h:inputText value="#{userController.username}" required="true" /&gt; &lt;h:inputSecret value="#{userController.password}" required="true" /&gt; &lt;h:commandButton value="login" action="#{userController.login}"/&gt; &lt;h:messages /&gt; &lt;/h:form&gt; </code></pre> <p>Alternatively, you can make the <code>UserController</code> a session scoped bean which holds the <code>User</code> entity, you'd only need to add an extra method to check if the user is logged in or not so that you can use it in EL by <code>#{userController.loggedIn}</code>.</p> <pre class="lang-java prettyprint-override"><code>@ManagedBean @SessionScoped public class UserController { private User user = new User(); @EJB private UserService service; public String login() { FacesContext context = FacesContext.getCurrentInstance(); try { user = userService.find(user.getUsername(), user.getPassword()); return "/views/commons/home.html?faces-redirect=true"; } catch (NoResultException) { context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unknown login, please try again", null)); return null; } } public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "/views/commons/login.html?faces-redirect=true"; } public boolean isLoggedIn() { return user.getId() != null; } // ... } </code></pre> <p>with</p> <pre><code>&lt;h:form&gt;     &lt;h:inputText value="#{userController.user.username}" required="true" /&gt;     &lt;h:inputSecret value="#{userController.user.password}" required="true" /&gt;     &lt;h:commandButton value="login" action="#{userController.login}"/&gt;     &lt;h:messages /&gt; &lt;/h:form&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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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