java - 如何将参数传递给 JSF 2.0 页面上的 javascript 函数

标签 java javascript jakarta-ee jsf-2 primefaces

我正在制作一个自定义登录对话框,类似于 http://www.primefaces.org/showcase/ui/dialogLogin.jsf 上找到的对话框但我在构建时遇到错误:

javax.el.PropertyNotFoundException: /WEB-INF/templates/BasicTemplate.xhtml @58,83 oncomplete="handleLoginRequest(#{securityController.logIn})": The class 'managedbeans.SecurityController' does not have the property 'logIn'.

我认为错误在于我尝试触发日志记录程序的方式。有人可以看看我的语法是否正确吗?

  <p:dialog id="dialog" header="Login" widgetVar="dlg" modal="true" width="400" resizable="false" draggable="false" fixedCenter="true">  
<h:form>    
    <h:panelGrid columns="2" cellpadding="5">  
        <h:outputLabel for="username" value="Em@il: *" />  
        <p:inputText value="#{securityController.email}"   
                id="email" required="true" label="email" />  

        <h:outputLabel for="password" value="Password: * " />  
        <h:inputSecret value="#{securityController.password}"   
                id="password" required="true" label="password" />  

        <f:facet name="footer">  
            <p:commandButton value="Login" update="growl"                       
                oncomplete="handleLoginRequest(#{securityController.logIn})"/>  
        </f:facet>  
    </h:panelGrid>           
</h:form>  
</p:dialog> 
<script type="text/javascript">  
function handleLoginRequest(input) {  
    if(input == false) {  
        jQuery('#dialog').parent().effect("shake", { times:3 }, 100);  
    } else {  
        dlg.hide();  
        jQuery('#loginLink').fadeOut();  
    }  
 }  
</script> 

这是托管 bean,它保存正在调用 onComplete 事件的方法:

@ManagedBean
@RequestScoped
public class SecurityController {

@EJB
private IAuthentificationEJB authentificationEJB;
private String email;
private String password;
private String notificationValue;   

public void logIn() {
    if(authentificationEJB.saveUserState(email, password)) {
        notificationValue = "Dobro dosli";
    }   
}
    //getters and setters...

最佳答案

您必须从 p:commandButtonactionListener 属性调用登录方法,而不是在 oncomplete 属性内调用。示例中的 javascript 函数无需更改即可使用。仅用于展示效果。

按如下方式更改您的 p:commandButton:

<p:commandButton value="Login" update="growl"                       
       oncomplete="handleLoginRequest(xhr, status, args)"
       actionListener="#{securityController.logIn}"
/>

并按照 primefaces 展示中的方式使用 js 函数:

<script type="text/javascript">  
    function handleLoginRequest(xhr, status, args) {  
        if(args.validationFailed || !args.loggedIn) {  
            jQuery('#dialog').parent().effect("shake", { times:3 }, 100);  
        } else {  
            dlg.hide();  
            jQuery('#loginLink').fadeOut();  
        }  
    }  
</script>  

关于java - 如何将参数传递给 JSF 2.0 页面上的 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5647224/

相关文章:

java - Eclipse 找不到 JFace 中的主类?

javascript - 我正在尝试解析从 firebase 查询中获取的数据,但数据没有得到解析

java.lang.NoClassDefFoundError : Lorg/apache/commons/collections/FastHashMap; 错误

java - 如何检查用户是否第一次使用我的应用程序

java - Unresolved 类 'FileProvider'

javascript - Angular-translate,翻译数据库中的内容

java - 为什么Java JDBC连接池在部署时要校验

java - 我可以使用普通的java类文件形式的web.xml文件读取上下文参数吗

java - Guice - "dynamic"绑定(bind)?

javascript - Backbone.js 多表数据操作