JSF页面onload执行按钮单击 [英] JSF page onload executes button click

查看:120
本文介绍了JSF页面onload执行按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的JSF页面,我检查用户是否在body onload事件上登录.

I have a following JSF page and I check if user is logged in on body onload event.

    <h:body onload="#{adminManagedBean.adminIsLoggedIn()}">
    <div id="top-bar" class="pure-g">
        <div class="pure-u-1-2">
            <img src="images/logo.png" border="0" style="margin-left: 20px;"/>
        </div>
        <div class="pure-u-1-2">
            <div class="pure-menu pure-menu-open pure-menu-horizontal">
                <ul>
                    <li><a href="addProduct.xhtml">Add product</a></li>
                    <li><a href="products.xhtml">Products</a></li>
                    <li><a href="orders.xhtml">Orders</a></li>
                    <li><h:button value="Logout" onclick="#{adminManagedBean.logoutUser()}"></h:button></li>

我的问题是,为什么页面加载时用户会自动注销?我调试了应用程序,即使未单击按钮,打开页面时也会调用logoutUser方法.未在adminIsLoggedIn()方法中调用logoutUser方法.如果用户未登录,则adminIsLoggedIn仅重定向页面.

My question is why is user automaticaly logged out when the page loads? I debuged the app and logoutUser method is called when the page is opened even though the button isn't clicked. The logoutUser method in not called in the adminIsLoggedIn() method. adminIsLoggedIn simply redirects the page if the user is not logged in.

推荐答案

您正在将javascript属性与服务器端属性混淆.例如. onclick是单击组件时执行的javascript.

You're confusing javascript attribute with server side attribute. eg. onclick is a javascript executed when the component is clicked.

您遇到的情况是,onclick="#{adminManagedBean.logoutUser()}"在服务器端评估adminManagedBean.logoutUser()会导致注销.

What happens on you case is that onclick="#{adminManagedBean.logoutUser()}" evaluate adminManagedBean.logoutUser() on the server side which make the logout.

如果使用,它将得到解决

It'll be fixed if you use:

<h:commandButton value="Logout" action="#{adminManagedBean.logoutUser()}"/>

您还滥用了h:body上的加载

You're also misusing onload on you h:body

这篇关于JSF页面onload执行按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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