如何使用 JAX-RS Web 服务打开 JSF 页面?
How to open JSF page using JAX-RS web service?
我希望其他网络应用程序(在 .net 或任何其他网络中)调用我的 JAX-RS 网络服务来设置和打开我的带有一些传递值的 JSF 页面。
例如
- 另一个 Web 应用程序只需通过
userId
和 token
即可打开一个入口页面。
- 用户将完成条目,服务将 return
uniqueId
创建条目。
我对如何设置 JSF 上下文参数并使用 JAX-RS 打开该 JSF 页面感到困惑。谁能给出有关如何使用 Web 服务设置 JSF 会话范围托管 bean 的值的想法?
在您的代码中试试这个,
return new Viewable("/index", "FOO");
或者,
RequestDispatcher rd = request.getRequestDispatcher("{url}");
rd.forward(request, response);
或者,
return Response.status(myCode).entity(new Viewable("/index", "FOO")).build();
尝试使用 jersey、
中的这些示例
@GET
public Response get() {
URI uri=new URI("http://nohost/context");
Viewable viewable=new Viewable("/index", "FOO");
return Response.ok(viewable).build();
}
到return不同的东西使用这种方法:
@GET
public Response get() {
int statusCode=204;
Viewable myViewable=new Viewable("/index","FOO");
return Response.status(statusCode).entity(myViewable).build();
}
希望对您有所帮助
首先,这个问题表明对 "REST web services" 的一般目的存在误解。该问题具体要求使用 REST Web 服务执行两个相当不寻常的任务:
- 操纵与请求关联的 HTTP 会话。
- 重定向到由有状态 MVC 框架管理的 HTML 页面作为响应。
两者都与 REST 的 stateless 本质相矛盾。这些任务不应由 REST Web 服务执行。此外,REST Web 服务主要供程序化客户端(例如 Java 脚本或 Java 代码)使用,而不是供使用 HTML 页面的网络浏览器使用。您通常不会在浏览器的地址栏中输入 REST 网络服务的 URL 以查看 HTML 页面。您通常在浏览器的地址栏中输入 HTML 页面的 URL。该 HTML 页面又可以由 HTML 基于表单的 MVC 框架(例如 JSF)生成。
在您的特定情况下,在程序化客户端检索到唯一
来自 REST Web 服务的 ID,然后编程客户端应该反过来向 JSF Web 应用程序发出新请求。例如。在基于 Java 的客户端中如下所示(下面的示例假设它是一个普通的 servlet,但它可以是您自己所说的任何其他内容):
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
:
<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 网络服务甚至可以 return 包含唯一 ID 的完整 URL,这样客户端就不需要担心目标 URL。
String uniqueIdURL = restClient.getUniqueIdURL(userId, token);
response.sendRedirect(uniqueIdURL);
另一方面,您很有可能只是误解了功能需求,您可以直接在 JSF Web 应用程序中处理用户 ID 和令牌,如果它与REST 网络服务,也使用 HTTPS。只需添加一个额外的 <f:viewParam>
并在 <f:viewAction>
方法中执行与 JAX-RS 资源相同的业务服务逻辑。
<f:metadata>
<f:viewParam name="userId" value="#{authenticator.userId}" />
<f:viewParam name="token" value="#{authenticator.token}" />
<f:viewAction action="#{authenticator.check}" />
</f:metadata>
另请参阅:
- What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
我希望其他网络应用程序(在 .net 或任何其他网络中)调用我的 JAX-RS 网络服务来设置和打开我的带有一些传递值的 JSF 页面。
例如
- 另一个 Web 应用程序只需通过
userId
和token
即可打开一个入口页面。 - 用户将完成条目,服务将 return
uniqueId
创建条目。
我对如何设置 JSF 上下文参数并使用 JAX-RS 打开该 JSF 页面感到困惑。谁能给出有关如何使用 Web 服务设置 JSF 会话范围托管 bean 的值的想法?
在您的代码中试试这个,
return new Viewable("/index", "FOO");
或者,
RequestDispatcher rd = request.getRequestDispatcher("{url}");
rd.forward(request, response);
或者,
return Response.status(myCode).entity(new Viewable("/index", "FOO")).build();
尝试使用 jersey、
中的这些示例@GET
public Response get() {
URI uri=new URI("http://nohost/context");
Viewable viewable=new Viewable("/index", "FOO");
return Response.ok(viewable).build();
}
到return不同的东西使用这种方法:
@GET
public Response get() {
int statusCode=204;
Viewable myViewable=new Viewable("/index","FOO");
return Response.status(statusCode).entity(myViewable).build();
}
希望对您有所帮助
首先,这个问题表明对 "REST web services" 的一般目的存在误解。该问题具体要求使用 REST Web 服务执行两个相当不寻常的任务:
- 操纵与请求关联的 HTTP 会话。
- 重定向到由有状态 MVC 框架管理的 HTML 页面作为响应。
两者都与 REST 的 stateless 本质相矛盾。这些任务不应由 REST Web 服务执行。此外,REST Web 服务主要供程序化客户端(例如 Java 脚本或 Java 代码)使用,而不是供使用 HTML 页面的网络浏览器使用。您通常不会在浏览器的地址栏中输入 REST 网络服务的 URL 以查看 HTML 页面。您通常在浏览器的地址栏中输入 HTML 页面的 URL。该 HTML 页面又可以由 HTML 基于表单的 MVC 框架(例如 JSF)生成。
在您的特定情况下,在程序化客户端检索到唯一 来自 REST Web 服务的 ID,然后编程客户端应该反过来向 JSF Web 应用程序发出新请求。例如。在基于 Java 的客户端中如下所示(下面的示例假设它是一个普通的 servlet,但它可以是您自己所说的任何其他内容):
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
:
<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 网络服务甚至可以 return 包含唯一 ID 的完整 URL,这样客户端就不需要担心目标 URL。
String uniqueIdURL = restClient.getUniqueIdURL(userId, token);
response.sendRedirect(uniqueIdURL);
另一方面,您很有可能只是误解了功能需求,您可以直接在 JSF Web 应用程序中处理用户 ID 和令牌,如果它与REST 网络服务,也使用 HTTPS。只需添加一个额外的 <f:viewParam>
并在 <f:viewAction>
方法中执行与 JAX-RS 资源相同的业务服务逻辑。
<f:metadata>
<f:viewParam name="userId" value="#{authenticator.userId}" />
<f:viewParam name="token" value="#{authenticator.token}" />
<f:viewAction action="#{authenticator.check}" />
</f:metadata>
另请参阅:
- What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?