【问题标题】:How to define form action in JSF?如何在 JSF 中定义表单动作?
【发布时间】:2011-08-08 14:07:59
【问题描述】:

为了替换默认的 Spring 安全登录表单,我想出了这个解决方案:

<form name="f" action="../j_spring_security_check" method="POST" >
    <h:panelGrid columns="2">
        <h:outputText value="Username" />
        <h:inputText id="j_username" />
        <h:outputText value="Password" />
        <h:inputText id="j_password" />
    </h:panelGrid>
    <h:commandButton value="Login" />
</form>

但我想使用&lt;h:form&gt; 而不是普通的&lt;form&gt; 标签,因为Primefaces 组件只能在&lt;h:form&gt; 中工作。使用&lt;h:form&gt;,表单动作将由 JSF 自动设置为当前页面,而不是我在上面的示例中设置的值。我现在必须在哪里编写动作"../j_spring_security_check"?我尝试将其放入&lt;h:commandButton&gt;,如下所示,但这不起作用:

<h:form name="f">
    <h:panelGrid columns="2">
        <h:outputText value="Username" />
        <h:inputText id="j_username" />
        <h:outputText value="Password" />
        <h:inputText id="j_password" />
    </h:panelGrid>

    <h:commandButton value="Click here" action="../j_spring_security_check" />
</form>

导致错误信息Unable to find matching navigation case with from-view-id '/login.xhtml' for action '../j_spring_security_check' with outcome '../j_spring_security_check'

这是在faces-config.xml 中定义导航案例的唯一方法吗?我想避免在这个简单的用例中使用 bean。

【问题讨论】:

  • See this answer 关于h:commandButton 操作
  • @Matt:不幸的是,要调用的站点../j_spring_security_check 不是XHTML 站点。您提供的链接是假定的。

标签: forms jsf action


【解决方案1】:

您的操作属性应该是如下的 EL 表达式:

action="#{managedBean.method}"

【讨论】:

  • 我想避免为我的登录 bean 定义一个 bean,因为这个 bean 除了返回导航案例之外什么都不做。
  • actionListener 属性是否给您相同的结果?我认为 PrimeFaces 组件需要 EL 表达式来执行操作,因为这是我能够使其工作的唯一方法。我还必须创建一个 LoginBean。
  • 感谢您的反馈。只要我们的登录表单需要更多功能,我就会使用 bean。现在我对通常的&lt;form&gt; 标签感到满意。
  • 是否可以将参数传递给方法?
  • @osgx 仅在 EL 2.2 或更高版本中。较旧的应用程序服务器和 Web 容器(如 Tomcat)捆绑了较旧版本的 EL,不允许您传递方法参数。
【解决方案2】:

对我来说最好的解决方案是使用默认的&lt;button&gt; 标签。由于没有应用典型的按钮样式(在我的情况下,我使用的是 Primefaces),我手动设置它。这是整个结果:

    <h:outputStylesheet library="primefaces" name="jquery/ui/jquery-ui.css" />
    <h:outputStylesheet library="css" name="login.css" />

    <div class="message">
        <c:if test="#{param.error == 1 and SPRING_SECURITY_LAST_EXCEPTION != null}">
            <span class="error">#{SPRING_SECURITY_LAST_EXCEPTION.message}</span>
        </c:if>
    </div>



    <div class="login">
        <form action="../j_spring_security_check" method="post">
            <h:panelGrid columns="2">
                <h:outputText value="Username" />
                <h:inputText id="j_username" />
                <h:outputText value="Password" />
                <h:inputSecret id="j_password" />
            </h:panelGrid>

            <div class="submit">
                <button type="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">
                    <span class="ui-button-text">Login</span>
                </button>
            </div>
        </form>
    </div>

【讨论】:

    【解决方案3】:
    <h:form id="login" prependId="false"              onsubmit="document.getElementById('login').action='j_security_check';">
    

    【讨论】:

    • 如果您不需要 id 来代替 &lt;h:form&gt;,只需使用 &lt;h:form onsubmit="this.action='j_security_check';"&gt;
    【解决方案4】:

    您最好的机会是使用按钮的 onclick 属性。这是一个带有 primefaces commandButton 的示例,但它也适用于 jsf 组件,因为它使用(本机)javascript onclick 函数。

    <h:form>
    <p:commandButton value="Submit" onclick="alert('JS!')" actionListener="#{tweetBean.addTweet()}" update=":tabView,:trends,:tweetsnr" /> 
    </h:form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-01
      • 2013-06-02
      • 1970-01-01
      • 2011-03-31
      相关资源
      最近更新 更多