1

My index.xhtml page contain this and more other little thing like footer, header,menu etc... :

index.html contain :

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

I'm using JSF 2.1 and PrimeFaces. I have the following command button on 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>

It refers to the following bean action:

@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);
    }
}
}

This is supposed to send a mail via JavaMail, however the button doesn't do anything except refreshing the page.

I have tested the SendHTMLEmail class as a stand alone Java Application, it works perfectly.

What i'm trying to do is a simple contact form made in xhtml using JSF2.1.

UPDATE- Ok some news today ! If i use this thing : <p:commandButton action="#{mail.send()}"/> on the page index.xhtml something happen i have to determine why the class can't be find... :

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

BUT on the contact.xhtml i have only this with the same button :

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

So all my have an unique id ! Someone have an idea to what happen ? edit1 : full code added edit2 : added the index.html with all balises edit3 : Discovered something about the action button into my xhtml page

Freest
  • 109
  • 6
  • Your code as-is looks good and should work. Please post a more real example of what you're trying to do in order to find the real problems. – Luiggi Mendoza May 24 '13 at 14:37
  • Please exclude all possible causes from http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked/2120183#2120183 – BalusC May 24 '13 at 14:41
  • Thanks for the link given, i have corrected some mistake done like "You cannot nest multiple UIForm components in each other.Nested h:form was not my problem i have checked ! I'm working on Netbean how can i check ajax&javascript calls mabe by clicking ? maybe it can help me ? – Freest May 24 '13 at 15:01
  • The code as you've so far does not involve any JavaScript/Ajax. If you refrain to post a real SSCCE (I can't imagine that using JavaMail can ever be the cause of the problem; it'd be easily excluded from being the cause by replacing the entire action method by a simple `System.out.println("method invoked!")`. Can you get a "Hello World" JSF form to work? Like the one as shown in our JSF wiki page: http://stackoverflow.com/tags/jsf/info – BalusC May 24 '13 at 15:02
  • 3
    Do you really have all the components in the `` section? Well, it should be `` and you must use a `` to wrap the components that will be sent to the server (including the ``). – Luiggi Mendoza May 24 '13 at 15:14
  • These components are inside a `` tag? – Diogo Moreira May 24 '13 at 17:55
  • I have edited my first post with my index.xhtml code who call my contact.xhtml code who is supposed to call my mail.send() method onclick...The fact is i have inside can it be a problem ? I'll try to put all my contact.xhtml code inside my index.xhtml code to see what happen really during call on the button ! – Freest May 26 '13 at 16:12
  • @BalusC the hello world made me realize something ! have tried to do an Hello world and i think i have found the problem ! When i click ont the button in my contact.xhtml page , the page is automatically rendered/refresh without doing the button action, how can i stop this rendering ? – Freest May 26 '13 at 18:08
  • @Freest start by removing `h:body` from `h:head` and also all page content from `h:head` like this : `` – Alexandre Lavoie May 26 '13 at 18:59
  • @AlexandreLavoie i noticed my code was not really great :S thank for the advice i'll clean it and make it proper ! – Freest May 27 '13 at 11:55

1 Answers1

1

So after few days stuck on this problem i have finally find THE solution

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

In my index.xhtml , the <h:form> were inside the <h:panelgroup> , more exactly in my contact.xhtml ! If i put these form balise outside the <h:panelGroup> one and don't put any form balise in contact.xhtml , everything works properly, and a click on my button just call the right Action !

I'll post the final code in my first post ! Feel free to use it if you need ! Thank you all for the help you provide to me !

Freest
  • 109
  • 6