如何修改我的登录页面以从数据库中检索值

时间:2013-11-07 11:38:16

标签: jsf jpa derby

我创建了一个非常简单的登录表单,用户输入用户名和密码,然后检查存储在支持bean中的值,如果匹配则会给你一条欢迎信息。

这很好用,但是我想添加数据库的功能,以允许更多用户和更好的安全性。

我使用内置的netbeans wizzard创建了一个连接到数据库(derby)的简单Web应用程序,如我在此处找到的:http://www.youtube.com/watch?v=tql4COiN5T0

我能够从数据库中检索数据,编辑它以查看它并将其销毁,这些功能的大部分将在最后删除。但是,我想知道的是,我怎么能不去支持bean来搜索正确的值来搜索derby数据库呢?

这是我当前的facelet页面:

    <h:form>
        <h:panelGrid columns="2">
            <h:outputText value="Name"></h:outputText>
            <h:inputText value="#{loginTest.username}"></h:inputText>
            <h:outputText value="Password"></h:outputText>
            <h:inputSecret value="#{loginTest.password}"></h:inputSecret>
        </h:panelGrid>
        <h:commandButton value="Login" action="second"></h:commandButton>
    </h:form>

这是一个我可以检索数据库中所有用户的页面:

<h:dataTable value="#{logindetailsController.items}" var="item" border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px">
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListLogindetailsTitle_id}"/>
                            </f:facet>
                            <h:outputText value="#{item.id}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListLogindetailsTitle_username}"/>
                            </f:facet>
                            <h:outputText value="#{item.username}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListLogindetailsTitle_password}"/>
                            </f:facet>
                            <h:outputText value="#{item.password}"/>
                        </h:column>
                        <h:column>
                            <f:facet name="header">
                                <h:outputText value="&nbsp;"/>
                            </f:facet>
                            <h:commandLink action="#{logindetailsController.prepareView}" value="#{bundle.ListLogindetailsViewLink}"/>
                            <h:outputText value=" "/>
                            <h:commandLink action="#{logindetailsController.prepareEdit}" value="#{bundle.ListLogindetailsEditLink}"/>
                            <h:outputText value=" "/>
                            <h:commandLink action="#{logindetailsController.destroy}" value="#{bundle.ListLogindetailsDestroyLink}"/>
                        </h:column>
                    </h:dataTable>

我有一个支持bean,我可以检索所有用户名和密码。我如何在这个bean中搜索正确的用户名和密码?

这是实体:

@Entity
@Table(name = "LOGINDETAILS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Logindetails.findAll", query = "SELECT l FROM Logindetails l"),
    @NamedQuery(name = "Logindetails.findById", query = "SELECT l FROM Logindetails l WHERE l.id = :id"),
    @NamedQuery(name = "Logindetails.findByUsername", query = "SELECT l FROM Logindetails l WHERE l.username = :username"),
    @NamedQuery(name = "Logindetails.findByPassword", query = "SELECT l FROM Logindetails l WHERE l.password = :password")})
public class Logindetails implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID")
    private Short id;
    @Size(max = 20)
    @Column(name = "USERNAME")
    private String username;
    @Size(max = 20)
    @Column(name = "PASSWORD")
    private String password;

// getters and setters 

1 个答案:

答案 0 :(得分:0)

如果我理解你,你应该在适当的托管bean中实例化实体,在这样的视图中使用它:

<h:form>
    ...
        <h:inputText value="#{loginTest.logindet.username}"></h:inputText>
    ...
        <h:inputSecret value="#{loginTest.logindet.password}"></h:inputSecret>

    <h:commandButton value="Login" action="second"></h:commandButton>
</h:form>

使用:

@ManagedBean
@SessionScoped
public class LoginTest implements Serializable {

    public Logindetails logindet;


    public LoginTest(){}

    public String login(){
        // login process
    }
 }