Spring mvc 在SpringMVC项目的引导模式中显示错误消息

Spring mvc 在SpringMVC项目的引导模式中显示错误消息,spring-mvc,bootstrap-modal,display,Spring Mvc,Bootstrap Modal,Display,是否可以通过控制器在Spring MVC项目的引导模式中显示“使用此用户名的用户不存在”错误消息? login.jsp <div class="modal-body"> <form id="register-form" role="form" autocomplete="off" class="form" method="POST" action="${pageContext.request.contextPath }/recoverPassword">

是否可以通过控制器在Spring MVC项目的引导模式中显示“使用此用户名的用户不存在”错误消息?

login.jsp

<div class="modal-body">
<form id="register-form" role="form" autocomplete="off" class="form"
    method="POST"
    action="${pageContext.request.contextPath }/recoverPassword">

    <div class="form-group">
        <div class="input-group">
            <span class="input-group-addon"><i
                class="glyphicon glyphicon-envelope color-blue"></i></span> <input
                id="email" name="username" placeholder="your username"
                class="form-control" type="text"> ${error}
        </div>
    </div>
    <input type="hidden" name="${_csrf.parameterName}"
        value="${_csrf.token}" />
    <button class="btn btn-lg btn-primary btn-block" type="submit">
        Send email link</button>

</form>
我已经验证了用户名是否存在,但如何在当前打开的相同模式上显示该错误


如果无法通过控制器实现,请分享任何其他实现方法。

最终通过AJAX解决了这个问题,仍然不知道是否可以通过控制器实现。对我来说很好,我用过这个:

function doAjaxPost() {
    // get the form values
    var name = $('#name').val();
    var education = $('#education').val();

    $.ajax({
        type: "POST",
        url: contexPath + "/AddUser.htm",
        data: "name=" + name + "&education=" + education,
        success: function(response){
            // we have the response
            if(response.status == "SUCCESS"){
                userInfo = "<ol>";
                for(i =0 ; i < response.result.length ; i++){
                    userInfo += "<br><li><b>Name</b> : " + response.result[i].name +
                    ";<b> Education</b> : " + response.result[i].education;
                 }
                 userInfo += "</ol>";
                 $('#info').html("User has been added to the list successfully. " + userInfo);
                 $('#name').val('');
                 $('#education').val('');
                 $('#error').hide('slow');
                 $('#info').show('slow');
             }else{
                 errorInfo = "";
                 for(i =0 ; i < response.result.length ; i++){
                     errorInfo += "<br>" + (i + 1) +". " + response.result[i].code;
                 }
                 $('#error').html("Please correct following errors: " + errorInfo);
                 $('#info').hide('slow');
                 $('#error').show('slow');
             }
         },
         error: function(e){
             alert('Error: ' + e);
         }
    });
}
函数doAjaxPost(){
//获取表单值
var name=$('#name').val();
var education=$(“#education”).val();
$.ajax({
类型:“POST”,
url:contexPath+“/AddUser.htm”,
数据:“name=“+name+”&education=“+education,
成功:功能(响应){
//我们得到了答复
如果(response.status==“SUCCESS”){
userInfo=“”;
对于(i=0;i
  • 名称:”+response.result[i].Name+ “教育:”+反应.结果[i].教育; } 用户信息+=“”; $('#info').html(“用户已成功添加到列表中。”+userInfo); $('#name').val(''); $(“#教育”).val(“”); $('#error')。隐藏('slow'); $('info')。显示('slow'); }否则{ errorInfo=“”; 对于(i=0;i”+(i+1)+“+响应。结果[i]。代码; } $('#error').html(“请更正以下错误:“+errorInfo”); $('info')。隐藏('slow'); $('#error')。show('slow'); } }, 错误:函数(e){ 警报('错误:'+e); } }); }
  • 以下技巧对我很有效 1.将${error}放入具有唯一id的p标记中 2.在login.jsp页面加载时,检查p标记的innerHtml是否为空。 3如果不为空,则打开登录模式

    function modelopen(){
    var temp = document.getElementById("errormsg").innerHTML;
      if(temp!==""){
        $('#modallogin').modal('show');
    }
    
    您可以尝试: 1) RestController而不是Controller(我认为这就是您解决此问题的方法)
    2) 您可以使用验证并将错误作为BindingResult的一部分返回 3) 使用异常处理程序

  • 函数errorshow(){
    const input1=document.getElementById('input1').value;
    const input2=document.getElementById('input2').value;
    const div=document.getElementById(“erroM”);
    如果(输入1=''&&input2=''){
    div.innerHTML=“嘿”;
    设置超时(()=>{
    2000年,第2节);
    }}
    
    模态中的Erorr显示
    情态标题
    &时代;
    接近
    拯救
    点击
    
    我认为,如果您想更改当前打开的模式,您需要通过AJAXHi进行更改。您能提供更多信息吗?如果没有web服务的响应,您如何在引导模式中显示任何内容?您使用的是哪个版本的spring非常重要。这是一个问题,我在一个控制器中做出了响应,该控制器验证这个用户名是否存在,问题是如果没有这样的用户名,我想保持引导模式打开,如果有,我将重定向到主页视图。因此,如何并行地完成这两件事是我的问题。实现版本:4.3.4。我正在使用的发布版本
    function modelopen(){
    var temp = document.getElementById("errormsg").innerHTML;
      if(temp!==""){
        $('#modallogin').modal('show');
    }