2013-04-09 32 views
0

我想用JSF發送電子郵件。我創建了成功發送電子郵件的頁面,但是當我從主菜單動態調用此頁面時,它會出現在我的頁面中心,但有些框不會顯示,最糟糕的是:Bean根本不工作。 所以我的問題是我應該如何正確地包含此頁面,以便仍然使用Beans的所有功能,或者如何從此頁面調用bean。 注意:它工作,只是在主頁內停止。通過ui包含頁面的觸發bean方法:include

因爲我也有問題直接調用我的Bean的sendMail2()方法,此刻我調用setter的屬性(因爲它的工作原理),並且在這個setter方法中我調用了sendMail2()。

頁:

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

豆:

/* 
* 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 = "[email protected]"; 
      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("[email protected]", "Agencja Ubezpieczeniowa"); 
      email.setSubject("TestMail"); 
      email.setMsg("This is a test mail 4"); 
      email.addTo("[email protected]", "ToName"); 
      //email.setStartTLSRequired(false); 
      email.send(); 
     } catch (EmailException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

在這裏我如何渲染菜單:

<!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> 
+0

什麼'helloBean.name'的價值? – 2013-04-09 22:21:41

+0

它是子頁面的名稱,即zapytaj.xhtml – 4pie0 2013-04-09 22:22:27

+0

我看到沒有任何反應,當我點擊這個子頁面的按鈕時:SendBean沒有被調用,但是當頁面打開時工作單一,主要 – 4pie0 2013-04-09 22:25:00

回答

0

爲路易門多薩說解決的辦法是喲使用的模板,ui:insert然後ui:composition。這是我工作的解決方案:

子頁面:

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

主頁,的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> 

相關問題