requestScope[ "javax.servlet.forward.request_uri" ] 在 JSF 中返回空值



正如我对JSF所知,默认情况下使用forward来处理页面导航,但为什么requestScope["javax.servlet.forward.request_uri"]返回null(不显示在outcome.xhtml页面中)。以下是示例代码:

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";
}

}

正如我对JSF所知,默认情况下使用forward来处理页面导航

根据xhtml扩展判断,您正在为视图定义语言使用Facelets。

如果您使用JSP作为VDL,那么您是正确的,因为RequestDispatcher.forward是用于将请求从servlet调度到JSP的标准机制。

然而,Facelet视图完全由JSF实现处理,不需要将视图呈现委托给容器。RequestDispatcher.forward不是必需的。

最新更新