【问题标题】:JSF reveal form with AJAXJSF 用 AJAX 显示表单
【发布时间】:2016-04-23 10:32:25
【问题描述】:

我想知道这是否可能。我有一个从用户那里接收 ID 和密码的登录表单。如果它在后端签出,则会显示另一种形式。到目前为止,我实际上正在将登录详细信息发送到另一个要处理的 xhtml 页面。但是,如果用户错误,页面仍然显示。

这是我的代码

索引.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://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Index</title>
</h:head>
<h:body>
    <h:form>
        <p>Login</p>
        <h:inputText value="#{jsfbean.username}"/>
        <h:inputText value="#{jsfbean.password}"/>
        <h:commandButton action="#{jsfbean.message}" value="Results Page"/>
    </h:form>
</h:body>
</html>

导航规则

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
          xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">


<navigation-rule>
    <from-view-id>>index.xhtml</from-view-id>
    <navigation-case>
        <from-action>#{jsfbean.getMessage}</from-action>
        <from-outcome>Success</from-outcome>
        <to-view-id>result.xhtml</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-action>#{jsfbean.getMessage}</from-action>
        <from-outcome>Fail</from-outcome>
        <to-view-id>index.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

jsf 豆

 public String getMessage() {
    if(getUser(this.username,this.password)==true){
        this.userID = aUser.getUserid();
        this.secqn = aUser.getSecqn();
        this.secans = aUser.getSecans();
        return "Success";
    }else{
        return "Fail";
    }
}

【问题讨论】:

    标签: ajax jsf jsf-2


    【解决方案1】:

    当用户输入他的用户名和密码时,您只是重定向到另一个页面。

    1)您需要修改您的操作:

     <h:form>
            <p>Login</p>
            <h:inputText value="#{jsfbean.username}"/>
            <h:inputText value="#{jsfbean.password}"/>
            <h:commandButton action="#{jsfbean.authenticateUser}" value="Results Page"/>
        </h:form>
    

    2) 在你的 bean 中编写 action 方法

        public String authenticateUser(){
        if (username!=null&&password!=null) {
            // Write your business logic here
            if (username.equals("username")&&password.equals("password")) {
                return "SUCCESS";// In your case it is result
            }
        }
        return "LOGIN";
    }
    

    3)添加导航规则

        <navigation-rule>
                <from-view-id>/pages/*</from-view-id>
                <navigation-case>
                    <from-outcome>LOGIN</from-outcome>
                    <to-view-id>/pages/public/login.xhtml</to-view-id>
                </navigation-case>
                <navigation-case>
                    <from-action>#{authenticateController.authenticateUser}</from-action>
                    <from-outcome>LOGIN</from-outcome>
                    <to-view-id>/pages/public/login.xhtml</to-view-id>
                </navigation-case>
                <navigation-case>
                    <from-action>#{authenticateController.authenticateUser}</from-action>
                    <from-outcome>SUCCESS</from-outcome>
                    <to-view-id>/pages/private/home.xhtml</to-view-id>
                    <redirect/>
                </navigation-case>
    </navigation-rule>
    

    【讨论】:

    • 谢谢!但是这个导航规则是适用于 JSF 1 还是我也可以将它用于 2?
    • 您也可以将它用于 JSF 2。
    • 对不起,我正在实现这个并且收到了一个错误 javax.el.MethodNotFoundException: /index.xhtml @13,80 action="#{jsfbean.message}": Method未找到:stateless.jsfbean@573e86b7.message()。我已经编辑了我的问题。
    • 你定义过什么方法叫message吗?
    • 请更改
    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 2011-03-01
    • 2011-05-30
    • 2011-08-31
    • 2011-09-22
    • 2013-02-27
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多