Spring MVC Populate Modal Form

2019-08-10 23:31发布

I have a modal form (using bootstrap) that I use to add a user. This form is wired to a 'addUser' method via RequestMapping in my Controller. Adding a user works fine.

Is there a simple way I can populate the same modal form to edit existing user details after clicking an edit button? Do I have to make an AJAX call to a REST endpoint to populate the form, or is there a better way of doing this?

I'm using Spring Boot.

Thanks, Jerry

2条回答
时光不老,我们不散
2楼-- · 2019-08-10 23:56

An AJAX request would be your best bet, otherwise you'd have to load all the editable user properties ahead of time when you load the page of existing users and store them in memory or on the page somehow. It's not the most efficient solution though because you're loading all those properties for all users, every time you reload the page. If you only have a handful of users, that might be ok for you, but if you're dealing with a large set of users, then that will make for longer request times.

查看更多
Fickle 薄情
3楼-- · 2019-08-11 00:14

Yes you can make a ajax request and get the modal details in form of Json or Xml. Below example retrieve the user details and set it into modal form variables like userName

$.ajax({
        type: "post",
        url: "Url to get the response",
        cache: false,               
        data:'userId=' + $("#userId").val(),
        success: function(response){
            var obj = JSON.parse(response);
            $('#userName').val(obj.userName);
        },
        error: function(){                      
            alert('Error while request..');
        }
    }); 
查看更多
登录 后发表回答