我想用JSF发送电子邮件。我创建了成功发送电子邮件的页面,但是当我从主菜单动态调用此页面时,它出现在我的页面中心,但是有些框没有出现,最糟糕的是:Bean根本不起作用。 所以我的问题是我应该如何正确地包含这个页面,以便仍然使用Beans的所有功能,或者如何从这个页面调用bean。 注意:它可以工作,只是在主页面内停止。
因为我也有问题直接调用我的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 = "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();
}
}
}
以及我如何渲染菜单:
<!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 :(得分:0)
Luigi Mendoza表示解决方案是使用模板,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>