2014-02-09 30 views
0

因爲我知道JSF,默認情況下使用forward來處理頁面導航,但爲什麼requestScope [「javax.servlet.forward.request_uri」]返回null(不會顯示在outcome.xhtml頁面中)。下面是樣本代碼:requestScope [「javax.servlet.forward.request_uri」]在JSF中返回null

的index.xhtml

<h:body> 
<h1>Index page</h1> 
<h1>Request URI: #{request.requestURI}</h1> 
<h1>Forward Request URI: #{requestScope['javax.servlet.forward.request_uri']}</h1> 
<h1>Forward Servlet Path: #{requestScope['javax.servlet.forward.servlet_path']}</h1> 
<h:form> 
    RequestScoped input: <h:inputText value="#{requestScopedBean.input}" /> 
    <h:commandButton value="submit" action="#{requestScopedBean.submit}" /> 
</h:form> 
</h:body> 

outcome.xhtml

<h:body> 
<h1>Outcome page</h1> 
<h1>Request URI: #{request.requestURI}</h1> 
<h1>Forward Request URI: #{requestScope['javax.servlet.forward.request_uri']}</h1> 
<h1>Forward Servlet Path: #{requestScope['javax.servlet.forward.servlet_path']}</h1> 
<h1>Requestscoped output: <h:outputText value="#{requestScopedBean.input}" /></h1> 

RequestScopedBean.java

@Named 
@RequestScoped 
public class RequestScopedBean { 

private int id; 
private String input; 

public String getInput() { 
    return input; 
} 

public void setInput(String input) { 
    this.input = input; 
} 

@PostConstruct 
public void init() { 
    Random random = new Random(); 
    id = random.nextInt(); 
    System.out.println(getClass().getName() + " id: " + id); 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String submit() { 
    System.out.println(getClass().getName() + " invokes submit() method"); 
    System.out.println("input: " + input); 
    return "outcome"; 
} 

}

回答

0

據我所知JSF的,默認情況下使用着處理頁面導航

您所使用的Facelets您視圖定義語言XHTML擴展來看。

如果您使用JSP作爲您的VDL,那麼您應該是正確的,因爲RequestDispatcher.forward是用於將請求從servlet分派到JSP的標準機制。

但是,Facelet視圖完全由JSF實現處理,並且不需要將視圖渲染委託給容器。 RequestDispatcher.forward不是必需的。

+0

嗨,我不是在談論與servlet + jsp的MVC模式,我只是想知道方法public String submit()使用forward或redirect機制來導航到「結果」。如果假設爲forward,那麼爲什麼requestScope [「javax.servlet.forward.request_uri」]在outcome.xhtml中沒有顯示任何內容 –

+0

JSF有一個servlet,它使用該MVC模式,但不使用Facelets。 「javax.servlet.forward.request_uri」未設置,因爲未使用Facelets調用前進。 _NavigationHandler_不會與Facelet VDL調用_forward_。 – McDowell

+0

你打到了點!我在哪裏可以找到有關NavigationHandler的文檔和示例,非常感謝。 –

相關問題