首页 > 解决方案 > 发生违反约束时显示警报消息

问题描述

晚上好,

我向您展示我的界面,以便您更好地理解:

添加地点

我在 2 列(local_name 和 number_place)上添加了一个唯一约束,一切正常,只是有一个小问题,我想添加一个带有已经存在的地号的本地名称,它让我回到这样的空白页面:

白页

它向我显示了 NetBeans 上的错误,即我违反了约束……而我希望它不要将我重定向到空白页面,但它会停留在同一页面上,并且它会向我显示一条类型的消息:

错误您输入了两个具有相同地点编号和相同本地名称的场所

我尝试了一些 javascript 代码和 ajax 但它在这里不起作用是我在我的 servlet jee 中添加的代码:

if("ajouter".equals(action)){
PreparedStatement st1=c.prepareStatement("insert into place values (null,?,?,?,?)");
st1.setString(1, nom_local);
st1.setInt(2, Integer.parseInt(numeroplace));
st1.setString(3, prixplace);
st1.setString(4, tailleplace);
int resadd=st1.executeUpdate();
if (resadd>0){
request.setAttribute("msg", "place ajouté avec succés !");

}
else {
request.setAttribute("msg", "place non ajouté !");
throw new Exception("Erreur vous avez entrez deux locaux ayant le meme numéro de place et le meme nom de local" );
}
getServletContext().getRequestDispatcher("/WEB-INF/Place_locaux.jsp").forward(request, response);
}

这是javascript代码:

<script>

jQuery.ajax({
type: POST,
url: Place_locaux.java,
success: function (response) {
//Code en cas de succès de l'appel
},
error: function (xhr, ajaxOptions, thrownError) {
alert(chr.responseText); //Ce code affichera le message d'erreur.
}
});
</script>

我希望我已经清楚了,我期待您的回复,我要提前感谢您的回复

标签: javascriptajaxjakarta-ee

解决方案


推荐阅读