如何使用JAX-RS Web服务打开JSF页面? [英] How to open JSF page using JAX-RS web service?

查看:120
本文介绍了如何使用JAX-RS Web服务打开JSF页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望其他Web应用程序(.net或任何其他形式)调用我的JAX-RS Web服务来设置和打开带有一些传递值的JSF页面.

I would like other web application (in .net or any other) to call my JAX-RS web service to set and open my JSF page with some passed values.

例如

  • 另一个Web应用程序只需传递userIdtoken即可打开条目页面.
  • 用户将完成输入,服务将返回创建的条目的uniqueId.
  • Another web application just pass the userId and a token to open an entry page.
  • User will complete the entry and service will return the uniqueId for created entry.

我对如何设置JSF上下文参数并使用JAX-RS打开该JSF页面感到困惑.谁能提供有关如何使用Web服务设置JSF会话范围的托管Bean的值的想法?

I am confused in how I can set JSF context parameters and open that JSF page using JAX-RS. Can anyone give idea about how to set value of JSF session scoped managed bean using web service?

推荐答案

首先,这个问题表明对"REST Web服务"的目的总体上存在误解.该问题具体要求使用REST Web服务执行两个相当不寻常的任务:

First of all, this question indicates a misunderstanding of purpose of "REST web services" in general. The question concretely asks to perform two rather unusual tasks with a REST web service:

  1. 处理与请求关联的HTTP会话.
  2. 重定向到由有状态MVC框架管理的HTML页面作为响应.

两者都与REST的无状态性质相矛盾.这些任务不应由REST Web服务执行.而且,REST Web服务主要旨在由编程客户端(例如JavaScript或Java代码)使用,而不是由使用HTML页面的网络浏览器使用.通常,您不需要在浏览器的地址栏中输入REST Web服务的URL即可查看HTML页面.通常,您可以在浏览器的地址栏中输入HTML页面的URL.该HTML页面又可以由基于HTML表单的MVC框架(例如JSF)生成.

Both squarely contradict the stateless nature of REST. Those tasks aren't supposed to be performed by a REST web service. Moreover, REST web services are primarily intented to be used by programmatic clients (e.g. JavaScript or Java code), not by webbrowsers which consume HTML pages. You normally don't enter the URL of a REST webservice in browser's address bar in order to see a HTML page. You normally enter the URL of a HTML page in browser's address bar. That HTML page can in turn be produced by a HTML form based MVC framework such as JSF.

在您的特定情况下,程序化客户端检索到唯一后, 来自REST Web服务的ID,那么程序化客户端本身应依次向JSF Web应用程序发出新请求.例如.在基于Java的客户端中如下所示(下面的示例假定它是一个普通的servlet,但是您可以自己说其他任何形式):

In your specific case, after the programmatic client has retrieved the unique ID from the REST web service, then the programmatic client should in turn all by itself fire a new request to the JSF web application. E.g. as follows in Java based client (below example assumes it's a plain servlet, but it can be anything else as you said yourself):

String uniqueId = restClient.getUniqueId(userId, token);
String url = "http://example.com/context/login.xhtml?uniqueId=" + URLEncoder.encode(uniqueId, "UTF-8");
response.sendRedirect(url);

在目标JSF Web应用程序中,只需以通常的方式使用<f:viewParam>/<f:viewAction>即可获取唯一ID并基于该ID执行业务操作.例如.如下login.xhtml所示:

In the target JSF web application, just use <f:viewParam>/<f:viewAction> the usual way in order to grab the unique ID and perform business actions based on that. E.g. as below in login.xhtml:

<f:metadata>
    <f:viewParam name="uniqueId" value="#{authenticator.uniqueId}" />
    <f:viewAction action="#{authenticator.check}" />
</f:metadata>

@ManagedBean
@RequestScoped
public class Authenticator {

    private String uniqueId;

    @EJB
    private UserService service;

    public String check() {
        FacesContext context = FacesContext.getCurrentInstance();
        User user = service.authenticate(uniqueId);

        if (user != null) {
            context.getExternalContext().getSessionMap().put("user", user);
            return "/somehome.xhtml?faces-redirect=true";
        }
        else {
            context.addMessage(null, new FacesMessage("Invalid token"));
            return null; // Or some error page.
        }  
    }

    // Getter/setter.
}

也许为了方便起见,REST Web服务甚至可以返回包括唯一ID的完整URL,从而使客户端无需担心目标URL.

Perhaps the REST webservice could for convenience even return the full URL including the unique ID so that the client doesn't need to worry about the target URL.

String uniqueIdURL = restClient.getUniqueIdURL(userId, token);
response.sendRedirect(uniqueIdURL);

另一方面,很有可能您只是误解了功能要求,并且可以直接在JSF Web应用程序中处理用户ID和令牌,如果它在与REST Web相同的服务器上运行,则可能性更大服务,也使用HTTPS.只需添加一个额外的<f:viewParam>并执行与<f:viewAction>方法中的JAX-RS资源相同的业务服务逻辑.

On the other hand, there's a reasonable chance that you just misunderstood the functional requirement and you can just directly process the user ID and token in the JSF web application, the more likely if it runs at the same server as the REST web service and also uses HTTPS. Just add an extra <f:viewParam> and do the same business service logic as the JAX-RS resource in the <f:viewAction> method.

<f:metadata>
    <f:viewParam name="userId" value="#{authenticator.userId}" />
    <f:viewParam name="token" value="#{authenticator.token}" />
    <f:viewAction action="#{authenticator.check}" />
</f:metadata>

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆