我想编写一个Web应用程序,触发用户的默认电子邮件客户端发送电子邮件。
因此,我创建了一个链接,它导致符合mailto URI方案的URL(http://en.wikipedia.org/wiki/Mailto):
Link emailLink = new Link("Send Email",
new ExternalResource("mailto:someone@example.com"));
但是,我想提供一个允许触发相应功能的Button,而不是使用Link。但是,对于按钮,我无法设置要打开的ExternalResource。
有没有人知道为Buttons解决这个问题,或者如何创建一个看起来和行为完全像按钮的链接?我也尝试了一些CCS修改,但没有自己管理任务。我还为前Vaadin版本找到了一些解决方案(https://vaadin.com/forum/#!/thread/69989),但不幸的是,他们不适用于Vaadin 7。
我记得使用ResourceReference解决类似的问题。
Button emailButton = new Button("Email");
content.addComponent(emailButton);
Resource res = new ExternalResource("mailto:someone@example.com");
final ResourceReference rr = ResourceReference.create(res, content, "email");
emailButton.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
Page.getCurrent().open(rr.getURL(), null);
}
});
为了解决类似问题,我之前申请过:
String email="info@ORGNAME.org";
Link l=new Link();
l.setResource(new ExternalResource("mailto:" + email));
l.setCaption("Send email to " + email);
addComponent(l);
经过一些进一步的尝试后,我们设法调整了提议的LinkButton解决方案 https://vaadin.com/forum/#!/thread/69989 对于Vaadin 7:
public class LinkButton extends Button {
public LinkButton(final String url, String caption) {
super(caption);
setImmediate(true);
addClickListener(new Button.ClickListener() {
private static final long serialVersionUID = -2607584137357484607L;
@Override
public void buttonClick(ClickEvent event) {
LinkButton.this.getUI().getPage().open(url, "_blank");
}
});
}
}
但是,此解决方案仍然不完美,因为它会导致弹出窗口的打开被某些Web浏览器阻止。