JSF page onload executes button click

自闭症网瘾萝莉.ら 提交于 2019-12-11 23:13:34

问题


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>

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.


回答1:


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

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()}"/>

You're also misusing onload on you h:body



来源:https://stackoverflow.com/questions/25704443/jsf-page-onload-executes-button-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!