自从我一直在互联网上搜索如何添加简单的警报以来已经有一段时间了,例如,当用户登录时(告诉他已经登录),但是经过大量的研究之后,仍然找不到或不起作用:/ 我使用Java EE和xhtml 如果输入为空,我想在这里显示警报。
有我的Java EE代码:
@Named
@SessionScoped
public class SeminaireController implements Serializable{
@EJB
private GestionSeminaireEJB gestionSeminaire;
private List<Seminaire> seminaires = new ArrayList<Seminaire>();
private Seminaire seminaire;
public SeminaireController() {}
public void init() {
seminaire = new Seminaire();
}
public Seminaire getSeminaire() {
if(seminaire == null) {
init();
}
return seminaire;
}
public void setSeminaire(Seminaire seminaire) {
this.seminaire = seminaire;
}
public List<Seminaire> getSeminaires(){
seminaires = gestionSeminaire.selectAll();
return seminaires;
}
public void deleteSeminaire(Seminaire seminaire) {
gestionSeminaire.deleteSemi(seminaire);;
seminaires = gestionSeminaire.selectAll();
}
/* public void updateSeminaire() {
gestionSeminaire.updateSeminaire(seminaire);
}
*/
public String addSeminaire() {
if(seminaire.getNomSeminaire() !="") {
gestionSeminaire.addSemi(seminaire);
seminaire.setNomSeminaire("");
return "Seminaire.xhtml?face-redirect=true";
}
return null;
}
}
有我的xHtml代码:
<!DOCTYPE html>
<xhtml xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:h="http://xmlns.jcp.org/jsf/html" charset="UTF-8"
pageEncoding="UTF-8" xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<meta charset="UTF-8" />
<title>Ajouter séminaire</title>
<style>
[required] {
border: 2px dotted orange;
}
</style>
</h:head>
<h:body>
<ui:include src="include/menu.xhtml" />
<h:form style="margin-left:auto;margin-top:10%;margin-right:auto;width: 35%;height:150px border-left: 6px solid red;background-color: lightgrey;font-size:18px;">
<h:outputLabel for="nameSemi">Nom du séminaire: </h:outputLabel>
<h:inputText type="text" id="nameSemi"
value="#{seminaireController.seminaire.nomSeminaire}" style="margin-left:10px;" required="true"/>
<br />
<h:commandButton value="Comfirmer"
action="#{seminaireController.addSeminaire()}" styleClass="btn btn-success btn-lg btn-block" />
</h:form>
</h:body>
</xhtml>
当我按下我的按钮时,它将触发我的addSeminaire()方法。在这一步中,我检查输入是否为空。如果输入为空,我想显示一条错误消息。 在此先感谢;)
答案 0 :(得分:0)
您可以添加仅在设置变量时显示的警报:
public class SeminaireController implements Serializable{
public bool showAlert = false;
if(seminaire.getNomSeminaire() !="") {
gestionSeminaire.addSemi(seminaire);
seminaire.setNomSeminaire("");
return "Seminaire.xhtml?face-redirect=true";
showAlert= false;
}
showAlert = true;
return null;
然后
<h:outputScript rendered="#{seminaireController.showAlert}">
alert("YourErrorMessagfe");
</h:outputScript>