简体   繁体   English

从JSF Page调用void方法

[英]Call a void method from JSF Page

I am trying to implement a page where a user enters the Email address, the subject and the Message and then on send button, the message is sent together with an attachment. 我正在尝试实现一个页面,其中用户输入电子邮件地址,主题和消息,然后在发送按钮,该消息与附件一起发送。 It seems My major Problem is calling the CommandButton to execute the Void Class Sending the Message. 看来我的主要问题是调用CommandButton来执行发送消息的Void类。 Here is My xhtml page: 这是我的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>

My Sending Class is this(Only the Mothod). 我的发送类是这个(只有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());
     }

I do not have any error showing on my logs and yet this method is not getting called. 我的日志中没有显示任何错误,但此方法未被调用。 What is the Problem. 问题是什么。

#{sendscreen.sendemails()}

Take off the () from the function. 从功能中取消()。

Send Mails needs to return String as you're navigating away to a new page, if you want to stay on the same page have it return "" or null. 发送邮件需要在您导航到新页面时返回String,如果您想要保持在同一页面上,则返回“”或null。

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

In general if you don't want to navigate away from the page your best of using a actionListener rather than an action on your button submit. 一般情况下,如果您不希望离开页面,最好使用actionListener而不是按钮提交操作。 (Ie, remove the ajax="false") and change action to actionListener - this will also fix your problem as it will pick up your void method. (即,删除ajax =“false”)并将操作更改为actionListener - 这也将解决您的问题,因为它将获取您的void方法。 (assuming you remove the () still from the action) (假设你从动作中删除了())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM