[JAVA] How to execute action attribute processing only when the confirmation dialog with h: commandButton is OK

--Environment - CentOS Linux release 7.8.2003 (Core) - Eclipse IDE for Enterprise Java Developers.Version: 2020-03 (4.15.0) - openjdk version "11.0.7" 2020-04-14 LTS - JSF 2.3.9

Thing you want to do

  1. Press the button to display the confirmation dialog
  2. Click the [OK] button in the confirmation dialog to execute the business logic of the action attribute.
  3. Nothing happens when the [Cancel] button is pressed in the confirmation dialog.

How to execute the action attribute process only when the confirmation dialog is OK

*. The html tag and next.xhtml are omitted. Use window.confirm for the confirmation dialog.

base.xhtml


<h:head>
  <title>For ponsuke's Qiita</title>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <h:outputScript library="js" name="base.js"/>
</h:head>
<h:body>
<h3>This is screen A</h3>
<h:form id="formId">
  <div>
    <h:commandButton value="Screen transition"
      onclick="return confirmMessage();"
      action="#{sampleBean.showNextPage()}" />
  </div>
</h:form>
</h:body>

base.js


/**
 *Display a confirmation dialog.
 * @return {boolean}Selection result in confirmation dialog true:Press the OK button.
 */
function confirmMessage() {
    return window.confirm('Can I change the screen?');
}

SampleBean.java


package brans;

import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class SampleBean implements Serializable {
    /** serialVersionUID. */
    private static final long serialVersionUID = -6782548672735889274L;
    /**
     *Display the next page.
     * @return Next page.
     */
    public String showNextPage() {
        return "next.xhtml";
    }
}

Failure case: Do not return with onclick attribute

I'm returning in base.js, so it's good, right? I often fail because I think. The action attribute process is executed regardless of the result of the confirmation dialog. I want someone to explain the reason ...

base.xhtml


<!--abridgement-->
    <h:commandButton value="Screen transition"
      onclick="confirmMessage();"
      action="#{sampleBean.showNextPage()}" />
<!--abridgement-->

Writing like this works fine for each button


    <h:commandButton value="Screen transition"
      onclick="return window.confirm('Can I change the screen?');"
      action="#{sampleBean.showNextPage()}" />

Recommended Posts

How to execute action attribute processing only when the confirmation dialog with h: commandButton is OK
[swift5] How to execute processing when tabBar is tapped
Uppercase only the specified range with substring. (How to use substring)
When requested access to the resource is denied when pushing with Docker
How to output the value when there is an array in the array
How to write the view when Vue is introduced in Rails?
[Docker] How to build when the source code is bind-mounted on the container
How to constrain the action of the transition destination when not logged in
How to compare only the time with Rails (from what time to what time, something like)
How to debug the processing in the Ruby on Rails model only on the console
[Ruby] How to prevent errors when nil is included in the operation
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[swift] How to control the behavior when the back button of NavigationBar is pressed
How to get the query string to actually issue when using PreparedStatement with JDBC
How to resolve the "OpenSSH keys only supported if ED25519 is available" error
[IntelliJ IDEA] How to format only local changes when saving the source code
How to set when "The constructor Empty () is not visible" occurs in junit
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container