【问题标题】:header containing h:link and h:commandLink wont work?包含 h:link 和 h:commandLink 的标头不起作用?
【发布时间】:2011-12-04 22:22:31
【问题描述】:

我遇到了这种奇怪的情况。我有一个带有模板页面的 jsf 2.0 应用程序。 在标题上,我有一些带有 h:link 的链接,以保持 url 可书签性和我用来登录和注销的 h:commandLink .. 问题是当我通过 h:link 导航到特定页面时 假设 url 变为 bla.jsf?uName=dino 然后一旦我进入 bla.jsf?uName=dino 我点击应该带我到 signout.jsf 的退出按钮将不起作用。相反,它会将我带到没有 ?uName=dino 的 bla.jsf 页面。

我没有嵌套的 h:form,我觉得一切都很正常。有什么建议为什么它会这样吗?

我确定您在问我是否在 h:commandLink 上有 h:form。是的,我也有。这是我的代码:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui" 
    xmlns:ui="http://java.sun.com/jsf/facelets">

<div class="floatRight">
<h:form id="firstFormHeader" prependId="false">
 <p:autoComplete id="searchInputId" size="40" style="z-index: 20;" value="#{autoComplete.text}" completeMethod="#{autoComplete.complete}"/>
 <h:commandButton  value="#{label.search}" action="#{autoComplete.getSearchedVideo}" />
 <p:watermark for="searchInputId" value="#{label.search}" />    
</h:form>
</div>

<div style="z-index: 1;" id="topsection">


<div style="position:relative; bottom:-51px;">
    <div id="menu" style="height:37px;" class="title ui-widget-header">
    <h:panelGrid border="0" columns="2" style="float: left;">
     <h:column>

         <h:panelGrid columns="2">
      <h:column>

    <h:form id="secondFormHeader" prependId="false" >
<h:selectOneMenu id="videoGenereHeaderId" value="#{homePageVideoLoader.videoCategory}"  required="true"  onchange="submit()" valueChangeListener="#{homePageVideoLoader.getSelectedCatagoryList}">
        <f:selectItem id="item0" itemLabel="#{select.category}" itemValue="" />
        <f:selectItem id="item1" itemLabel="#{select.animation}" itemValue="23" />
        <f:selectItem id="item2" itemLabel="#{select.autos}" itemValue="15" />
        <f:selectItem id="item3" itemLabel="#{select.comedy}" itemValue="10" />
        <f:selectItem id="item4" itemLabel="#{select.documentary}" itemValue="19" />
        <f:selectItem id="item5" itemLabel="#{select.drama}" itemValue="4" />
        <f:selectItem id="item6" itemLabel="#{select.education}" itemValue="16" />
        <f:selectItem id="item7" itemLabel="#{select.hiphop}" itemValue="5" />
        <f:selectItem id="item8" itemLabel="#{select.guragigna}" itemValue="12" />
        <f:selectItem id="item9" itemLabel="#{select.news}" itemValue="6" />
        <f:selectItem id="item10" itemLabel="#{select.oldies}" itemValue="7" />
        <f:selectItem id="item11" itemLabel="#{select.reggae}" itemValue="8" />
        <f:selectItem id="item12" itemLabel="#{select.harari}" itemValue="13" />
        <f:selectItem id="item13" itemLabel="#{select.oromigna}}" itemValue="14" />
    <!--    <f:selectItem id="item7" itemLabel="#{select.entertainment}" itemValue="ent" />
        <f:selectItem id="item8" itemLabel="#{select.gaming}" itemValue="gam" /> 
        <f:selectItem id="item9" itemLabel="#{select.health}" itemValue="hea" />
        <f:selectItem id="item10" itemLabel="#{select.howto}" itemValue="how" /> -->
        <f:selectItem id="item14" itemLabel="#{select.music}" itemValue="3" />
        <f:selectItem id="item15" itemLabel="#{select.politics}" itemValue="18" />
    <!--   <f:selectItem id="item13" itemLabel="#{select.nonprofit}" itemValue="non" />
        <f:selectItem id="item14" itemLabel="#{select.blog}" itemValue="blo" />
        <f:selectItem id="item15" itemLabel="#{select.animals}" itemValue="anm" />
        <f:selectItem id="item16" itemLabel="#{select.science}" itemValue="sci" />
        <f:selectItem id="item17" itemLabel="#{select.sport}" itemValue="spo" />
        <f:selectItem id="item18" itemLabel="#{select.travel}" itemValue="tra" /> -->

        </h:selectOneMenu>

</h:form>

</h:column>
<h:column>


    <h:outputText rendered="#{userAuthentication.userLogged and userAuthentication.user.usersValid}" value="#{label.hello} "/>
     <h:link rendered="#{userAuthentication.userLogged and userAuthentication.user.usersValid}" value="#{userAuthentication.user.usersFirst}" outcome="userhome?uName=#{userAuthentication.user.usersUname}">

         </h:link>


   </h:column>
</h:panelGrid>

     </h:column>
    </h:panelGrid>
    <h:panelGrid border="0" columns="2" styleClass="menuPostion">
     <h:column>

            <h:link outcome="home" value="#{label.home}" /> | 

        <h:link outcome="upload" value="#{label.upload}" /> | 
        <h:form id="thridFormHeader" prependId="false" >
        <h:commandLink rendered="#{userAuthentication.userLogged}" value="#{label.signout}" action="#{userAuthentication.doSignOut}" immediate="true"/> 
        </h:form>
        <h:link rendered="#{!userAuthentication.userLogged}" value="#{label.createAccount}" outcome="registration" /> 
        <h:outputText rendered="#{!userAuthentication.userLogged}" value=" | "/>    
        <h:link rendered="#{!userAuthentication.userLogged}" value="#{label.signin}" outcome="signin" />

      </h:column>    
      <h:column>
        <h:form prependId="false" id="languageForm">
         <h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
                    valueChangeListener="#{language.countryLocaleCodeChanged}">
                    <f:selectItems value="#{language.countriesInMap}" /> 
                </h:selectOneMenu>
         </h:form>
       </h:column>   
    </h:panelGrid>
    </div>
</div>
</div>
</ui:composition>

第二个&lt;h:form&gt; 不起作用。


更新:抱歉不起作用。它甚至不会使用 doSignOut 方法.. 一旦我到达页面 bla.jsf?uName=dino 并查看有关它向我显示的退出链接的源代码

<form id="j_idt31" enctype="application/x-www-form-urlencoded" action="/myapp/content/bla.jsf" method="post" name="j_idt31" target="">

...

显然 bla.jsf 给了我错误消息,因为它正在等待 uName 有一个值。 为什么这样做?是bug吗?

另外,您可能已经注意到我有 h:selectOneMenu 在值更改时向 bean 发送数据,但是一旦我在 bla.jsf?uName=dino 上并尝试更改 h:selectOneMenu 的值由于某种原因它没有重定向到 bean 是不行的,所以 h:commandLink 但 h:link(s) 工作正常.. 我怎样才能解决它 BalusC.. 非常感谢您抽出宝贵的时间并回答方式

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    &lt;h:commandLink&gt; 嵌套在 &lt;h:form&gt; 中,其 HTML 生成的 &lt;form action&gt; URL 默认为当前请求 URL。它基本上是一个使用 JavaScript 提交该表单的链接。由于表单的操作默认为当前请求 URL,因此您看不到请求 URL 的变化。从技术上讲,它运行良好

    如果您想在提交后更改请求 URL,则需要发送重定向。您可以通过将faces-redirect=true 参数添加到结果值来做到这一点:

    public String doSignOut() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "signout?faces-redirect=true";
    }
    

    或者当您还在faces-config.xml 中使用老式的&lt;navigation-case&gt; 时,则需要添加一个&lt;redirect /&gt; 条目。

    【讨论】:

    • 抱歉不起作用。它甚至不会使用 doSignOut 方法.. 一旦我到达页面 bla.jsf?uName=dino 并查看有关注销链接的源代码,它会显示我
      ...
      显然 bla.jsf 给了我错误信息,因为它正在等待uName 有一个值。为什么这样做?这是一个错误吗?
    • 阅读此内容:stackoverflow.com/questions/2118656/… 我对数据表值保存不当的好处。
    • 所以你收到了错误信息?您不应忽略错误消息,就好像它们仅用于装饰一样。它们包含答案。如果您只是无法理解错误信息,那么您应该与我们分享,以便我们通俗易懂地解释,以便您理解错误信息.一旦错误消息被理解,那么解决方案就显而易见了。
    • 错误消息只是告诉我,当我尝试注销时 uName 为空,因为表单仍然指向 userhome.jsf
    【解决方案2】:

    在伟大的 BalusC 的帮助下,我找到了我的解决方案。我的代码不起作用,因为它是菜单栏,因此每次页面更改标题或菜单栏时都保持不变,这意味着表单不会更改。

    我不知道为什么,但我没有使用 h:link,而是使用 h:commandLink 和 f:setPropertyActionListener 的组合来发送 uName,然后在服务器端我这样做了

    public String goToUserHomePage(){
         methodNM = CLASS_NAME+".goToUserHomePage()";
         log.info("in " + methodNM);
         System.out.println("in " + methodNM);
    
         System.out.println("in " + methodNM+"-------------------------"+user.getUsersUname());
    
         return "userhome?uName="+user.getUsersUname()+"&amp;faces-redirect=true";
    }
    

    通过这样做,无论是使用 h:commandLink 还是 h:link 的每个链接都可以正常工作

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 2012-12-17
      相关资源
      最近更新 更多