2012-09-01 32 views
1

對不起,我剛從所有這些EJB,JSF和JAX-RS的東西開始,現在需要你的幫助。 我創建了一個JAX-RS資源類,它很好地工作並實現了@GET,@PUT等方法。如何從JSF頁面獲取JAX-RS絕對路徑?

在同一個項目中,我現在用相應的BackBean創建了一個JSF頁面。這個Backbean應該與REST接口交談。在測試時,我將REST接口的URI硬編碼到了bean中,但是當然我想通過編程方式獲得URI。我試着用@Produces方法和注入,但我總是得到一個IllegalStateException。我認爲這與上下文有關,但我實際上沒有理解解決它。

我的REST資源:

@Path("task") 
@ManagedBean 
@RequestScoped 
public class TaskResource { 

@Context 
private UriInfo context; 

@Inject TaskLifecycle lc; 

public TaskResource() { 
} 

@GET 
@Path("{id}") 
public Response getTask(@PathParam("id") String id) { ... etc. 

我Backbean:

@ApplicationScoped 
@LocalBean 
@Named("tmmlWrapper") 
public class TmmlTaskWrapperBean implements Serializable { 

// Here another ManagedBean is injected, which works fine! 
@Inject TaskLifecycle  lc; 

,最後我的JSF頁面:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org 
/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<h:head> 
    <title>Tasklist</title> 
</h:head> 
<h:body> 
    <h:form> 
     <h:outputLabel ><h3>Tasklist:</h3></h:outputLabel> 

     <h:dataTable value="#{tmmlWrapper.taskList}" var="tl"> 

     <h:column> 

      <f:facet name="header">ID</f:facet> 
      #{tl.id} 

     </h:column> ... and so on ... etc. 

我的問題: 我BackBean怎樣才能得到的URI REST資源(例如「http:// exampledomain:8080/as」)? 歡迎任何幫助!

乾杯, Joern

回答

3

你首先需要獲得(而不是portlet容器假定爲一個)訪問底層的Servlet容器生產HttpServletRequest對象。使用FacesContext對象來訪問以下面的方式HttpServletRequest對象:

HttpServletRequest origRequest = (HttpServletRequest)FacesContext.getExternalContext().getRequest(); 

HttpServletRequest類提供了幾種實用方法,以獲得原始請求的近表示:

  • getRequestURL(),它提供了原始請求SANS查詢字符串
  • getSchemegetServerNamegetServerPortgetContextPathgetServletPathgetPathInfogetQueryString所有的輸出可以按順序組合以獲得原始請求。如果您想要較小的URL片段,則可能不得不省略後者的調用。
+0

Thanx爲此,作品!我永遠不會朝這個方向前進! – Joern

相關問題