user1594895
user1594895

Reputation: 587

JSF correct use of dependency

JSF 2.1 Tomcat 7.0

Is this a wrong use of dependency injection?

It works ok moving around the page. But "it works" is not the same as "it's correct".

I would use this pattern also for search purpose. I have a request and i like to use it to populate a table in the same page. Is this possible?

index.xhtml

<?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">
    <h:head>
        <link rel="SHORTCUT ICON" href= "resources/img/onlyT.ico"></link>
        <title>title</title>
        <h:outputStylesheet library="css" name="compass.css"/>
    </h:head>
    <h:body>
        Login system
        <br />

        <h:form>
            User : <h:inputText value="#{user.username}" />
            Password : <h:inputSecret value="#{user.password}" />
            <h:commandButton action="#{loginBean.performLogin()}" value="Submit" />
            <h:commandButton value="reset" type="reset" />
            <h:commandButton value="ChangePsW" action="changePassword"></h:commandButton>
        </h:form>

        <h:message for="" style="color:red;margin:8px;"/>

    </h:body>
</html>

LoginBan.java

@ManagedBean
@RequestScoped
public class LoginBean {


    @ManagedProperty(value="#{user}")
    private UserBean userBean;

    //must povide the setter method
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public LoginBean() {}


    public String performLogin(){


        //Mi connetto al db
        if(userBean.getUsername().equalsIgnoreCase( "mario")){

        //effettuo i controlli per stabilire se esiste l'utente

        userBean.setRoles("-EE-E-E-E-E-E");
        return "/mainPortal/mainPortal";
        }
        return "/mainPortal/index";

    }
}

UserBean.java

@ManagedBean (name="user")
@SessionScoped 
public class UserBean implements Serializable  {

private String Username;
private String Password;
private String Roles;

/** Creates a new instance of UserBean */
public UserBean() {
}


/**
 * @return the Username
 */
public String getUsername() {
    return Username;
}


/**
 * @param Username the Username to set
 */
public void setUsername( String Username ) {
    this.Username = Username;
}


/**
 * @return the Password
 */
public String getPassword() {
    return Password;
}


/**
 * @return the Roles
 */
public String getRoles() {
    return Roles;
}


/**
 * @param Roles the Roles to set
 */
public void setRoles( String Roles ) {
    this.Roles = Roles;
}


/**
 * @param Password the Password to set
 */
public void setPassword( String Password ) {
    this.Password = Password;
}

change password also use the userBean and has his changePasswordBean.

Upvotes: 0

Views: 90

Answers (1)

Aritz
Aritz

Reputation: 31669

JSF 2 allows you injecting managed beans in other managed beans. This is only limited by the scope of the bean you're actually injecting, which has to be greater than the scope of the bean you actually are in. I mean, you can inject @SessionScoped bean in a @ViewScoped one, but not in the other way. As you follow that convention, I think you're doing it well.

Generally, your application should be composed by @ViewScoped or @RequestScoped beans to handle current user input/outputs and wider scoped beans for session or application means. So, when user logs in, it's a good idea to maintain his data in a session context, however I suggest you not to maintain user's password into session, at least if you're not going to use it once the login has been succesfully done.

Finally the question you make about search requests, I think you can implement a search input in your page and make your result table load dinamically depending on the search. Just use ajax to obtain it without having to reload the whole page. You can implement everything in a @ViewScoped bean, but remember not to return navigation results in your listener methods if you want to maintain the bean working.

Upvotes: 1

Related Questions