【问题标题】:How to pass a parameter to a javascript function on a JSF 2.0 page如何将参数传递给 JSF 2.0 页面上的 javascript 函数
【发布时间】:2011-08-04 13:40:50
【问题描述】:

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

javax.el.PropertyNotFoundException: /WEB-INF/templates/BasicTemplate.xhtml @58,83 oncomplete="handleLoginRequest(#{securityController.logIn})": 类'managedbeans.SecurityController'没有属性'登录。

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

  <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...

【问题讨论】:

  • 您可以将参数而不是方法传递给javascript

标签: java javascript jakarta-ee jsf-2 primefaces


【解决方案1】:

您必须从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>  

【讨论】:

  • @sfrj with your case #{securityController.logIn} 这将在 securityController 控制器 bean 中搜索 getLogIn(),您将其作为值传递。 @马特+1
  • 我这样做了,现在我没有得到构建错误。但是当我点击提交按钮时,我得到一个错误。我认为这与EJB有关。我会更新给你看。
【解决方案2】:

如果您想在 javascript 事件上执行方法,请使用 &lt;a4j:jsfunction&gt;

例如here

【讨论】:

  • 您忘记了示例的链接
  • 还有其他选择吗,我不想使用richfaces。我过去曾与 Primefaces + Rich faces 发生冲突
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 2011-10-02
  • 2016-01-16
相关资源
最近更新 更多