JSF 提供非常基本的 REST get 方法

发布于 2024-12-07 16:13:35 字数 398 浏览 0 评论 0原文

有没有办法在 @ManagedBean 中使用 doGet 方法并定义该 bean 将对其做出反应的 URL。

我正在使用 JSF 并且想要提供一个非常基本的提要,但为此我需要对 get 请求做出反应。

我首先使用普通的 servlet 编写它,但现在我注意到我需要来自另一个 ManagedBean 的信息,因此我需要一个 @ManagedProperty - 因此 JSF...

我的问题:

  • 是否有 URLPattern 注释或类似内容?

  • 是否有类似于 Servlet 的 doGetdoGet 方法?

Is there a way to have a doGet method in a @ManagedBean and define a URL on which this bean will react.

I am using JSF and want to provide a really basic feed, but for this I need to react on a get request.

I wrote it with normal servlets first, but now I noticed that I need information from another ManagedBean therefore I need a @ManagedProperty - hence JSF...

My questions:

  • Is there a URLPattern annotation or similar?

  • Is there a doGet method that is similar to the Servlet's doGet?

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

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

发布评论

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

评论(2

大海や 2024-12-14 16:13:35

如果您想要 RESTful Web 服务,请使用 JAX-RS(例如Jersey)而不是 JSF。

或者,如果您只想要 JSF 的“漂亮”(类似 REST)URL,请使用 PrettyFaces

If you want a RESTful web service, use JAX-RS (e.g. Jersey) instead of JSF.

Or, if you just want "pretty" (REST-like) URLs for JSF, use PrettyFaces.

酒绊 2024-12-14 16:13:35

假设 servlet...

如果您依赖于 JSF 上下文,技巧是获取 FacesServlet 来执行代码。 FacesServlet 负责创建和销毁请求上下文。

这是我们要调用的托管 bean:

@ManagedBean @RequestScoped
public class Restlike {
  public void respond() {
   FacesContext context = FacesContext.getCurrentInstance();
   ExternalContext ext = context.getExternalContext();
   HttpServletResponse response = (HttpServletResponse) ext.getResponse();
   response.setContentType("text/plain; charset=UTF-8");
    try {
      PrintWriter pw = response.getWriter();
      pw.print("Hello, World!");
    } catch (IOException ex) {
      throw new FacesException(ex);
    }
    context.responseComplete();
  }
}

这是将执行代码的占位符视图。 resty.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<metadata xmlns="http://java.sun.com/jsf/core">
  <event type="preRenderView" listener="#{restlike.respond}"/>
</metadata>

点击 resty.faces 看起来不太 RESTful,但使用 filter

@WebFilter("/rest/*")
public class RestyFilter implements Filter {
  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    request.getRequestDispatcher("/resty.faces").forward(request, response);
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}

  @Override
  public void destroy() {} 
}

生成的 URL 类似于 http://host/context/rest< /code>


这个解决方案有点一种 hack,仅适用于 servlet 环境。更好的方法可能是添加自定义 ResourceHandler 但我没有花太多时间研究 API 的这一部分。

Assuming servlets...

If you're relying on the JSF context, the trick will be to get the FacesServlet to execute the code. The FacesServlet is responsible for creating and destroying the request context.

Here is the managed bean we want to invoke:

@ManagedBean @RequestScoped
public class Restlike {
  public void respond() {
   FacesContext context = FacesContext.getCurrentInstance();
   ExternalContext ext = context.getExternalContext();
   HttpServletResponse response = (HttpServletResponse) ext.getResponse();
   response.setContentType("text/plain; charset=UTF-8");
    try {
      PrintWriter pw = response.getWriter();
      pw.print("Hello, World!");
    } catch (IOException ex) {
      throw new FacesException(ex);
    }
    context.responseComplete();
  }
}

Here is the placeholder view that will execute the code. resty.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<metadata xmlns="http://java.sun.com/jsf/core">
  <event type="preRenderView" listener="#{restlike.respond}"/>
</metadata>

Hitting resty.faces doesn't look very RESTful, but it is trivial to handle with a filter:

@WebFilter("/rest/*")
public class RestyFilter implements Filter {
  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    request.getRequestDispatcher("/resty.faces").forward(request, response);
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}

  @Override
  public void destroy() {} 
}

The resultant URL will look something like http://host/context/rest


This solution is a bit of a hack and only applicable to servlet environments. A better approach might be to add a custom ResourceHandler but I haven't spent much time investigating that part of the API.

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