添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐

Is it possible to display "User with this username does not exist" Error Message in Bootstrap Modal in Spring MVC Project through the controller?

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}
    <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>
@RequestMapping(value = "/recoverPassword", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void sendingEmailForForgot(@RequestParam("username") String username) {
    // Check whether email exists in DB
    System.out.println(userDAO.getUserByName(username));
    System.out.println(username);
    if (userDAO.getUserByName(username) == null) {
        System.out.println("This ID does not exists");
        redirectAttributes.addAttribute("error", "wrong");
    } else {
        System.out.println("mail sent to your id");
    // If exists then hash and send url link
    // else error

I have validated username whether if exists, but how to display that error on same currently opened modal?

If not possible through controller please share any other way to do so.

I think if you wanted to change the currently open Modal you would need to do it via AJAX – Plog Mar 24 '17 at 12:19 Without getting response from web service how can you show anything in bootstrap modal? And which version of spring are you using that is very important. – Shalin Patel Mar 24 '17 at 18:24 that's the question, I have made the response in a controller which validates whether this username exists, the problem is I want to keep bootstrap modal open if there is no such username and if it is there then i will redirect to homepage view. So how to do these two things parallelly is my question. – Yogesh Devgun Mar 24 '17 at 18:39

Finally solved it through AJAX, still don't know if possible through the controller. Works fine for me, I have used this:

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);

Following trick worked for me 1. put ${error} in p tag with unique id 2. On login.jsp page load,check if innerHtml of p tag is empty. 3 If it is not empty open the login modal.

function modelopen(){
var temp = document.getElementById("errormsg").innerHTML;
  if(temp!==""){
    $('#modallogin').modal('show');

You can try: 1) RestController instead of Controller (I think that this is way you solved this)
2) You can use validations and return your error as part of BindingResult https://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/validation.html 3) Use exceptionHandler

function errorshow() {
const input1 = document.getElementById('input1').value;
const input2 = document.getElementById('input2').value;
const div = document.getElementById("erroM");
if (input1 == '' && input2 == '') {
   div.innerHTML = "hey";
   setTimeout(() => {
    div.remove()}, 2000);
<!doctype html>
<html lang="en">
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
            crossorigin="anonymous">
             <title>Erorr Show in Modal</title>
    </head>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <div class="modal-body">
                    <div id="erroM"></div>
                        <input id="input1" type="text">
                        <input id="input2" type="text">
                        <input id="input3" type="text">
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" onclick="errorshow()">save</button>
            </form>
<!-- Button trigger modal -->
<button id="button" type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Click</button>
        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
            crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
            crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
        <script src="main.js"></script>
    </body>
</html>
Nipu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required. rev 2019.1.22.32706