0

JSF 2.1 雄猫 7.0

这是对依赖注入的错误使用吗?

它可以在页面上移动。但是“它有效”与“它是正确的”不同。

我也会将此模式用于搜索目的。我有一个请求,我喜欢用它来填充同一页面中的表格。这可能吗?

索引.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";

    }
}

用户Bean.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;
}

更改密码也使用userBean,并拥有他的changePasswordBean。

4

1 回答 1

1

JSF 2 允许您在其他托管 bean 中注入托管bean。这仅受您实际注入的 bean 的范围的限制,它必须大于您实际所在的 bean 的范围。我的意思是,您可以@SessionScoped在其中注入 bean @ViewScoped,但不能以另一种方式注入。当您遵循该约定时,我认为您做得很好。

通常,您的应用程序应该由处理当前用户输入/输出的 bean 和用于会话或应用程序手段的更广泛范围的 bean@ViewScoped组成。@RequestScoped因此,当用户登录时,最好在会话上下文中维护他的数据,但是我建议您不要在会话中维护用户的密码,至少如果您在成功登录后不打算使用它.

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.

于 2013-02-10T22:28:29.683 回答