weixin_33674976 2016-04-23 10:32 采纳率: 0%
浏览 51

JSF使用AJAX揭示表单

I was wondering if this is possible. I have a login form that receives an ID and password from a user. If it checks out in the backend, another form is revealed. So far I am actually sending the login details to another xhtml page to be processed. However, if the user is wrong, the page still shows.

This is my code

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://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>

Navigation rule

<?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 bean

 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";
    }
}

Structure

  • 写回答

1条回答 默认 最新

  • weixin_33737774 2016-04-23 12:40
    关注

    You are simply redirecting to another page when user enters his username and password.

    1)You need to modify your action:

     <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) Write the action method inside your bean

        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)Add the navigation rule

        <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大