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";
}
}
嗨,我不是在談論與servlet + jsp的MVC模式,我只是想知道方法public String submit()使用forward或redirect機制來導航到「結果」。如果假設爲forward,那麼爲什麼requestScope [「javax.servlet.forward.request_uri」]在outcome.xhtml中沒有顯示任何內容 –
JSF有一個servlet,它使用該MVC模式,但不使用Facelets。 「javax.servlet.forward.request_uri」未設置,因爲未使用Facelets調用前進。 _NavigationHandler_不會與Facelet VDL調用_forward_。 – McDowell
你打到了點!我在哪裏可以找到有關NavigationHandler的文檔和示例,非常感謝。 –