从JSF Page调用void方法

时间:2014-03-20 07:40:24

标签: java jsf commandbutton

我正在尝试实现一个页面,用户输入电子邮件地址,主题和消息,然后在发送按钮,消息与附件一起发送。看来我的主要问题是调用CommandButton来执行发送消息的Void类。这是My xhtml页面:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <ui:composition template="./fros3.xhtml">
        <ui:define name="top">
        </ui:define>
        <ui:define name="content">
        </ui:define>
        <ui:define name="bottom">
            <h:form>
                <p:growl id="msg" showDetail="true" sticky="true" />
                <p:panel header="Email ScreenShot" style=" width: 50%">
                    <h:panelGrid columns="2">
                        <h:outputLabel value="E-mail address:" styleClass="requiredLbl" />
                        <p:inputText value="#{sendscreen.email}"
                                     id="username" required="true" label="username" />
                        <h:outputLabel value="Subject:" styleClass="requiredLbl" />
                        <p:inputText value="#{sendscreen.subject}"
                                     id="subject" required="true" />
                        <h:outputLabel value="Message:" styleClass="requiredLbl" />
                        <h:inputTextarea id="txt" value="#{sendscreen.message1}" required="True" style=" width: 250px; height: 100px"/>
                        <p:commandButton value="Send Email" ajax="false" action="#{sendscreen.sendemails()}" />
                        <h:panelGroup/>
                    </h:panelGrid>
                </p:panel>
            </h:form>
        </ui:define>
    </ui:composition>
</html>

我的发送类就是这个(只有Mothod)。

public void sendemails() {
        try {
            System.out.println("Sending ......");
            Message message = new MimeMessage(sm.getSession());
            message.setFrom(new InternetAddress(username));
            message.setRecipient(Message.RecipientType.CC, new InternetAddress(email));
            message.setSubject(Subject);
            message.setContent(message1, "text/plain");
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(message1);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            MimeBodyPart attachmentBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource("C:/capture/screenShot.jpg");
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            attachmentBodyPart.setFileName("screenShot.jpg");
            multipart.addBodyPart(attachmentBodyPart);

            Transport.send(message);
     }catch(Exception asd){
       System.out.println(asd.getMessage());
     }

我的日志中没有显示任何错误,但此方法未被调用。问题是什么。

1 个答案:

答案 0 :(得分:2)

#{sendscreen.sendemails()}

从功能中取消()。

当您导航到新页面时,发送邮件需要返回字符串,如果您希望保持在同一页面上,则返回“”或返回null。

public String sendemails() {
   //do stuff
   return null;
}

一般情况下,如果您不希望离开页面,最好使用actionListener而不是按钮提交操作。 (即,删除ajax =“false”)并将操作更改为actionListener - 这也将解决您的问题,因为它将获取您的void方法。 (假设您从动作中删除了())

相关问题