如何显示成功消息Spring MVC中的同一页上的(从验证数据库中的数据后)?(how to sho

2019-10-30 03:52发布

我在春天MVC新。 我从.JSP形式从数据库中采取了一些数据,并确认相同的数据,如果输入的数据是已经存在那么我想打印一个消息:“已存在ENTER不同的数据”,否则“新增的数据成功地将”,但同一网页上在这里用户不输入单独的网页上的数据。

_____________
**form.jsp**
_____________
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<form action="signup.op" method ="post">
<input type = "text" name="nm">
<input type = "password" name="pwd">
<input type = "num" name = "mobileNumber">
<input type = "submit" value="press"><br>
</form>
</body>
</html>

-----------------------------
_________________
**Controller class**
_________________
-------------------------------------------------------------------------------
**NOTE**:Before reading this code I want to clear you pople that in this Controller I am using *ModelAndView* to show the message on a separate response.jsp page but actually I don't want it.
------------------------------------------------------------------------------

    @Controller
    @RequestMapping("/")
    public class Controller {

        @Autowired
        private Services services;

        //constructor
        public Controller() {
    System.out.println(getClass().getName());
        }

        @RequestMapping(value="signup.op", method = RequestMethod.POST)
        public ModelAndView signup( DTO dto) {

        /*don't confuse from the below line of code I am just storing a String
        type of value("yes" or "no") in "returnFromServices" variable and based on 
        this value I want to send the message(that is mentioned above) on the same 
         web page*/
         String returnFromServices = services.saveStudentRecords(dto);

        ModelAndView modelAndView = new ModelAndView("response.jsp");
        if(returnFromServices.equals("yes")) {
    modelAndView.addObject("message", "ALREADY PRESENT ENTER DIFFERENT DATA");
            return modelAndView;
        }

            modelAndView.addObject("message","DATA ADDED SUCCESSFULLY" );
            return modelAndView;
       }
    }

Answer 1:

使用可以使用Ajax调用来调用的Spring MVC控制器马托和使用引导模式显示同一页上的响应消息

这是代码片段,

Ajax调用

function postAjaxWithJSONData(url,formId,callback){
     var data = JSON.stringify(jQuery(formId).serialize());
     $.ajax({ 
             url: url,    
             type: "POST",
             data:$(formId).serializeObject(),
             dataType : "json",
             success: function(respose) {
                    if(respose.status == 'error') {
                        showAjaxResponseMessage(respose);
                    } else {
                        callback(respose);
                        showAjaxResponseMessage(respose);
                    }
                },
             error: function(){
                 console.log('error');
             }
         });
}

function showAjaxResponseMessage(response) {
     $("#message").text(response.message);
     $('#message-modal').modal('show');
}

function editUser() {
    var editCallback = function(){
        $('#myModal').modal('hide');
        //displayDashboardContent();
    };
    postAjaxWithJSONData('editUser','#editUser',editCallback);
}

HTML代码

jsp页面

 <form id="editUser">
    <div class="modal-body">
       <div class="form-group">
          <input type="hidden" class="form-control" name="id" value=""/>
          <br/>

          <label for="username" class="form-control-label">First Name</label>
          <input type="text" class="form-control" name="firstName" value=""/>
          <br/>
      </div>

      <div class="form-group">
          <label for="username" class="form-control-label">Last Name</label>
          <input type="text" class="form-control" name="lastName" value=""/>
          <br/>
      </div>

      <div class="form-group">
          <label for="username" class="form-control-label" >Username</label>
          <input type="text" class="form-control" name="userName" value="" readonly/>
          <br/>
      </div>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-primary" onclick="editUser()">Save</button>
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  </div>
</form>   

模式弹出

     <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
           <button type="button" class="close" data-dismiss="modal">&times;         </button>
           <h4 class="modal-title">Message</h4>
        </div>
        <div class="modal-body">
           <p id="message"></p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
     </div>
  </div>

EDIT1

控制器的方法将是,

@RequestMapping(value = "editUser", method = RequestMethod.POST)
public @ResponseBody ResponseVO editUser(@ModelAttribute(value = "userVO") UserDataVO userDataVO) {
    logger.debug("in editUser controller");
    ResponseVO responseVO = userServiceImpl.editUser(userDataVO);
    return responseVO;
}


文章来源: how to show success message(after validating data from database) on the on the same page in spring mvc?
标签: spring-mvc