如何从 JSF 支持 bean 调用 servlet 或 Web 服务?

发布于 2024-10-19 13:50:56 字数 802 浏览 0 评论 0 原文

我真的找不到正确的方法来做到这一点。

我有这个方法并且它有效,但这似乎是一种解决方法来完成如此基本的事情。

  FacesContext context = FacesContext.getCurrentInstance();      

    String baseURL = context.getExternalContext().getRequestContextPath();

    String startDateString = sdf.format(startDate);
    String endDateString = sdf.format(endDate);

    String url = baseURL + "/Excel?pkgLineId="+selectedPkgLine.getPkgLineId()+"&dateStart=" + startDateString + "&dateEnd=" + endDateString;

    try {

        String encodeURL = context.getExternalContext().encodeResourceURL(url);
        context.getExternalContext().redirect(encodeURL);
    } catch (Exception e) {
    } finally {
        context.responseComplete();
    }

我还了解到,调用 servlet 不被认为是最佳实践。如果我将 servlet 移至 Web 服务会怎样?我该如何称呼它?感谢您的任何帮助。

I seriously cannot find the correct way to do this.

I have this method and it works, but it seems kind of a work around to do something so basic.

  FacesContext context = FacesContext.getCurrentInstance();      

    String baseURL = context.getExternalContext().getRequestContextPath();

    String startDateString = sdf.format(startDate);
    String endDateString = sdf.format(endDate);

    String url = baseURL + "/Excel?pkgLineId="+selectedPkgLine.getPkgLineId()+"&dateStart=" + startDateString + "&dateEnd=" + endDateString;

    try {

        String encodeURL = context.getExternalContext().encodeResourceURL(url);
        context.getExternalContext().redirect(encodeURL);
    } catch (Exception e) {
    } finally {
        context.responseComplete();
    }

I have also read that calling servlets is not considered best practice. What if I moved my servlet to a web service? How would I go about calling that? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2024-10-26 13:50:56

你并不是真的在打电话给他们。您正在将响应重定向给他们。您基本上是在告诉网络浏览器它应该在给定的 URL 上触发新的 HTTP 请求。这是否是最佳实践取决于唯一的功能需求。据给定的代码示例提示,这对我来说似乎完全合法。尽管我可能会使用普通的 HTML

来代替带有托管 bean 的 。同样,这取决于功能需求(只需问问自己:为什么这个特定的需求到底需要 JSF?验证?特定的后处理?)。

如果您实际上想要以编程方式调用它并处理其响应,那么您应该使用 HTTP 客户端 API。基本的 Java SE API 为此提供了裸露的 java.net.URLConnection API。如果它是一个 Web 服务,例如 JAX-WS/JAX-RS,那么您应该为此使用 API 提供的客户端。

另请参阅:


与具体问题无关,当您使用 FacesContext#responseComplete() “http://download.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html#redirect%28java.lang.String%29” rel="nofollow noreferrer">ExternalContext#redirect () (但是当您从 JSF 下提取 HttpServletResponse 并对其调用 sendRedirect() 时,这是必要的)。

You aren't really calling them. You are redirecting the response to them. You are basically telling the webbrowser that it should fire a new HTTP request on the given URL. Whether that is the best practice or not depends on the sole functional requirement. As far the given code example hints, it seems perfectly legal to me. Although I would probably have used a normal HTML <form action="Excel"> for this instead of a <h:form> with a managed bean. Again, that depends on the functional requirement (just ask yourself: why exactly do you need JSF for this particular one? Validation? Specific postprocessing?).

If you actually want to call it and process its response programmatically, then you should be using a HTTP client API. The basic Java SE API offers the bare java.net.URLConnection API for this. If it is a Webservice, for example JAX-WS/JAX-RS, then you should use the API-provided client for this.

See also:


Unrelated to the concrete problem, manually calling FacesContext#responseComplete() is unnecessary when you use ExternalContext#redirect() (but it is necessary when you haul the HttpServletResponse from under the JSF covers and call sendRedirect() on it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文