以jsf格式输入时出错 [英] error in taking input in jsf form

查看:52
本文介绍了以jsf格式输入时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载登录页面后,输入文本框将显示#{ad.userid}.当我擦除它并输入id和pwd并单击Submit按钮时,将调用登录方法,但userid属性在bean中给出了空值.这种情况怎么发生?我该如何解决?

When login page is loaded the input text box is displaying #{ad.userid}. When I erased it and entered id and pwd and clicked submit button the login method is called but userid property is giving null value in bean. How can this happen and how can I solve it?

这是login.jsp:

<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<f:view>
<html>

        <h:form>
            <h:outputText value="Login"/>
            <h:inputText value="#{ad.userid}" id="log" required="true"/>
            <h:outputText  value="Password" />
            <h:inputSecret id="pw" value="#{ad.password}" required="true"/>
            <h:commandButton  value="submit" action="#{ad.login}"/>
        </h:form>
    </body>
</html>
</f:view>

这是bean的操作方法.该登录之前已成功运行.所有数据库连接都在构造函数中设置.

Here is the bean's action method. The login worked successfully earlier. All db connections are set in constructor.

public String login() {
    ResultSet rs;
    try {
        System.out.println(userid); // this is giving null
        String s = "select id from slogin where id='" + userid + "'";
        System.out.println(s);
        rs = st.executeQuery(s);
        if (rs.next()) {
            String loginid = rs.getString(1);
            if (userid.equals(loginid)) {
                id = loginid;
                return "studhome";
            }
        } else {
            System.out.println("error");
        }
    } catch(Exception e) {

    }
}

Getter和setter方法:

Getter and setter methods:

public void setpassword(String pass) {
    this.password = pass;
}

public String getpassword() {
    return password;
}

public void setuserid(String uid) {
    this.userid = uid;
}

public String getuserid() {
    return userid;
}

推荐答案

请花点时间编辑您的问题,并使用StackOverflow提供的格式化代码工具对问题进行正确格式化.如果您使用带有符号"0101010101"的按钮,则将出现大括号.

Please, take your time to edit your question, format it properly using the formatting code tools that StackOverflow provides. The braces will appear if you use the button with the symbol "0101010101".

答案:

更改:

public void setuserid(String uid)

public void setUserid(String uid)

还有:

public void setpassword(String pass)

public void setPassword(String pass)

看看 Java命名约定,JSF依靠它们来访问属性托管bean.

Have a look at Java Naming Conventions, JSF relies on them to access the properties of a managed bean.

顺便说一句,尝试将您的支持bean设置为Session范围以查看其是否有效,以丢弃其他问题

Btw, try to set your backing bean to Session scope to see if it works, to discard other problems

这篇关于以jsf格式输入时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆