我有一个Primefaces Commandlink,它在mailto地址的末尾给了我一个工件。我不知道“#”是从哪里来的。
这是前端代码。
<p:commandLink value="Mail Video Link" action="#{requestBean.requestUtility.informationRequestLink()}" />
这是后端操作代码。
public void informationRequestLink() {
String subject = "Video Link";
String cc = "friend2@domain.com,friend3@domain.com";
String requestLink = "https://www.youtube.com/watch?v=SjeS6gtPq8E";
String body
= "Here is the link.\n"
+ requestLink + "\n\n"
+ "Watch at your leisure.";
try {
Desktop desktop = Desktop.getDesktop();
String mailURIString = String.format("?subject=%s&cc=%s&body=%s",
subject, cc, body);
URI mailURI = new URI("mailto", "user@domain.com", mailURIString);
desktop.mail(mailURI);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
我可以去掉“#”,但是我得到UTF-8编码的空格“+”。
String subject = "Video Link";
String cc = "friend2@domain.com,friend3@domain.com";
String requestLink = "https://www.youtube.com/watch?v=SjeS6gtPq8E";
String body
= "Here is the link.\n"
+ requestLink + "\n\n"
+ "Watch at your leisure.";
try {
Desktop desktop = Desktop.getDesktop();
String mailURIString = String.format("mailto:%s?subject=%s&cc=%s&body=%s",
"friend1@domain.com", subject.replaceAll(" ", "%20"), cc, URLEncoder.encode(body, "UTF-8"));
URI mailURI = URI.create(mailURIString);
desktop.mail(mailURI);
} catch (Exception e) {
e.printStackTrace();
}