在xpages中的java中构建url

时间:2016-09-05 19:16:40

标签: java xpages

在xpages中我可以使用:

var protocol = context.getUrl().getScheme() + "://";
var url:XSPUrl = new XSPUrl(database.getHttpURL());
var host = url.getHost();
...

构建文档中的文档/文件的URL

我应该如何在Java中构建URL的等价物?

2 个答案:

答案 0 :(得分:3)

我一直用Java构建URL。

以下是我使用的一些RANDOM代码片段。这不是一个单独的类,只是随机片段,应该可以帮助您至少开始。

FacesContext facesContext = FacesContext.getCurrentInstance();
XSPContext context = XSPContext.getXSPContext(facesContext);

String entryPage = context.getUrl().getPath() + context.getUrl().getQueryString();

if (entryPage.contains("/home.xsp")) {
    this.console("Entry Page contains /home.xsp");
    if (this.isBasicMode()) {
            entryPage.replace("home.xsp", "basic_Menu.xsp");
        }
    } else {
        this.console("entry page does NOT contain /home.xsp");
    }

context.redirectToPage(“/myPage.xsp”);


public void redirectExternal(String url) throws IOException {

    FacesContext fc = FacesContext.getCurrentInstance();
        ExtenalContext externalContext = fc.getExternalContext();
        externalContext.redirect(url);

    }

public void redirectToPage(final String pageName) {
        // pageName = "/myPage.xsp"

        try {
            // You'd think this would end all Java processing but that's NOT
            // what happens
            // It looks like the Java code will finish and only then will the
            // redirection happen.

            final String entryPage = JSFUtil.getXSPContext().getUrl().getPath() + JSFUtil.getXSPContext().getUrl().getQueryString();
            FrameworkUtils.getSessionScope().put("entryPage", entryPage);
            this.setEntryPage(entryPage);

            JSFUtil.getXSPContext().redirectToPage(pageName);

        } catch (final RedirectSignal rs) {
            // Ignoring this error. Useless!
        }
        // Returning false so we can stop the calling code from continuing
        // return false;

    }

    public String getParam(final String key) {
        if (!this.getQueryString().containsKey(key)) {
            return null;
        } else {
            return this.getQueryString().get(key);
        }

    }


    @SuppressWarnings("unchecked")
    public Map<String, String> getQueryString() {
        final Map<String, String> qs = (Map<String, String>) FrameworkUtils.resolveVariable("param");
        return qs;
    }

答案 1 :(得分:1)

取决于文档文件的位置。它认为最好的方法是在nsf中创建一个servlet,通过传递docUNID和attachmentName来调用它。然后,您可以获取文档并将附件作为流写入响应。如果您将标题设置为附件,例如

response.setHeader("Content-disposition", "attachment; filename=\""+attachment.getName()+"\"");

您将获得附件下载