【问题标题】:Command button does not invoke action method, but only refreshes the page命令按钮不调用action方法,只刷新页面
【发布时间】:2013-05-24 14:35:15
【问题描述】:

我的 index.xhtml 页面包含这个和更多其他小东西,如页脚、页眉、菜单等...:

index.html 包含:

<h:head/>
<h:body>
 <p:layout fullPage="true">
  <p:layoutUnit position="center" >
  <h:panelGroup id="content" layout="block">

    <h:form id="contentform">
<!-- Cas page contact == page affichée lors du clic sur contact -->
     <h:form id="contact">
      <h:panelGroup rendered="#{navBean.page == 'contact'}">
       <ui:include src="contact.xhtml" />
      </h:panelGroup>
     </h:form>
    </h:form>
<!--Fin de gestion des cas possible -->
 </h:panelGroup>
</p:layoutUnit>
<!-- Fin layout central --> 
 </p:layout>
</h:body>

我正在使用 JSF 2.1 和 PrimeFaces。我在contact.xhtml 上有以下命令按钮:

<h:head/>
 <h:body> 
  <p:commandButton value="Envoie la sauce" action="#{mail.sendEmail()}"  >
   <f:ajax execute="@form" render="@form" />
  </p:commandButton>
</h:body>

指的是下面的bean动作:

@ManagedBean(name="mail")
@SessionScoped
public class MailBean {
public void sendEmail() throws IOException {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
                            @Override
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("username","password");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("toto@yopmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("toto@gmail.com"));
        message.setSubject("Demande de Contact");
        message.setText("Dear Mail Crawler," +
                "\n\n No spam to my email, please!");

        Transport.send(message);

                    //to see if message was send
        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
}

这应该通过 JavaMail 发送邮件,但是该按钮除了刷新页面之外没有任何作用。

我已经将 SendHTMLEmail 类作为一个独立的 Java 应用程序进行了测试,它运行良好。

我想做的是使用 JSF2.1 在 xhtml 中制作的简单联系表格。

更新- 好的,今天有消息! 如果我使用这个东西:&lt;p:commandButton action="#{mail.send()}"/&gt; 在页面 index.xhtml 上发生了一些事情,我必须确定为什么找不到该类...:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><error><error-name>class javax.faces.el.EvaluationException</error-name><error-message><![CDATA[/index.xhtml @35,47 action="#{mail.send()}": Target Unreachable, identifier 'mail' resolved to null]]></error-message></error></partial-response>

但是在contact.xhtml上我只有这个和同一个按钮:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="log:messages"><![CDATA[<div id="log:messages" class="ui-messages ui-widget" aria-live="polite"></div>]]></update><update id="javax.faces.ViewState"><![CDATA[-2283025416922274948:-229809047557820449]]></update></changes></partial-response>

所以我所有人都有一个唯一的 id !有人知道会发生什么吗? edit1:添加了完整的代码 edit2:添加了所有应答器的 index.html edit3:在我的 xhtml 页面中发现了一些关于操作按钮的信息

【问题讨论】:

  • 您的代码看起来不错,应该可以工作。请发布一个更真实的示例,说明您正在尝试做什么以找出真正的问题。
  • 请排除stackoverflow.com/questions/2118656/…中所有可能的原因
  • 感谢您提供的链接,我已经纠正了一些错误,例如“您不能将多个 UIForm 组件相互嵌套。嵌套的 h:form 不是我检查过的问题!我正在使用 Netbean如何通过单击检查 ajax 和 javascript 调用 mabe?也许它可以帮助我?
  • 到目前为止的代码不涉及任何 JavaScript/Ajax。如果您避免发布真正的 SSCCE(我无法想象使用 JavaMail 可能会成为问题的原因;通过简单的System.out.println("method invoked!") 替换整个操作方法,很容易将其排除在原因之外。可以你得到了一个“Hello World”的 JSF 表单吗?就像我们的 JSF wiki 页面中显示的那样:stackoverflow.com/tags/jsf/info
  • 您真的拥有&lt;h:head&gt; 部分中的所有组件吗?嗯,应该是&lt;h:body&gt;,你必须使用&lt;h:form&gt;来包装将要发送到服务器的组件(包括&lt;h:commandButton&gt;)。

标签: jsf primefaces facelets


【解决方案1】:

所以在这个问题上停留了几天后,我终于找到了解决方案

   <h:form id="contact">
    <h:panelGroup rendered="#{navBean.page == 'contact'}">
     <ui:include src="contact.xhtml" />
    </h:panelGroup>
   </h:form>

在我的 index.xhtml 中,&lt;h:form&gt;&lt;h:panelgroup&gt; 中,更确切地说在我的 contact.xhtml 中!如果我将这些表单应答器放在&lt;h:panelGroup&gt; 之外并且不将任何表单应答器放在contact.xhtml 中,那么一切正常,单击我的按钮只需调用正确的操作!

我将在我的第一篇文章中发布最终代码! 如果您需要,请随意使用它! 谢谢大家对我的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多