【问题标题】:Desktop and URI Producing Character Artifact桌面和 URI 生成字符工件
【发布时间】:2019-02-18 09:37:28
【问题描述】:

我有一个 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();
    }   

【问题讨论】:

  • 所以如果你使用 p:commandLink 和 ajax 你没有这个吗?还是没有ajax?还是来自没有 jsf 的单元测试?
  • 对于 ajax="true" 和 ajax="false",'#' 字符仍然存在。
  • 抱歉,应该是h:commandLink ;-) 排除它与 PF 相关
  • 并且请在单元测试中尝试 99,9% 肯定与 jsf 或 primefaces 无关

标签: java primefaces jsf-2


【解决方案1】:

哈希字符由您使用的构造函数附加。 看看JavaDoc

public URI(String scheme, String ssp, String fragment) throws URISyntaxException

[...]

最后,如果给定一个片段,则将一个哈希字符 ('#') 附加到字符串,然后是片段。任何不是合法 URI 字符的字符都会被引用。

你应该使用URI的适当构造函数;请参阅文档。对我来说,您的第三个论点似乎更像是 query 而不是 fragment

【讨论】:

  • 更新了构造函数以正确使用它。原始构造函数 = URI mailURI = new URI("mailto", "user@domain.com", mailURIString);新构造函数 = URI mailURI = new URI("mailto", mailURIString, "");宾果游戏!
猜你喜欢
  • 1970-01-01
  • 2016-03-26
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多