How to define form action in JSF?

To replace the standard 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 the usual <form> I would like to use <h:form> , since Primefaces components work only inside <h:form> . Using <h:form> , the form action will be automatically set by JSF to the current page, and not to the value that I set in the above example. Where do I need to code the action "../j_spring_security_check" now? I tried putting it in <h:commandButton> as follows, but this does not 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> 

This 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 this the only way to determine the navigation case in faces-config.xml ? I want to avoid using a bean for this simple use.

+6
source share
4 answers

The best solution for me is to use the default <button> tag. Since the typical button style is not applied (in my case, I use Primefaces), I install it manually. Here is 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> 
+8
source
 <h:form id="login" prependId="false" onsubmit="document.getElementById('login').action='j_security_check';"> 
+7
source

The action attribute must be an EL expression as follows:

 action="#{managedBean.method}" 
+1
source

Your best chance is to use the onclick property. This is an example using the primface commandButton command, but it will also work for jsf components as it uses the (built-in) javascript onclick function.

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

Source: https://habr.com/ru/post/886231/


All Articles