我的 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 中制作的简单联系表格。
更新-好的,今天有一些消息!如果我使用这个东西:<p:commandButton action="#{mail.send()}"/>
在页面 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 页面中发现了一些关于操作按钮的内容