0

I want to send email with JSF. I created page that sends email successfully, but when I call this page dynamically from main menu it appears in center of my page, but some boxes do not appear and worst thing: Bean doesn't work at all. So my question is how should I include this page correctly, in order to still use all functionality of Beans, or alternatively how to call bean from this page. note: it works, just stops when is inside main page.

because I have also problem to call method sendMail2() of my Bean directly, at the moment I call setter of property (because it works) and inside this setter method I call sendMail2().

page:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">

    <h:body>
        <h3>JSF 2.0 + Ajax Hello World Example</h3>

        <h:form>

            <h:inputText id="namez" value="#{mySendBean.name}"></h:inputText>
            <h:commandButton value="Welcome Me">
                 <f:ajax execute="namez" render="outputz" />
            </h:commandButton>

            <h2><h:outputText id="outputz" value="#{helloBean2.sayWelcome}" /></h2>

        </h:form>

    </h:body>
</html> 

Bean:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.mail.*;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;


/**
 *
 * @author root
 */
@ManagedBean
@SessionScoped
public class mySendBean {

    private myEmailSend mE;
    /**
     * Creates a new instance of mySendBean
     */
    public mySendBean() {
        mE=new myEmailSend();
    }

    private static int j;

    /**
     * Get the value of j
     *
     * @return the value of j
     */
    public int getJ() {
        return j;
    }

    /**
     * Set the value of j
     *
     * @param j new value of j
     */
    public void setJ(int j) {
        this.j = j;
    }

        private String name="iop";

    /**
     * Get the value of name
     *
     * @return the value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Set the value of name
     *
     * @param name new value of name
     */
    public void setName(String name) {
        this.name = name;
        sendMail2();
    }

    public String getSayWelcome(){
       //check if null?
       if("".equals(name) || name ==null){
        return "";
       }else{
        return "Ajax message : Welcome " + name;
       }
    }

    public void setSendMail(){
        sendMail2();
    }
    public void sendMail2(){


        Email email = new SimpleEmail();
        try {
            String authuser = "me@gmail.com";
            String authpwd = "pass";
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
            email.setDebug(true);
            email.setHostName("smtp.gmail.com");
            email.getMailSession().getProperties().put("mail.smtps.auth", "true");
            email.getMailSession().getProperties().put("mail.debug", "true");
            email.getMailSession().getProperties().put("mail.smtps.port", "587");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
            email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
            email.setFrom("me@gmail.com", "Agencja Ubezpieczeniowa");
            email.setSubject("TestMail");
            email.setMsg("This is a test mail 4");
            email.addTo("someone@gmail.com", "ToName");
            //email.setStartTLSRequired(false);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

and here how I render menu:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">




        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>

            </f:facet>
        </h:head>

        <h:body>
                <style type="text/css">
                @import url("myCss.css");
                </style>
            <p:layout fullPage="true">

                <p:layoutUnit styleClass="myLayoutStyleClass ui-layout-unit-content" position="north" size="200" resizable="true" closable="true" collapsible="true" border="0">
                    <h:form style="margin: 0; border:0" ><h1 style="color: white"><br>ubezpieczenia</br></h1></h:form>
                </p:layoutUnit>

                <p:layoutUnit position="south" size="100" closable="true" collapsible="true">
                    Zapraszamy do odwiedzania naszego biura!
                </p:layoutUnit>

                <p:layoutUnit style="background-color: white;color: white" position="west" size="175" header="Menu" collapsible="true">
                    <h:form style="background-color: white">
                    <p:menu style="background-color: white;border-width: 0" >
                            <f:ajax render=":content">
                            <p:menuitem value="O naszej agencji" action="#{helloBean.setName('/main_pages/onas.xhtml')}" update=":content" />
                            <p:menuitem value="Ubezpieczenia pojazdów" action="#{helloBean.setName('/main_pages/ubpoj.xhtml')}" update=":content" />
                            <p:menuitem value="Ubezpieczenia majątkowe" action="#{helloBean.setName('/main_pages/ubmaj.xhtml')}" update=":content" />
                            <p:menuitem value="Ubezpieczenia na życie" action="#{helloBean.setName('/main_pages/ubnaz.xhtml')}" update=":content" />
                            <p:menuitem value="Zapytaj" action="#{helloBean.setName('/main_pages/zapytaj.xhtml')}" update=":content" />
                            <p:menuitem value="Kontakt" action="#{helloBean.setName('/main_pages/kontakt.xhtml')}" update=":content" />
                            </f:ajax>
                    </p:menu>
                    </h:form>
                </p:layoutUnit>

                <p:layoutUnit position="center">

                    <br></br><br></br>
                    <p:panel id="content">
                                        <ui:include src="#{helloBean.name}" />
                    </p:panel>       

                </p:layoutUnit>

            </p:layout>

        </h:body>



</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • What's the value of `helloBean.name`? – Luiggi Mendoza Apr 09 '13 at 22:21
  • it is name of the subpage, i.e. zapytaj.xhtml – 4pie0 Apr 09 '13 at 22:22
  • I see that nothing happens when I click button of this subpage: the SendBean is not called, however works when page is opened single, as main – 4pie0 Apr 09 '13 at 22:25
  • Can you see if there's a problem in the generated HTML code when calling the *parent* page? – Luiggi Mendoza Apr 09 '13 at 22:28
  • main page works ok, though I didnt try it with beans – 4pie0 Apr 09 '13 at 22:29
  • do you think there are some special limitations when including page this way? or can it be any xhtml page? – 4pie0 Apr 09 '13 at 22:29
  • Please **check the generated HTML**. – Luiggi Mendoza Apr 09 '13 at 22:30
  • what do you mean? I open page in browser, moreover you can do it too, it is here: www.cf16.eu/web2 – 4pie0 Apr 09 '13 at 22:31
  • 1
    As expected, when **you check the generated HTML code** you will see that there is an internal ` ` (and the rest of the content of your *zapytaj.xhtml* file **as is**), generating a wrong HTML code (that is elegantly parsed by some browsers) and breaking the page functionality. This can be solved by using a template (which you can learn **following a sane tutorial**). – Luiggi Mendoza Apr 09 '13 at 22:32
  • so can it be done only with template? – 4pie0 Apr 09 '13 at 22:34
  • @LuiggiMendoza please elapborate and post you answer I will accept it – 4pie0 Apr 09 '13 at 22:34
  • 1
    possible duplicate of [How to include another XHTML in XHTML using JSF 2.0 Facelets?](http://stackoverflow.com/questions/4792862/how-to-include-another-xhtml-in-xhtml-using-jsf-2-0-facelets) – Luiggi Mendoza Apr 09 '13 at 22:35
  • You're welcome. Next time, don't try to go blindly on JSF, there are plenty of resources in the net. You can start looking at [StackOverflow JSF wiki](http://stackoverflow.com/tags/jsf/info), [Highest Voted JSF Questions](http://stackoverflow.com/questions/tagged/jsf?sort=votes&pagesize=15) and following a tutorial (BalusC is highly recommended since he's JSF expert). – Luiggi Mendoza Apr 09 '13 at 22:43
  • @LuiggiMendoza but how are you navigating than from menu? will only the "insert" part be updated/refreshed? not all page? I mean there will be no flashes, etc? this was the reason why I decided only refresh the center of main page not load whole page of course – 4pie0 Apr 09 '13 at 23:21

1 Answers1

0

as Luigi Mendoza stated the solution is yo use templates, ui:insert and then ui:composition. Here is my working solution:

subpage:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      >
    <h:body>
 <link type="text/css" rel="stylesheet" href="theme.css"/>
        <ui:composition template="index.xhtml">

            <ui:define name="centerContent">
                    <h:form>
                        <p:growl id="msg" showDetail="true" sticky="true" />
                        <p:panelGrid  styleClass="myPanel2" columnClasses="myColumnClasses" columns="2">

                        <p:column>
                           <p:row>
                                <ui:param name="mainTag" value="Z chęcią odpowiemy" />
                                <h2 align="left">Zapytaj</h2>
                                <h4 align="left">#{mainTag}</h4>

                                 <h:outputLabel styleClass="myTextArea">Twój email</h:outputLabel>
                            </p:row>
                            <p:row>
                                <p:inputText id="input1" title="Twój adres email" styleClass="myText" value="#{mySendBean.userMail}"/>
                                <p:watermark for="input1" value="Twój adres email" />
                                <p:inputTextarea id="input2" styleClass="myTextArea" title="Twoja wiadomość" autoResize="true" value="#{mySendBean.mailContent}" />
                                <p:watermark for="input2" value="Twoja wiadomość" />

                            </p:row>
                            </p:column>
                            <p:column styleClass="myColumnClasses">
                                <p:row>
                                    <div id="buttons" style="position: absolute !important; bottom: 50px !important; vertical-align: bottom !important;">
                                    <h:commandButton styleClass="mycommandButton" actionListener="#{mySendBean.sendMail2()}" value="Wyślij" update="msg">
                                    </h:commandButton>
                                    </div>
                                </p:row>
                         </p:column>

                        </p:panelGrid>
                    </h:form>
            </ui:define>


        </ui:composition>

    </h:body>

</html>

main page, index.xhtml:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui" xmlns:c="http://java.sun.com/jsp/jstl/core">
<style type="text/css">
                @import url("myCss.css");
                </style>
                <style type="text/css">
                @import url("myCss2.css");
                </style>

        <h:head>
            <title>Dorota Gregor</title>
            <link type="text/css" rel="stylesheet" href="theme.css"/>
            <script src="http://maps.google.com/maps/api/js?sensor=false" 
              type="text/javascript"></script>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>

            </f:facet>
        </h:head>

        <h:body>

            <p:layout fullPage="true">

                <p:layoutUnit styleClass="myLayoutStyleClass1" position="north" size="170" resizable="true" closable="true" collapsible="true" >
                    <h:form style="margin: 0; border:0" ><h1 style="color: white">Dorota Gregor<br/><span class="mysection-title">ubezpieczenia</span><br></br></h1></h:form>
                </p:layoutUnit>

                <p:layoutUnit styleClass="myLayoutStyleClass2" position="south" size="50" closable="true" collapsible="true" >
                    <h:form style="margin: 0; border:0" ><span class="myfootter-title tab">Krosno Odrzańskie</span><span class="myfootter-title tab">Plac 11-ego Pułku</span><span class="myfootter-title tab">66-600 Krosno Odrzańskie</span><span class="myfootter-title tab">tel./fax 68 383 40 58</span><span class="myfootter-title tab">tel. 501 198 308</span><span class="myfootter-title tab">tel. 609 338 308</span>
                    </h:form>
                </p:layoutUnit>

                <p:layoutUnit style="background-color: white;color: white" position="west" size="190" header="Menu" collapsible="true">
                    <h:form style="background-color: white">
                    <p:menu style="background-color: white;border-width: 0" >

                            <p:menuitem value="O naszej agencji" action="onas.xhtml?faces-redirect=true" />
                            <p:menuitem value="Ubezpieczenia pojazdów" action="ubpoj.xhtml?faces-redirect=true" />
                            <p:menuitem value="Ubezpieczenia majątkowe" action="ubmaj.xhtml?faces-redirect=true"  />
                            <p:menuitem value="Ubezpieczenia na życie" action="ubnaz.xhtml?faces-redirect=true"  />
                            <p:menuitem value="Zapytaj" action="zapytaj.xhtml?faces-redirect=true" />
                            <p:menuitem value="Kontakt" action="kontakt.xhtml?faces-redirect=true" />

                    </p:menu>
                    </h:form>
                </p:layoutUnit>

                <p:layoutUnit position="center">

                    <br>
                    <div id="centerContent">
                        <img src="images/tablicakarnety/domek.png"/><br/>
        <ui:insert name="centerContent" >
                    <ui:param name="mainTag" value="" />
          <ui:include src="/template/commonContent.xhtml" />
        </ui:insert>

        </div>
                    </br><br></br>     

                </p:layoutUnit>
                <p:layoutUnit style="background-color: white;color: white" position="east" size="250" header="Są z nami" collapsible="true">
                    <h:form style="background-color: white">
                        <p:panelGrid styleClass="myPanel" columns="2">
                            <p:column>
                                <p:row><img src="images/tablicakarnety/warta_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/hestia_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/generali_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/concordia_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/liberty_40.png" /></p:row><br/>
                             </p:column>
                             <p:column>
                                <p:row><img src="images/tablicakarnety/inter_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/hdi_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/gothaer_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/mtu_40.png" /></p:row><br/>
                                    <p:row><img src="images/tablicakarnety/ptu_40.png" /></p:row><br/>
                            </p:column>
                        </p:panelGrid>


                    </h:form>

                </p:layoutUnit>

            </p:layout>

        </h:body>

4pie0
  • 29,204
  • 9
  • 82
  • 118