Introduction
This is an article where the main focus is to discuss about how to solve a specific error. Actually, there is a time where the submit process is not working at all. There is a connection with the previous article. The article is describing about how to connect to a PostgreSQL database server. It is an article with the title of ‘How to Connect to PostgreSQL Database Server from a Java Web Application using Primefaces Framework’. That article is available in this link. In summary, there is a JSF file where it has a short form with the name of ‘db-connection-test.xhtml’ as an example. The content of that file as the original version exist below :
<?xml version="1.0" encoding="UTF-8"?> <!-- Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/XHtml.xhtml to edit this template --> <!DOCTYPE html> <html 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.org/ui"> <h:head> <title>Database Connection</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> </h:head> <body> <h:form> <p:outputPanel> <p:outputLabel id="connectionStatus" value="#{dbConnectionTestView.status}"></p:outputLabel> </p:outputPanel> <p:commandButton type="button" value="Alert" onclick="alert('Prime')" actionListener="#{dbConnectionTestView.submit()}" update="@form"/> </h:form> </body> </html>
Solution
The solution for solving the problem where the submit method is not working is easy. Although the javascript ‘alert’ function is working, the button type is not working for the actionListener method. The method ‘submit’ available in the backing Java bean with the name of ‘dbConnectionTestView’ is not running at all. In this case, changing the type of the button from a regular button to a submit button will solve the problem. In other words, just change the button type as follows :
<p:commandButton type="button" value="Alert" onclick="alert('Prime')" actionListener="#{dbConnectionTestView.submit()}" update="@form"/>
Change it into in the following line :
<p:commandButton type="submit" value="Alert" onclick="alert('Prime')" actionListener="#{dbConnectionTestView.submit()}" update="@form"/>
.
If there are no more errors appear, it will show a window popup alert and then process the ‘submit’ method available in the Java bean with the name of ‘dbConnectionTestView’.