7

In order to replace the default Spring security login form I came up with this solution:

<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>

But instead of plain <form> tag I would like to use <h:form> since Primefaces components only work within <h:form>. Using <h:form> the form action will be set automatically by JSF to the current page, not to the value set by me in the example above. Where to I have to code the action "../j_spring_security_check" now? I tried putting it into the <h:commandButton> as follows but that doesn't work:

<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>

It leads to the error message Unable to find matching navigation case with from-view-id '/login.xhtml' for action '../j_spring_security_check' with outcome '../j_spring_security_check'.

Is it the only way to define a navigation case in faces-config.xml? I want to avoid using a bean for this simple use case.

Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
  • [See this answer](http://stackoverflow.com/questions/3807746/jsf-change-the-action-for-a-hcommandbutton-2-0/3807808#3807808) regarding `h:commandButton` action – Matt Handy Apr 19 '11 at 11:21
  • 1
    @Matt: Unfortunately the site `../j_spring_security_check` to be called is not an XHTML site. The link that you provided presumes that. – Lars Blumberg Apr 19 '11 at 11:25

4 Answers4

9

The best solution for me is to use the default <button> tag instead. Since typical button styling is not being applied (in my case I am using Primefaces), I set it manually. Here's the whole result:

    <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>
Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
7
<h:form id="login" prependId="false"              onsubmit="document.getElementById('login').action='j_security_check';">
Ramkumar Pillai
  • 154
  • 2
  • 8
1

Your best chance is to use the onclick property of the button. This is an example with a primefaces commandButton, but it will also work for jsf components because it uses the (native) javascript onclick function.

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

Your action attribute should be an EL expression like follows:

action="#{managedBean.method}"
maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • I would like to avoid defining a bean for my login bean since this bean would do nothing except returning the navigation case. – Lars Blumberg Apr 19 '11 at 11:16
  • Does actionListener attribute give you the same result? I think the PrimeFaces components require EL expressions for action as that was the only way I was able to get it to work. I had to create a LoginBean as well. – maple_shaft Apr 19 '11 at 11:36
  • Thanks for your feedback. I will use a bean as soon as our login form needs more functionality. For now I am satisfied with a usual `
    ` tag.
    – Lars Blumberg Apr 20 '11 at 02:33
  • Is it possible to pass arguments to the method? – osgx Jun 02 '13 at 23:53
  • @osgx Only in EL 2.2 or later. Older application servers and web containers like Tomcat have an older version of EL bundled that will not allow you to pass method arguments. – maple_shaft Jun 03 '13 at 00:28
  • Thanks! there are some samples here: http://stackoverflow.com/questions/3599948 How can I check the version of EL? – osgx Jun 03 '13 at 00:33
  • @osgx: You can virtually update your EL version to 2.2. be using Oracle's EL which implements EL 2.2. See http://stackoverflow.com/questions/3284236/invoke-direct-methods-or-methods-with-arguments-variables-parameters-in-el – Lars Blumberg Jul 31 '13 at 13:49