JSF -参数中的空值- FacesContext



我试图得到几个参数(现在它是两个,但在另一个xhtml可能我需要更多)。

在页index.html我有一个链接,页Threads.xhtml,参数user_id和section_id:

<h:dataTable value="#{Sections.sections}" var="Section">
    <h:column>
        <h:form>
            <h:link value="#{Section.section_label}" outcome="Threads">
                <f:param name="user_id" value="#{LoginIn.user_id}"/>
                <f:param name="section_id" value="#{Section.section_id}"/>
            </h:link>
        </h:form>
    </h:column>
</h:dataTable>

当我点击其中一个链接时,它会转到例如:

Project/faces/Threads.xhtml?user_id=5&section_id=2

所以这很好:)。

现在,在页Threads.xhtml我有一个链接(一个链接,不是dataTable,如在index.xhtml -创建新的部分),页NewThread.xhtml,参数user_id和section_id:

<h:form>
    <h:link value="#{msg.create_new_thread}" outcome="NewThread">
        <f:param name="user_id" value="#{param.user_id}"/>
        <f:param name="section_id" value="#{param.section_id}"/>
    </h:link>
</h:form>

当我点击其中一个链接时,它会转到,例如::

Project/faces/NewThread.xhtml?user_id=5&section_id=2

所以也不错:).

现在,我检查NewThread.xhtml页面user_id和section_id的值,通过:

<h:outputText value="User_id: #{param.user_id}"/><br/>
<h:outputText value="Section_id: #{param.section_id}"/><br/>

我看到页面值:

User_id: 5
Section_id: 2

所以ok:)。但是现在,当我试图在Java代码newthread。Java中获取这些值时,它们只返回null:

FacesContext facesContext = FacesContext.getCurrentInstance();
String user_id = (String) facesContext.getExternalContext().getRequestParameterMap().get("user_id");
String section_id= (String) facesContext.getExternalContext().getRequestParameterMap().get("section_id");
//For test:
System.out.println("User_id: " + user_id);
System.out.println("Section_id: " + section_id);

在控制台我有:

User_id: null
Section_id: null

我也尝试过toString()方法,但它没有帮助。

回到index.xhtml,当我点击到其中一个部分的链接时,例如第二部分(任何用户,例如user_id=5),它会转到Threads.xhtml:

Project/faces/Threads.xhtml?user_id=5&section_id=2

在Threads.xhtml上,我有第二部分的线程列表(但这部分xhtml代码是无关的):

<h:dataTable value="#{Threads.threads}" var="Thread">
    <h:column>
        <h:link value="#{Thread.thread_label}" outcome="Threads">
            <f:param name="thread_id" value="#{Thread.thread_id}"/>
        </h:link>
    </h:column>
</h:dataTable>

和Java代码的线程。Java,我有:

FacesContext facesContext = FacesContext.getCurrentInstance();
String section_id = (String) facesContext.getExternalContext().getRequestParameterMap().get("section_id");
System.out.println("Section_id: " + section_id);

在控制台我有:

Section_id: 2

WTF ?一次成功,另一次失败。

编辑:

对不起,我忘了。我访问NewThread.java,通过NewThread.xhtml中的commandButton:
<h:commandButton value="#{msg.add_thread}" action="#{NewThread.AddThread}"/>

,它调用NewThread.java中的AddThread方法;在try (of try-catch) of AddThread方法中,我试图获取参数。

我已经在faces-config.xml中添加了:

<managed-bean>
    <managed-bean-name>Threads</managed-bean-name>
    <managed-bean-class>forum.Threads</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean>
    <managed-bean-name>NewThread</managed-bean-name>
    <managed-bean-class>forum.NewThread</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

单击h:commandButton时,将发出新的请求。问题是参数没有传递给这个请求。

因此,如果您希望您的请求作用域bean NewThread从请求参数中获取数据,那么您也必须在h:commandButton发出的请求中包含这些参数。试试这个:
<h:commandButton value="#{msg.add_thread}" action="#{NewThread.AddThread}">
    <f:param name="user_id" value="#{param.user_id}"/>
    <f:param name="section_id" value="#{param.section_id}"/>
</h:commandButton>

当您使用JSF 2时,您可能会发现使用f:metadataf:viewParam以及includeViewParameters=true技巧在页面之间导航是有用的,请查看这些问题:

  • 什么可以和& lt; f: viewAction>被用于?
  • post后在JSF中处理视图参数

正如elias所说,当您单击命令按钮时,有一个新的请求(post),这将不包括来自前一个请求的get参数。

另一种解决方案是将上述视图参数与OmniFaces中的o:form组件结合使用,该组件具有自动保留所有视图参数作为GET参数的属性:

<o:form includeViewParams="true">

这还有一个额外的好处,用户可以在URL中看到这些参数,所以如果他们复制它或通过在浏览器的地址栏中按enter键刷新页面,事情就会像预期的那样工作。

最新更新