--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
*. 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";
}
}
I'm return
ing 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